【发布时间】:2017-08-22 14:54:09
【问题描述】:
我有一个自定义ListView,ListView 的每个项目都包含两个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