大家好,我在列表视图中添加了一个文本观察器,该列表视图在我的基本适配器中有很多编辑字段,但是当通过文本观察器应用文本时,我在第 226 行收到堆栈溢出错误,如果我评论这一行,然后在滚动相同的 stackoverflow第 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("");
}
}
}
}
日志信息
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)