2017-08-22 6 views
0

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); 
     } 
    } 
} 
+0

私はあなたに2つのことをお伝えしたいと思います。まず、ViewHolder Patternを使って作業し、アダプタのBaseAdapterを拡張してみてください。第2に、次回はlistViewを避けるようにしてください。代わりにRecycler Viewを使用してください。これはlistviewよりはるかに優れています –

答えて

0

- ビューの画面をオフに行くと同じビューを再利用すること画面に入ります。

iは、リストビューの最後の項目は自動的にそのビューを使用すると、EditTextにテキストを入力した別のビューから再利用されているためかもしれません

満たされていることがわかります。

また、スクロールすると、最初の項目の値が失われます。

上記と同じ理由。他のビューはこの位置で再利用されています。

ソリューション:

保存いくつかのモデル・オブジェクトのリストにテキストを入力したユーザー。たとえば、次のようになります。ArrayList<MyData>MyDataオブジェクトには、入力されたテキストがあります。リストには、リスト内の各項目に対して1つのオブジェクトがあります。

getViewコールバックでは、対応する位置のArrayList<MyData>からテキストを取得し、EditTextに設定します。

関連する問題