EditText in listview repeats the value

后端 未结 3 737
死守一世寂寞
死守一世寂寞 2020-12-22 04:57

I have three textviews and one edit text in the list. when I entered any value in the edittext, the same value is also copied in the other edittext. I tired implementing wit

3条回答
  •  [愿得一人]
    2020-12-22 05:54

    This is a good tutorial that solved my problem:

    http://www.webplusandroid.com/creating-listview-with-edittext-and-textwatcher-in-android/

    The key is:

     @Override
            public View getView(int position, View convertView, ViewGroup parent) {
     
                //ViewHolder holder = null;
                final ViewHolder holder;
                if (convertView == null) {
     
                    holder = new ViewHolder();
                    LayoutInflater inflater = ListviewActivity.this.getLayoutInflater();
                    convertView = inflater.inflate(R.layout.lyt_listview_list, null);
                    holder.textView1 = (TextView) convertView.findViewById(R.id.textView1);
                    holder.editText1 = (EditText) convertView.findViewById(R.id.editText1);    
     
                    convertView.setTag(holder);
     
                } else {
     
                    holder = (ViewHolder) convertView.getTag();
                }
     
                holder.ref = position;
     
                holder.textView1.setText(arrText[position]);
                holder.editText1.setText(arrTemp[position]);
                holder.editText1.addTextChangedListener(new TextWatcher() {
     
                    @Override
                    public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
                        // TODO Auto-generated method stub
     
                    }
     
                    @Override
                    public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                            int arg3) {
                        // TODO Auto-generated method stub
     
                    }
     
                    @Override
                    public void afterTextChanged(Editable arg0) {
                        // TODO Auto-generated method stub
                        arrTemp[holder.ref] = arg0.toString();
                    }
                });
     
                return convertView;
            }
     
            private class ViewHolder {
                TextView textView1;
                EditText editText1;
                int ref;
            }

提交回复
热议问题