2016-12-31 54 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