0

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 onFocusChangedListener and onTextChanged but the problem is same in both the cases.

public class ProductListAdapter extends BaseAdapter {

Context context;
public ArrayList<Products> prodList;
private static LayoutInflater inflater = null;
public ProductsList productListActivity;

public ProductListAdapter(Context context,ArrayList<Products> prodList) {
    this.context = context;
    this.prodList = prodList;
    inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    Log.e("In adapter", "yes");
}

@Override
public int getCount() {
    return prodList.size();
}

@Override
public Products getItem(int position) {
     return prodList.get(position); }

@Override
public long getItemId(int position) {
    return position;
}
@Override
public View getView( final int position, View convertView, ViewGroup parent) {
      Log.i(" in prodlist adapter","View holder");
      final   ViewHolder holder;
      if (convertView == null) {
            convertView = inflater.inflate(R.layout.productslistviewadapter, parent, false);

            holder = new ViewHolder();
            holder.tvdrCode = (TextView) convertView.findViewById(R.id.tvname);
            holder.tvDrName = (TextView) convertView.findViewById(R.id.tvprodpack);
            holder.tvterrcode= (TextView) convertView.findViewById(R.id.textView3);
            holder.caption = (EditText)convertView.findViewById(R.id.editText1);
            convertView.setTag(holder);

        } 
     else {
            holder = (ViewHolder) convertView.getTag();
        }

        Products p = prodList.get(position);
        holder.tvdrCode.setText(p.getDocCode());
        holder.tvDrName.setText(p.getDocName());
        holder.tvterrcode.setText(p.getAdr());

        //holder.caption.setText(prodList.get(position).caption);
       /* holder.caption.setId(position);

        holder.caption.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            public void onFocusChange(View v, boolean hasFocus) {
               if (!hasFocus) {
                    final int position = v.getId();
                    final EditText Caption = (EditText) v;  
                //  prodList.get(position)= Caption.getText().toString();
                    String d = prodList.get(position).getDocCode();
                    Log.e("dr code",d);
                }
            }
        }); */          

        holder.caption.setTag(position);
        // holder.caption.setText(ProdList.get(position).toString());
        int tag_position=(Integer) holder.caption.getTag();
        holder.caption.setId(tag_position); 

        holder.caption.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start,int before, int count) {
                  //2nd method  String d = planArraylist.get(position).getDocCode();
                 final int position2 = holder.caption.getId();
                 final EditText Caption = (EditText) holder.caption;
                 if(Caption.getText().toString().length()>0){
                    String d = prodList.get(position2).getDocCode();
                    Log.e("dr code",d);

                 }
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start,
                    int count, int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });  
            return convertView;
} 

 static class ViewHolder {
     TextView tvdrCode;
     TextView tvDrName;
     TextView tvterrcode;
     EditText caption;

} 
}

xml code

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" 
    android:animationCache="false"
    android:scrollingCache="false"
    android:smoothScrollbar="true" > 

<!--    android:descendantFocusability="beforeDescendants" -->

</ListView>

3
  • Here is the complete example to solve your problem.. widecodes.com/7NNzjgUqPe/…
    – Piyush
    Jul 10, 2015 at 5:24
  • Uncomment holder.caption.setText and in addTextChangedListener save string in 'prodList.get(position).caption'
    – Harin
    Jul 10, 2015 at 6:31
  • @Override public long getItemId(int position) { return prodList.indexOf(getItem(position)) ; } Jul 13, 2015 at 4:53

3 Answers 3

5

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/

0
1

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.

1
  • Thank you u save my time :)
    – Nilesh
    Sep 3, 2017 at 19:46
1

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;
        }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.