listview中的EditText重复值

EditText in listview repeats the value

我在列表中有三个文本视图和一个编辑文本。当我在 edittext 中输入任何值时,相同的值也会复制到另一个 edittext 中。我厌倦了同时使用 onFocusChangedListener 和 onTextChanged 来实现,但是这两种情况下的问题都是一样的。

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代码

<?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>

正如我们所知,当我们滚动整个列表时,ListView 会重用 ListItem 的视图。

因此,当我们有一个带有 EditText 的自定义 ListView 时,如果我们在第一个 EditText 中输入任何值并开始滚动,那么当我们滚动 listview 时,一个 EditText 的值将一个一个地复制到另一个 EditText。

这发生在列表视图重用该视图和另一个视图中的另一个列表项时,即未看到的视图向上滚动它重用旧列表视图,因此在新编辑文本中可以看到该视图的旧值.

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

添加到下面的答案中,您应该始终使用 if else 条件将值放入 getView(..) 方法中,如果没有将 put empty space 放在 else 条件下,我也遇到了同样的问题。

这是一个很好的教程,解决了我的问题:

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

关键是:

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