2014-10-04 33 views
0

希望有人能幫助我。我有一個包含listview的佈局,列表視圖中的每個項目都包含表示星期幾的單獨文本字段 - > SMTWTFS 我想要做的是更改文本項的顏色,如果它們在數據庫。這幾乎可以工作,但我注意到當列表視圖中的第一行在其他列表行中設置時,所有項目的顏色都會改變。來自光標的Android ViewBinder值

我的文字資料的聲明如下

 textSUN = (TextView) v.findViewById(R.id.textSUN); 
     textMON = (TextView) v.findViewById(R.id.textMON); 
     textTUE = (TextView) v.findViewById(R.id.textTUE); 
     textWED = (TextView) v.findViewById(R.id.textWED); 
     textTHU = (TextView) v.findViewById(R.id.textTHU); 
     textFRI = (TextView) v.findViewById(R.id.textFRI); 
     textSAT = (TextView) v.findViewById(R.id.textSAT); 

然後我查詢我的數據庫,並設置適配器

db = new DatabaseHandler(getActivity()); 

      mCursor=db.getReadableDatabase().rawQuery("SELECT rowid _id,* "+ 
           "FROM table", null); 

      adapter = new SimpleCursorAdapter(getActivity(), 
        R.layout.list_row, mCursor, 
        new String[] {DatabaseHandler.KEY_SUNDAY, DatabaseHandler.KEY_MONDAY,DatabaseHandler.KEY_TUESDAY,DatabaseHandler.KEY_WEDNESDAY, DatabaseHandler.KEY_THURSDAY, DatabaseHandler.KEY_FRIDAY, 
          DatabaseHandler.KEY_SATURDAY,}, 
        new int[] {R.id.textSUN,R.id.textMON, R.id.textTUE, R.id.textWED, R.id.textTHU, R.id.textFRI, R.id.textSAT}); 

最後,我嘗試設置在視圖中粘結劑的顏色,如果返回的值是1從分區

adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() { 
       @Override 
      public boolean setViewValue(View view, Cursor cursor, int 
        columnIndex) { 

       final int sun_column = cursor.getColumnIndex("sun"); 
       final int mon_column = cursor.getColumnIndex("mon"); 
       final int tue_column = cursor.getColumnIndex("tue"); 
       final int wed_column = cursor.getColumnIndex("wed"); 
       final int thu_column = cursor.getColumnIndex("thu"); 
       final int fri_column = cursor.getColumnIndex("fri"); 
       final int sat_column = cursor.getColumnIndex("sat"); 

       } 

       if (columnIndex == sun_column) { 

        int sun = cursor.getInt(cursor.getColumnIndexOrThrow("sun")); 
        if(sun == 1){ 
         ((TextView) view).setTextColor(Color.parseColor("#FFFFFF")); 
        } 
       return true; 

       } 

       if (columnIndex == mon_column) { 

        int mon = cursor.getInt(cursor.getColumnIndexOrThrow("mon")); 
        if(mon == 1){ 
         ((TextView) view).setTextColor(Color.parseColor("#FFFFFF")); 
        } 
       return true; 

       } 

       if (columnIndex == tue_column) { 

        int tue = cursor.getInt(cursor.getColumnIndexOrThrow("tue")); 
        if(tue == 1){ 
         ((TextView) view).setTextColor(Color.parseColor("#FFFFFF")); 
        } 
       return true; 

       } 

       if (columnIndex == wed_column) { 

        int wed = cursor.getInt(cursor.getColumnIndexOrThrow("wed")); 
        if(wed == 1){ 
         ((TextView) view).setTextColor(Color.parseColor("#FFFFFF")); 
        } 
       return true; 

       } 

       if (columnIndex == thu_column) { 

        int thu = cursor.getInt(cursor.getColumnIndexOrThrow("thu")); 
        if(thu == 1){ 
         ((TextView) view).setTextColor(Color.parseColor("#FFFFFF")); 
        } 
       return true; 

       } 

       if (columnIndex == fri_column) { 

        int fri = cursor.getInt(cursor.getColumnIndexOrThrow("fri")); 
        if(fri == 1){ 
         ((TextView) view).setTextColor(Color.parseColor("#FFFFFF")); 
        } 
       return true; 

       } 

       if (columnIndex == sat_column) { 

        int sat = cursor.getInt(cursor.getColumnIndexOrThrow("sat")); 
        if(sat == 1){ 
         ((TextView) view).setTextColor(Color.parseColor("#FFFFFF")); 
        } 
       return true; 

       } 
       return false; 
       } 
      }); 

     lv = (ListView) v.findViewById(R.id.listView); 

     lv.setAdapter(adapter); 

也許我正在談論這一切都是錯誤的?它幾乎可以工作。

非常感謝

回答

0

你仍然需要處理,其中週一,週二,等等== 0添加以下到每個案件的if語句應該修復它:

else { 
((TextView) view).setTextColor(//color that you want if day == 0); 
} 

此外,您可能想要考慮改變你的整個setViewValue來使用switch語句,它應該讓cod更具可讀性。