listview中的EditText重复该值

时间:2015-07-10 05:21:50

标签: android listview android-edittext

我在列表中有三个文本视图和一个编辑文本。当我在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>

3 个答案:

答案 0 :(得分:5)

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

当我们有一个带有EditText的自定义ListView时会出现问题,如果我们在第一个EditText中输入任何值并开始滚动,那么当我们滚动列表视图时,EditText的值将被逐个复制到另一个EditTexts。

当listview重用视图时,以及另一个视图中的另一个listitem,即未向上滚动的视图向上滚动它会重新使用旧列表视图,因此在新的edittext中可以看到该视图的旧值。 / p>

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

答案 1 :(得分:1)

添加到下面的答案,你应该总是使用 if else 条件将值放在 getView(..)方法中,如果没有值,则在else条件中放置空白空间,我也遇到了同样的问题。

答案 2 :(得分:0)

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

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