こんにちは、ベースアダプターに多くの編集フィールドがあるリストビューにテキストウォッチャーを追加しましたが、テキストウォッチャーを介してテキストを適用すると、行番号226でスタックオーバーフローエラーが発生します。この行にコメントすると、同じスタックオーバーフローをスクロールします行番号のエラー:223。以下のコードとlogcatメッセージで誰かが提案できますか。
class ListAdapter extends BaseAdapter {
Context ctx;
LayoutInflater lInflater;
ArrayList<Product> objects;
ProductImageLoader imageLoader;
String quantity;
EditText quantity_lit;
int loader1;
int sPosition;
Product p = null;
ListAdapter(Context context, ArrayList<Product> products) {
//super(context, R.layout.literature_items_port, products);
ctx = context;
objects = products;
lInflater = (LayoutInflater) ctx
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader=new ProductImageLoader(context.getApplicationContext());
}
class ViewHolder
{
//protected TextView text;
//protected CheckBox checkbox;
protected EditText quantity_lit;
}
@Override
public int getCount() {
return objects.size();
}
@Override
public Product getItem(int position) {
return objects.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
loader1 = R.drawable.cat_image_thumb;
if (view == null) {
view = lInflater.inflate(R.layout.literature_items_port, parent, false);
}
view.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
p = getProduct(position);
quantity_lit = (EditText) view.findViewById(R.id.quantity_lit);
((TextView) view.findViewById(R.id.catalog_name)).setText(p.name);
ImageView image = (ImageView) view.findViewById(R.id.literature_image);
imageLoader.DisplayImage(p.image,loader1, image);
CheckBox cbBuy = (CheckBox) view.findViewById(R.id.check_lit);
quantity_lit.setTag(p);
//attach the TextWatcher listener to the EditText
quantity_lit.addTextChangedListener(new MyTextWatcher(view));
if(p.getQuantity() != 0){
quantity_lit.setText(String.valueOf(p.getQuantity()));
}
else {
quantity_lit.setText("");
}
cbBuy.setOnCheckedChangeListener(myCheckChangList);
cbBuy.setTag(position);
cbBuy.setChecked(p.box);
image.setScaleType(ScaleType.FIT_XY);
return view;
}
Product getProduct(int position) {
return ((Product) getItem(position));
}
ArrayList<Product> getBox() { System.out.println("box");
ArrayList<Product> box = new ArrayList<Product>();
for (Product p : objects) {
if (p.box)
box.add(p);
/*if (!p.score.equals(""))
box.add(p);*/
}
return box;
}
OnCheckedChangeListener myCheckChangList = new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
getProduct((Integer) buttonView.getTag()).box = isChecked;
}
};
private class MyTextWatcher implements TextWatcher {
private View view;
private MyTextWatcher(View view) {
this.view = view;
}
EditText qtyView;
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//do nothing
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
//do nothing
qtyView = (EditText) view.findViewById(R.id.quantity_lit);
qtyView.removeTextChangedListener(this);//after this line you do the editing code
qtyView.addTextChangedListener(this); // you register again for listener callback
}
public void afterTextChanged(Editable s) {
String qtyString = s.toString().trim();
int quantity = qtyString.equals("") ? 0:Integer.valueOf(qtyString);
p.setQuantity(quantity);
qtyView = (EditText) view.findViewById(R.id.quantity_lit);
Product product = (Product) qtyView.getTag();
if(product.getQuantity() != 0){
//line no: 223 qtyView.setText(String.valueOf(product.getQuantity()));
}
else {
// line no: 226
qtyView.setText("");
}
}
}
}
Logcat情報
10-09 10:40:22.348: E/AndroidRuntime(5103): FATAL EXCEPTION: main
10-09 10:40:22.348: E/AndroidRuntime(5103): java.lang.StackOverflowError
10-09 10:40:22.348: E/AndroidRuntime(5103): at android.widget.TextView.invalidateCursor(TextView.java:4467)
10-09 10:40:22.348: E/AndroidRuntime(5103): at android.widget.TextView.spanChange(TextView.java:7325)
10-09 10:40:22.348: E/AndroidRuntime(5103): at android.widget.TextView$ChangeWatcher.onSpanAdded(TextView.java:8907)
10-09 10:40:22.348: E/AndroidRuntime(5103): at android.text.SpannableStringBuilder.sendSpanAdded(SpannableStringBuilder.java:979)
10-09 10:40:22.348: E/AndroidRuntime(5103): at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:688)
10-09 10:40:22.348: E/AndroidRuntime(5103): at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:588)
10-09 10:40:22.348: E/AndroidRuntime(5103): at android.text.Selection.setSelection(Selection.java:76)
10-09 10:40:22.348: E/AndroidRuntime(5103): at android.text.Selection.setSelection(Selection.java:87)
10-09 10:40:22.348: E/AndroidRuntime(5103): at android.text.method.ArrowKeyMovementMethod.initialize(ArrowKeyMovementMethod.java:302)
10-09 10:40:22.348: E/AndroidRuntime(5103): at android.widget.TextView.setText(TextView.java:3684)
10-09 10:40:22.348: E/AndroidRuntime(5103): at android.widget.TextView.setText(TextView.java:3554)
10-09 10:40:22.348: E/AndroidRuntime(5103): at android.widget.EditText.setText(EditText.java:80)
10-09 10:40:22.348: E/AndroidRuntime(5103): at android.widget.TextView.setText(TextView.java:3529)
10-09 10:40:22.348: E/AndroidRuntime(5103): at com.amit.listview.MainActivity$ListAdapter$MyTextWatcher.afterTextChanged(MainActivity.java:226)
10-09 10:40:22.348: E/AndroidRuntime(5103): at android.widget.TextView.sendAfterTextChanged(TextView.java:7247)