0

大家好,我在列表视图中添加了一个文本观察器,该列表视图在我的基本适配器中有很多编辑字段,但是当通过文本观察器应用文本时,我在第 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)
4

2 回答 2

0

嗨,在 viewHolder 类中,您必须声明 TextView、ImageView 和 CheckBox。

static class viewHolder {
            TextView tsub;
        ImageView tmore;
            ........
    }

然后在 getView 函数中像这样编辑代码;

@Override
        public View getView(int position, View convertView, ViewGroup parent) {
            viewHolder holder;
            final int pos_st = position;
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.listso, null);
                holder = new viewHolder();
                convertView.setTag(holder);
            } else {
                holder = (viewHolder) convertView.getTag();
            }
            holder.tsub = (TextView) convertView.findViewById(R.id.textViewSub);
            holder.tsub.setText("Hai");
return convertView;
}

试试这个..让我知道。

于 2013-10-09T11:27:08.523 回答
0

在这一行中qtyView.setText("");,您更改注册到 qtyView 的 TextWatcher 的 s 字符串。所以你对你的 TextWatcher 做了一个无限循环,它以 StackOverFlow 错误结束。

在 TextWatcher 文档afterTextChanged 中

调用此方法是为了通知您,在 s 中的某处,文本已更改。从此回调中对 s 进行进一步更改是合法的,但请注意不要让自己陷入无限循环,因为您所做的任何更改都会导致再次递归调用此方法。(您不会被告知更改发生的位置,因为其他 afterTextChanged() 方法可能已经进行了其他更改并使偏移量无效

于 2013-10-09T11:30:01.470 回答