ListView 中的 EditText 在滚动时更改或复制内容

EditText in ListView changes or duplicates content while scrolling

我的 ListView 中有两个 EditText。我想要的只是向两个 EditTexts 添加一个 TextWatcher ,这样我就可以避免在滚动时复制和更改其中的值。我可以成功地将 TextWatcher 添加到其中一个 EditText,但无法为两个 EditText 实现。我尝试了 Whosebug 的其他答案。但是我没有得到想要的输出。 这是我的代码:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.orders);
    list = (ListView)findViewById(R.id.list1);
     this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
       SelectItems();
    ViewItems();
    }
public List<Map<String, String>> SelectItems() {
    // TODO Auto-generated method stub
try {

       datas = new ArrayList<Map<String, String>>();
        DatabaseHelper dbHelper = new DatabaseHelper(this.getApplicationContext());
    newDB = dbHelper.getWritableDatabase();
    Cursor c = newDB.rawQuery("select distinct im_code ,im_desc ,im_srp"
            + " from itemmaster", null);
    c.moveToFirst();
        while (c.moveToNext()){
    datanums.put("name",c.getString(c.getColumnIndex("im_desc"))); 
          datanums.put("code", c.getString(c.getColumnIndex("im_code")));
          datanums.put("imsrp",c.getString(c.getColumnIndex("im_srp"))); 
       datas.add(datanums);
        }
    } catch (SQLiteException se ) {
        Log.e(getClass().getSimpleName(), "Could not create or Open the database");
       }
return datas; 
}
public void ViewItems() {
    // TODO Auto-generated method stub
    arrTemp = new String[datas.size()];
     MyListAdapter myListAdapter = new MyListAdapter();
        list.setAdapter(myListAdapter);
     }
 public  class MyListAdapter extends BaseAdapter{
     @Override
        public int getCount() {
            // TODO Auto-generated method stub
            if(datas != null && datas.size() != 0){
                return datas.size();    
            }
            return 0;
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return datas.get(position);
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {

            //ViewHolder holder = null;
            final  ViewHolder holder;
            if (convertView == null) {

                holder = new ViewHolder();
                LayoutInflater inflater = Orders.this.getLayoutInflater();
                convertView = inflater.inflate(R.layout.order_simple_row, null);
                holder.textView1 = (TextView) convertView.findViewById(R.id.name);
                holder.textView2 = (TextView) convertView.findViewById(R.id.srp);
                holder.textview3 = (TextView) convertView.findViewById(R.id.code);

                holder.editText1 = (EditText) convertView.findViewById(R.id.cases);    
                holder.editText2 = (EditText) convertView.findViewById(R.id.pcs); 

                convertView.setTag(holder);

            } else {

                holder = (ViewHolder) convertView.getTag();
            }
          holder.ref = position;
            String [] names = new String[550];
            String [] codes = new String[550];
            String [] prize = new String[550];

                DatabaseHelper dbHelper = new DatabaseHelper(Orders.this.getApplicationContext());
            newDB = dbHelper.getWritableDatabase();
            Cursor c = newDB.rawQuery("select distinct im_code ,im_desc ,im_srp "
                    + " from itemmaster", null);
            Log.v("item detailss", c.toString());   
                c.moveToFirst();

                while (c.moveToNext()){


               String cod = c.getString(c.getColumnIndex("im_code"));
             codes[c.getPosition()] =cod;
                String desc1  = c.getString(c.getColumnIndex("im_desc"));
                names[c.getPosition()] = desc1;

                 String price = c.getString(c.getColumnIndex("im_srp"));
                prize[c.getPosition()] = price;

                }
            newDB.close();
                 holder.textView1.setText(names[position+1]);
                holder.textview3.setText(codes[position+1]);
                     holder.textView2.setText(prize[position+1]);

            holder.editText1.setText(arrTemp[holder.ref]);

            holder.editText2.setText(arrTemp[holder.ref]);
           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();
             datas.get(position).put("pref", holder.editText1.toString().trim());
                }
            });
 return convertView;
    }
        private class ViewHolder {
            TextView textView1,textView2,textview3;
         EditText editText1,editText2;
           public  int ref;
            }
    }

请帮助我以正确的方式做到这一点。

et1.addTextChangedListener(new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

            // TODO Auto-generated method stub
        }

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

            // TODO Auto-generated method stub
        }

        @Override
        public void afterTextChanged(Editable s) {

            // TODO Auto-generated method stub
        }
    });

Use different textwatchers for each edit text if you want to perform different operations on each edit text

EditText1.addTextChangedListener(new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

            // TODO Auto-generated method stub
        }

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

            // TODO Auto-generated method stub
        }

        @Override
        public void afterTextChanged(Editable s) {

            // TODO Auto-generated method stub
        }
    });

Apply same for other EditText too

最近遇到了类似的问题,下面是我用HashMap解决的方法:

在适配器构造函数中,初始化 HashMap 并确保清除它:

public MyAdapter(Context context, String[] texts){
    this.context = context;
    this.texts = texts;

    hashMapTexts = new HashMap<String,String>();
    for(String text: texts){
        hashMapTexts.put(text, "");
}

然后,像这样更正适配器内的私有 class ViewHolder:

private class ViewHolder {

    private TextView listItemTextView;
    private EditText listItemEditText;
    private int position;

    public ViewHolder(View view) {
        listItemTextView = (TextView)view.findViewById(R.id.list_item_text_view);
        listItemEditText = (EditText)view.findViewById(R.id.list_item_edit_text);
    }
}

然后,在覆盖的 getView() 方法中初始化 ViewHolder:

if(convertView == null){
        convertView = View.inflate(context, R.layout.list_item, null);
        viewHolder = new ViewHolder(convertView);

        convertView.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder)convertView.getTag();
}

同方法获取ViewHolder的位置:

viewHolder.position = position;

为您的观点设置文本:

    viewHolder.listItemTextView.setText(texts[position]);
    viewHolder.listItemEditText.setText(hashMapTexts.get(texts[position]));

现在使用 TextWatcher:

viewHolder.listItemFormEditText.addTextChangedListener(new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
            hashMapTexts.put(texts[viewHolder.position], s.toString());
    }
});

return convertView;

我建议你从更简单的案例开始,就像我的一样,然后将其应用到你的具体场景中。