【问题标题】:EditText loses value when scrolling ListView android滚动 ListView android 时 EditText 失去价值
【发布时间】:2017-08-22 14:54:09
【问题描述】:

我有一个自定义ListViewListView 的每个项目都包含两个EditTexts。 例如,当我将EditTexts 的值放在ListView 的第一项中,然后向下滚动到ListView 的末尾时,我看到ListView 的最后一项被自动填充,并且当我向上滚动,第一项失去了它的价值。

注意:在这种情况下我使用TextWatcher。 我应该怎么做才能解决这个问题?

这是我的适配器:

public class MyResultAdapter extends ArrayAdapter<Integer> {

    ArrayList<HashMap<String, String>> boardInformation = new ArrayList<>();

    EditText foodPrice;
    EditText foodName;

    Context context;
    int layoutView;

    public MyResultAdapter(Context context, int layoutView) {
        super(context, layoutView);

        this.context = context;
        this.layoutView = layoutView;
    }

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

        View view = convertView;

        boolean convertViewWasNull = false;
        if(view == null)
        {
            view = LayoutInflater.from(getContext()).inflate(layoutView, parent, false);
            convertViewWasNull = true;
        }

        foodPrice = (EditText) view.findViewById(R.id.food_price);
        foodName = (EditText) view.findViewById(R.id.food_name);

        if(convertViewWasNull )
        {
            //be aware that you shouldn't do this for each call on getView, just once by listItem when convertView is null
            foodPrice.addTextChangedListener(new GenericTextWatcher(foodPrice, position, "price"));
            foodName.addTextChangedListener(new GenericTextWatcher(foodName, position, "name"));
        }

        return view;
    }

    private class GenericTextWatcher implements TextWatcher{

        private View view;

        private int position;
        private String name;

        private GenericTextWatcher(View view, int position, String name) {
            this.view = view;
            this.position = position;
            this.name = name;
        }

        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {}

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

        public void afterTextChanged(Editable editable)
        {
            updateBoardInformationArray(editable.toString());
        }


        private void updateBoardInformationArray(String newValue)
        {
            if(name.equals("name")) boardInformation.get(position).put("food_name", newValue);
            else boardInformation.get(position).put("food_price", newValue);
        }
    }
}

【问题讨论】:

  • 好吧,伙计,我建议您做两件事,首先尝试使用 ViewHolder 模式并为您的适配器扩展 BaseAdapter。其次,下次尽量避免使用listView——改用Recycler View,比listview好很多

标签: android listview scroll android-edittext textwatcher


【解决方案1】:

我假设您知道您正在使用convertView 重用ListView 中的视图——重用离开屏幕的相同视图用于进入屏幕的视图。

我看到listview的最后一项是自动填充的

这可能是因为该视图已从您在 EditText 中输入一些文本的另一个视图中重复使用。

当我向上滚动时,第一项会丢失其值。

原因同上。在这个位置上正在重用其他一些视图。

解决方案:

将用户输入的文本保存在一些模型对象的列表中。例如,像这样ArrayList&lt;MyData&gt;MyData 对象具有键入的文本。列表中的每个项目都有一个对象。

现在在getView 回调中从ArrayList&lt;MyData&gt; 中获取相应位置的文本并将其设置为EditText

【讨论】:

  • 感谢您拯救我的一天。
猜你喜欢
  • 2014-02-02
  • 1970-01-01
  • 2018-05-01
  • 2017-05-22
  • 2014-12-18
  • 1970-01-01
  • 2018-03-16
  • 2014-10-06
相关资源
最近更新 更多