2012-06-09 190 views
0

我試圖動態改變的backgroundColor在ListView的一部分,我有例子,當我試圖用一個可擴展列表視圖複製它的另一部分工作在ListView罰款它失敗改變列表視圖(可擴展列表視圖)的backgroundColor

這段代碼的作品,如果一個學生是否在線

顯示不同的顏色...

map.put(KEY_FIRSTNAME, temp.firstName); 
map.put(KEY_NAME, temp.name); 
map.put(KEY_EMAIL, temp.email); 
map.put(KEY_ISONLINE, temp.isOnLine); 

// change image if student is online or not 
Log.d("demo", "is on line= " + temp.isOnLine); 
if (temp.isOnLine.equalsIgnoreCase("1")) { 
    map.put(KEY_IMAGE_ISONLINE, R.color.greenColor); 
} else { 
    map.put(KEY_IMAGE_ISONLINE, R.color.greyColor); 
} 
listItem.add(map); 
} 
myListView = (ListView) findViewById(R.id.listViewTabLeerlingen); 

SimpleAdapter adapter = new SimpleAdapter(StudentTab.this, 
    listItem, 
    R.layout.list_item_student, 
    new String[] { KEY_FIRSTNAME, KEY_NAME, 
    KEY_IMAGE_ISONLINE }, new int[] { 
    R.id.firstNameTextView, 
    R.id.lastNameTextView, 
    R.id.logo }); 
    myListView.setAdapter(adapter); 

與它一起去的XML

<ImageView 
      android:id="@+id/logo" 
      android:layout_width="85dp" 
      android:layout_height="match_parent" 
      android:background="@color/greenColor" 
      android:contentDescription="Image if student is online or not" 
      android:src="@drawable/transparent_pixel" /> 

上述工作正常但是下面的代碼(只是部分的代碼)

...

ArrayList<Map<String, Object>> children = new ArrayList<Map<String, Object>>(); 
for (int i = 0; i < _data.length(); i++) { 
try { 
    JSONArray tmp = _data.getJSONArray(i); 
     HashMap<String, Object> map = new HashMap<String, Object>(); 

    // change image if student is online or not 
    if (tmp.getString(3).equalsIgnoreCase("0")) { 
     map.put(KEY_POINTS,R.color.redColor); 
    }else{ 
     map.put(KEY_POINTS,R.color.greenColor); 
    } 
     map.put(KEY_QUESTIONTEXT, tmp.getString(1)); 
     map.put(KEY_ANSWER, tmp.getString(2)); 
     children.add(map); 
    } 
catch (JSONException e) { 
    e.printStackTrace(); 
} 
childData.add(children); 

...

...

ArrayList<ArrayList<Map<String, Object>>> childData) { 
      SimpleExpandableListAdapter listAdapter = new SimpleExpandableListAdapter(
        this, groupData, R.layout.list_item_results_students, 
        new String[] { KEY_FIRSTNAME, KEY_NAME, KEY_ISJUIST }, 
        new int[] { R.id.firstnameResults, R.id.nameResults, 
          R.id.resultsTextView }, childData, 
        R.layout.list_item_results_results, new String[] { 
          KEY_QUESTIONTEXT, KEY_ANSWER, KEY_POINTS }, new int[] { 
          R.id.questionTextView, R.id.answerTextTextView, R.id.score }); 
      ExpandableListView myListView = (ExpandableListView) findViewById(R.id.listViewTabResultaten); 
      myListView.setAdapter(listAdapter); 

with xml:

<ImageView 
     android:id="@+id/score" 
     android:layout_width="16dp" 
     android:layout_height="match_parent" 
     android:background="@color/greenColor" 
     android:contentDescription="Image if student has correct answer" 
     android:src="@drawable/transparent_pixel" /> 

我會得到這個錯誤:

十月6日至9日:35:21.490:E/AndroidRuntime(4406):java.lang.ClassCastException:android.widget.ImageView不能轉換爲android.widget。 TextView

回答

1

的文檔SimpleExpandableListAdapter狀態

childTo The child views that should display column in the "childFrom" parameter. These should all be TextViews. The first N views in this list are given the values of the first N columns in the childFrom parameter.

所以SimpleExpandableListAdapter只接受TextView類。 文檔狀態爲SimpleAdapter相同,但查看源代碼(如:SimpleAdapter.bindView()),你可以看到,他們也檢查ImageView等多種類型。

因此,你的第一個例子工作,但第二個例子拋出ClassCastException

要解決它,你可以覆蓋SimpleExpandableListAdapter行爲像顯示在此舊StackOverflow question

+0

看來所有的答案都是一樣的,但這個給了我一個解決方案,我只是不明白爲什麼simpleadapter接受imageview。謝謝 – Stofke

+0

我設法按照您提供的網址工作。我可以根據條件設置自定義圖像,但是在我的第一個示例中,我希望以背景顏色而不是圖像傳遞。這怎麼能實現? – Stofke

+0

好吧,我自己發現它:.setBackgroundDrawable((Drawable)...很好用 – Stofke

0

在您的SimpleExpandableListAdapter構造函數中,childTo參數包含您的ImageView項目@ + id/score。 childTo數組應該只包含TextViews。您正試圖爲圖像分配一個顏色int。

SimpleExpandableListAdapter Reference

同樣實際上是真正的爲您SimpleAdapter上面,你使用@ + ID /標誌。

Simple Adapter Reference

0

參數

方面,其中與此SimpleExpandableListAdapter相關的ExpandableListView運行地圖 groupData列表中的上下文。列表中的每個條目都對應於列表中的一個組。地圖包含每個組的數據,並應包含「groupFrom」中指定的所有條目,其中定義組的視圖的視圖佈局的groupLayout資源標識符爲「groupFrom」 。佈局文件應至少包含在「groupTo」中定義的那些命名視圖。 groupFrom將從與每個組關聯的Map中獲取的鍵列表。 groupTo應在「groupFrom」參數中顯示列的組視圖。這些都應該是TextView。這個列表中的前N個視圖給出了groupFrom參數中前N列的值。 childData地圖列表。外部列表中的每個條目對應一個組(按組的位置),內部列表中的每個條目對應組中的一個子組(索引按子位置),而Map對應於子組的數據(索引爲childFrom數組中的值)。該地圖包含每個孩子的數據,並應包括在「childFrom」中指定的所有條目。 定義兒童視圖的視圖佈局的childLayout資源標識符。佈局文件應至少包含在「childTo」中定義的那些命名視圖。childFrom將從與每個子項關聯的Map中獲取的鍵列表。 childTo應該在「childFrom」參數中顯示列的子視圖。這些都應該是TextView。該列表中的前N個視圖被賦予了childFrom參數中前N列的值。

在SimpleExpandableListAdapter.java的源代碼你會發現

私人無效bindView(查看圖,地圖數據,字符串[]從,INT []到){ INT LEN = to.length ;

for (int i = 0; i < len; i++) { 
     TextView v = (TextView)view.findViewById(to[i]); 
     if (v != null) { 
      v.setText((String)data.get(from[i])); 
     } 
    } 
}