Changing backgroundcolor in listview (expandable listview)
- by Stofke
I'm trying to dynamically change a backgroundcolor in a part of a listview, I have on example that works fine in a listview when I try to replicate it in another part with an expandable listview it fails
This piece of code works and displays a different color if a student is online or not
...
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);
the xml that goes along with it
<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" />
The above works fine however the following code (just part of the code)
...
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" />
I will get this error:
06-09 10:35:21.490: E/AndroidRuntime(4406): java.lang.ClassCastException: android.widget.ImageView cannot be cast to android.widget.TextView