ListView
の各項目にはEditTexts
という2つの項目が含まれています。 たとえば、ListView
の最初の項目にEditText
の値を入れた後、ListView
の最後までスクロールすると、ListView
の最後の項目が自動的に満たされ、またスクロールすると最初の項目その価値を失う。スクロールするとEditTextの値が失われます
注:この場合、私はTextWatcher
を使用します。 この問題を解決するにはどうすればよいですか?
この
は私のアダプタです:私はあなたがconvertView
使用ListView
でビューを再利用していることを知っていると仮定し
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);
}
}
}
私はあなたに2つのことをお伝えしたいと思います。まず、ViewHolder Patternを使って作業し、アダプタのBaseAdapterを拡張してみてください。第2に、次回はlistViewを避けるようにしてください。代わりにRecycler Viewを使用してください。これはlistviewよりはるかに優れています –