Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/379.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
Java 如何按位置设置listview中特定项目的背景色?_Java_Android_Mobile - Fatal编程技术网

Java 如何按位置设置listview中特定项目的背景色?

Java 如何按位置设置listview中特定项目的背景色?,java,android,mobile,Java,Android,Mobile,我想在listview中设置特定项的背景色 respondMessageListView = (ListView) findViewById(R.id.respondMessageListView); respondMessageListView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, autoRespondMessages.getMessages())); 我的l

我想在listview中设置特定项的背景色

respondMessageListView = (ListView) findViewById(R.id.respondMessageListView);
respondMessageListView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, autoRespondMessages.getMessages()));
我的listview由ArrayAdapter使用ArrayList生成

我在listview中有一个特定的项目,我计划更改背景颜色

我知道该项目在列表中的位置

这是我生成listview的代码

respondMessageListView = (ListView) findViewById(R.id.respondMessageListView);
respondMessageListView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, autoRespondMessages.getMessages()));
respondMessageListView=(ListView)findViewById(R.id.respondMessageListView);
setAdapter(新的ArrayAdapter(这个,android.R.layout.simple_list_item_1,autoRespondMessages.getMessages());
谢谢大家!

[编辑]


根据,如果在onCreate()中使用setSelection,则使用setSelection无效,解决方法是“在
PullToRefreshListView
中删除方法
onAttachedToWindow
”。我不太明白解决办法。请问我应该如何做到这一点?我是
活动
的子类,因此我不能再对任何其他类进行子类化。

您必须对
数组适配器
进行子类化并重写该方法。为了简单起见,您可以直接调用基类实现并为返回的
视图设置背景色

编辑: 下面的示例以黑白交替的方式对项目的背景进行着色

private class MyAdapter extends ArrayAdapter {

    ...

    public View getView(int position, View convertView, ViewGroup parent) {
        View v = super.getView(position, convertView, parent);
        v.setBackgroundColor(position % 2 == 0 : 0xff000000, 0xffffffff);
    }
}

此代码用于选择listitem时的

请尝试此代码

listview.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> myAdapter, View myView, int pos, long mylng) {

             if( pos == 1) {
                   // to change the listview background
                   listview.setBackgroundColor(getResources().getColor(R.color.your_color_id));

                   // to change the selected item background color
                   myView.setBackgroundColor(getResources().getColor(R.color.your_color_id));
             }
            }
          });
listview.setOnItemClickListener(新的OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView myAdapter、View myView、int pos、long mylng){
如果(位置==1){
//更改listview背景的步骤
setBackgroundColor(getResources().getColor(R.color.your_color_id));
//更改选定项目背景色的步骤
myView.setBackgroundColor(getResources().getColor(R.color.your_color_id));
}
}
});

祝你好运。

你试过了吗?我试过了,它在myList.getSelectedView().setBackgroundColor(getResources().getColor(Color.RED))上给了我nullpointerexception;不一样。子类化ArrayAdapter允许您重写必要的方法。子类化活动不会给您带来任何好处。给,嗯。。我的意思是我只能给一个类划分子类。。。所以,如果我将Activity子类化,我就不能再将ArrayAdapter子类化了。您可以根据需要将任意多个类子类化。我想你把这和多重继承混淆了。对不起,我还是不明白。Java只接受关键字扩展后的一个类。。。如何将Activity和ArrayAdapter都分为子类?不要说
respondMessageListView.setAdapter(新ArrayAdapter(…)
,而是说
respondMessageListView.setAdapter(新MyAdapter(…)
)。在另一个名为MyAdapter.java的文件中,您将创建ArrayAdapter子类。