Skip to content Skip to sidebar Skip to footer

Edittext In Listview Repeats The Value

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 with both

Solution 1:

As we know ListView reuses the view of ListItem as we scroll the whole list.

So problem arises when we have a custom ListView with EditText where if we enter any value in the first EditText and start scrolling then the value of EditText one is copied to another the EditTexts one by one as we scroll the listview .

This happens as the listview reuses the view and as the other listitem from another view i.e. the view which is not seen scrolls upwards it reuses the old lists view and hence the old value of that view is seen in the new edittext.

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

Solution 2:

Adding to below answer , you should always use if else condition to put values in getView(..) method , if no value the put empty space in else condition , I too faced the same issue.

Solution 3:

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 ViewgetView(int position, View convertView, ViewGroup parent) {
 
            //ViewHolder holder = null;
            final ViewHolder holder;
            if (convertView == null) {
 
                holder = newViewHolder();
                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(newTextWatcher() {
 
                @Override
                public voidonTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
                    // TODO Auto-generated method stub
 
                }
 
                @Override
                public voidbeforeTextChanged(CharSequence arg0, int arg1, int arg2,
                        int arg3) {
                    // TODO Auto-generated method stub
 
                }
 
                @Override
                public voidafterTextChanged(Editable arg0) {
                    // TODO Auto-generated method stub
                    arrTemp[holder.ref] = arg0.toString();
                }
            });
 
            return convertView;
        }
 
        private classViewHolder {
            TextView textView1;
            EditText editText1;
            int ref;
        }

Post a Comment for "Edittext In Listview Repeats The Value"