Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/191.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 更改listview中的背景色(可扩展listview)_Android_Android Layout - Fatal编程技术网

Android 更改listview中的背景色(可扩展listview)

Android 更改listview中的背景色(可扩展listview),android,android-layout,Android,Android Layout,我试图在listview的一部分中动态更改背景颜色,我有一个例子,在listview中效果很好,当我尝试在另一部分中使用可扩展listview复制它时,它失败了 如果学生在线或不在线,这段代码可以工作并显示不同的颜色 与之配套的xml <ImageView android:id="@+id/logo" android:layout_width="85dp" android:layout_height="match_p

我试图在listview的一部分中动态更改背景颜色,我有一个例子,在listview中效果很好,当我尝试在另一部分中使用可扩展listview复制它时,它失败了

如果学生在线或不在线,这段代码可以工作并显示不同的颜色

与之配套的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 children=new ArrayList();
对于(int i=0;i<_data.length();i++){
试试{
JSONArray tmp=_data.getJSONArray(i);
HashMap=newHashMap();
//如果学生在线或不在线,则更改图像
if(tmp.getString(3.equalsIgnoreCase(“0”)){
map.put(关键点,R.color.redColor);
}否则{
地图放置(关键点,R.color.greenColor);
}
map.put(KEY_QUESTIONTEXT,tmp.getString(1));
map.put(KEY_ANSWER,tmp.getString(2));
添加(地图);
} 
捕获(JSONException e){
e、 printStackTrace();
}
添加(子项);

arraylistchilddata){
SimpleExpandableListAdapter listAdapter=新SimpleExpandableListAdapter(
这个,groupData,R.layout.list\u item\u results\u students,
新字符串[]{KEY_FIRSTNAME,KEY_NAME,KEY_ISJUIST},
新int[]{R.id.firstnameResults,R.id.nameResults,
R.id.resultsTextView},childData,
R.layout.list_项_结果_结果,新字符串[]{
关键问题文本,关键答案,关键点},新int[]{
R.id.questionTextView,R.id.answerTextView,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

在SimpleExpandableListAdapter的构造函数中childTo参数包含ImageView项@+id/score。childTo数组应仅包含TextView。您正在尝试为图像分配颜色int

对于上面使用@+id/徽标的SimpleAdapter,事实上也是如此


SimpleExpandableListAdapter的文档说明

childTo应显示在“childFrom”列中的子视图 参数。这些都应该是文本视图。此参数中的前N个视图 列表中给出了childFrom中前N列的值 参数

因此,
SimpleExpandableListAdapter
只接受
TextView
类。 文档中对
simpledapter
的说明是相同的,但是查看源代码(例如),您可以看到它们还检查
ImageView
和其他类型

因此,您的第一个示例有效,但第二个示例抛出了
ClassCastException

要修复此问题,您可以重写类似于此旧参数中所示的行为

上下文与此SimpleExpandableListAdapter关联的ExpandableListView正在运行的上下文 groupData映射列表。列表中的每个条目对应于列表中的一个组。映射包含每个组的数据,并且应该包括“groupFrom”中指定的所有条目 groupLayout视图布局的资源标识符,用于定义组的视图。布局文件应至少包括“groupTo”中定义的命名视图 groupFrom将从与每个组关联的映射中提取的键列表。 groupTo应在“groupFrom”参数中显示列的组视图。这些视图都应为TextView。此列表中的前N个视图提供了groupFrom参数中前N列的值。 childData映射列表。外部列表中的每个条目对应一个组(按组位置索引),内部列表中的每个条目对应一个组内的子级(按子级位置索引),映射对应一个子级的数据(按childFrom数组中的值索引)。映射包含每个子级的数据,并且应该包括“childFrom”中指定的所有条目 定义子视图的视图布局的childLayout资源标识符。布局文件应至少包括“childTo”中定义的命名视图 child从将从与每个子项关联的映射中提取的键列表中提取。 childTo应在“childFrom”参数中显示列的子视图。这些都应是文本视图。此列表中的前N个视图给出了childFrom参数中前N列的值。

在SimpleExpandableListAdapter.java的源代码中,您会发现:

私有void bindView(视图视图,映射数据,字符串[]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]));
        }
    }
}
for(int i=0;i
似乎所有答案都是一样的,但这一个给了我一个解决方案,我只是不明白为什么simpleadapter接受imageview。感谢我按照您提供的url成功地让它工作。我可以根据条件设置自定义图像,但就像在我的第一个示例中一样,我希望输入背景色这是怎么实现的呢?好的,我自己刚发现:.setBackgroundDrawable((Drawable)…效果很好
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);
<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" />
    for (int i = 0; i < len; i++) {
        TextView v = (TextView)view.findViewById(to[i]);
        if (v != null) {
            v.setText((String)data.get(from[i]));
        }
    }
}