2012-06-09 51 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])); 
     } 
    } 
}