2016-12-31 8 views
0

私はうまく動作し、すべてのデータを表示する単純なカーソルアダプタを持っています。ビューをリサイクルし、(色を追加することによって)選択したように、新しいアイテムをマーキングされたスクロールアダプタにAndroid SimpleCursorAdapterはスクロールでスタイリングを維持します

listViewM.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
       public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
        boolean exists = false; 
        TextView item = (TextView) view.findViewById(R.id.r_lv_name); 
        String selectedAnswer = item.getText().toString(); 
        MultiSelection multiSelection = new MultiSelection((int) id, selectedAnswer); 

        for (MultiSelection mm : mMultiSelectionsArray) { 
         if (id == mm.getId()) { 
          mMultiSelectionsArray.remove(mm); 
          exists = true; 
          break; 
         } else { 
          exists = false; 
         } 
        } 

        if (!exists) { 

         mMultiSelectionsArray.add(multiSelection); 
         item.setTextColor(Color.parseColor("#2EFE2E")); 
        } else { 

         mMultiSelectionsArray.remove(multiSelection); 
         item.setTextColor(Color.parseColor("#000000")); 
        } 

       } 
      }); 

: 私のリスナーは、クリックで色が変わります。私は何とかステータスを保持し、ビューの作成にそれを適用する必要があると思いますが、見て3日後に私はあきらめます。誰でも助けてくれますか?

答えて

1

カスタムアダプタを使用しよう、のようなもの:

public class CustomSimpleCursorAdapter extends SimpleCursorAdapter { 

    public CustomSimpleCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to, int flags) { 
     super(context, layout, c, from, to, flags); 
    } 

    @Override 
    public View newView(Context context, Cursor cursor, ViewGroup parent) { 
     return super.newView(context, cursor, parent); 
    } 

    @Override 
    public void bindView(View view, Context context, Cursor cursor) { 
     super.bindView(view, context, cursor); 

     //HERE you can set the correct color for each item 

     TextView item = (TextView) view.findViewById(R.id.r_lv_name); 
     boolean exists = //check is item is selected 
     if (!exists) { 
      item.setTextColor(Color.parseColor("#2EFE2E")); 
     } else { 
      item.setTextColor(Color.parseColor("#000000")); 
     } 
    } 
} 
+0

うん、私はすでにこれをやったが、それは正しいアプローチだ - 私の代わりにアダプタのメインスレッドでそれを確認しようとした - それが今の私のために働いて - 感謝を – sziszu

関連する問題