我尝试在列表视图的一部分中动态更改背景颜色,例如,当我尝试将其复制到具有可扩展列表视图的另一部分中时,它在列表视图中工作得很好,但失败了
如果学生在线或不在线,这段代码将以不同的颜色显示
..。
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);使用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" />我将得到这个错误:
06-09 10:35:21.490: E/AndroidRuntime(4406):java.lang.ClassCastException: android.widget.ImageView不能转换为android.widget.TextView
发布于 2012-06-09 18:37:34
SimpleExpandableListAdapter状态的文档
childTo应该在"childFrom“参数中显示列的子视图。这些都应该是TextViews。此列表中的前N个视图被赋予childFrom参数中前N列的值。
所以SimpleExpandableListAdapter只接受TextView类。文档中对SimpleAdapter的说明与此相同,但请查看源代码(例如,SimpleAdapter.bindView())您可以看到,它们还检查ImageView和其他类型。
因此,您的第一个示例起作用了,但是第二个示例抛出了ClassCastException。
要修复它,您可以覆盖SimpleExpandableListAdapter行为,如此较旧的StackOverflow question中所示
发布于 2012-06-09 18:15:33
在SimpleExpandableListAdapter的构造函数中,childTo参数包含您的分数项@+id/ ImageView。childTo数组应仅包含TextViews。您正在尝试将颜色int分配给图像。
SimpleExpandableListAdapter Reference
对于上面使用@+id/ SimpleAdapter的徽标,实际上也是如此。
发布于 2012-06-09 18:41:14
参数
上下文与此SimpleExpandableListAdapter关联的ExpandableListView正在运行groupData A映射列表的上下文。列表中的每个条目对应于列表中的一个组。地图包含每个组的数据,并且应该包括在定义组视图的视图布局的"groupFrom“groupLayout资源标识符中指定的所有条目。布局文件应该至少包括那些在"groupTo“groupFrom中定义的命名视图将从与每个组关联的映射中获取关键字列表。groupTo应该在"groupFrom“参数中显示列的组视图。这些都应该是TextViews。此列表中的前N个视图被赋予groupFrom参数中前N列的值。childData映射列表列表。外部列表中的每个条目对应于一个组(通过组位置进行索引),内部列表中的每个条目对应于组中的一个子项(通过子项位置进行索引),而映射对应于子项的数据(通过childFrom数组中的值进行索引)。映射包含每个子视图的数据,并且应该包括在定义子视图的视图布局的"childFrom“childLayout资源标识符中指定的所有条目。布局文件应该至少包括那些在"childTo“childFrom中定义的命名视图将从与每个子级关联的映射中获取键的列表。childTo应该在"childFrom“参数中显示列的子视图。这些都应该是TextViews。此列表中的前N个视图被赋予childFrom参数中前N列的值。
在SimpleExpandableListAdapter.java的源代码中,您会发现:
private void bindView(视图视图,地图数据,String[] from,int[] to) { 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]));
}
}
}https://stackoverflow.com/questions/10959932
复制相似问题