【问题标题】:Android ListView with EditText values duplicating with TextWatcher带有与 TextWatcher 重复的 EditText 值的 Android ListView
【发布时间】:2019-03-23 21:56:33
【问题描述】:

我有一个 ViewHolder:

import android.widget.EditText;
import android.widget.TextView;

public class ListViewHolder {
    TextView productName;
    EditText productStock;
}

还有我的 ListAdapter(listProducts 是 Product 对象的 ArrayList):

public Product getItem(int position){
    return listProducts.get(position);
}


public View getView(final int position, View convertView, ViewGroup parent){
    View row;
    final ListViewHolder listViewHolder;

    if(convertView == null){
        LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = layoutInflater.inflate(R.layout.set_stock_listview,parent,false);
        listViewHolder = new ListViewHolder();
        listViewHolder.productName = (TextView) row.findViewById(R.id.productName);
        listViewHolder.productStock = (EditText) row.findViewById(R.id.productStock);
        row.setTag(listViewHolder);
    }
    else{
        row = convertView;
        listViewHolder = (ListViewHolder) row.getTag();
    }

    final Product product = getItem(position);

    if(product.getDesiredQuantity()>0){
        listViewHolder.productStock.setText(String.valueOf(product.getDesiredQuantity()));
    }

    listViewHolder.productName.setText(product.getName()+":");
    listViewHolder.productStock.setInputType(InputType.TYPE_CLASS_NUMBER);
    listViewHolder.productStock.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        }

        @Override
        public void afterTextChanged(Editable editable) {
            product.setDesiredQuantity(Integer.valueOf(editable.toString()));
        }
    });
    return row;
}

但是,当滚动列表时,EditText 的值与许多其他行重复。看起来它也必须更新其他 Product 对象,但我找不到问题,有什么想法吗?

【问题讨论】:

    标签: java android listview android-edittext listadapter


    【解决方案1】:

    问题是视图可以回收(通过convertView 参数)和TextView 仅允许您添加 TextWatcher 的事实的结合。在您发布的代码中,每次使用非空 convertView 调用 getView() 时,您将创建一个 additional TextWatcher 并将其分配给 productStock... 和之前的观察者将保持原地更新之前绑定的product

    您必须删除现有的TextWatcher(如果有的话)。我建议使用ListViewHolder 来完成此操作。

    public class ListViewHolder {
        TextView productName;
        EditText productStock;
        TextWatcher productStockWatcher;
    }
    

    然后改变这个:

    listViewHolder.productStock.addTextChangedListener(new TextWatcher() { ... });
    

    到这里:

    if (listViewHolder.productStockWatcher != null) {
        listViewHolder.productStock.removeTextChangedListener(productStockWatcher);
    }
    
    listViewHolder.productStockWatcher = new TextWatcher() { ... };
    listViewHolder.productStock.addTextChangedListener(listViewHolder.productStockWatcher);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-17
      相关资源
      最近更新 更多