如何在listview中为所选项目设置颜色,同时不应选择其他项目(在android中)?

如何在listview中为所选项目设置颜色,同时不应选择其他项目(在android中)?,android,listview,Android,Listview,如何在listview中为所选项目设置颜色,同时不应选择其他项目(在android中) 提前感谢,, Neha在listview上注册onItemClickListener(),然后获取视图并按如下方式设置colot listview .setOnItemClickListener(new AdapterView.OnItemClickListener() { public void onItemClick(

如何在listview中为所选项目设置颜色,同时不应选择其他项目(在android中)

提前感谢,,
Neha

在listview上注册
onItemClickListener()
,然后获取视图并按如下方式设置colot

listview
                    .setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        public void onItemClick(AdapterView parent, View view,
                                int position, long id) {
view.setBackgroundColor(color);
}

@罗希特:我试过这种方法,但它并不完全有效。当我从listview中选择一行时,它会改变列表中其他行的颜色。我不知道为什么会发生这种情况。你能帮帮我吗?谢谢,NehaNow我很确定问题出在适配器的
getView()
方法中。您似乎没有管理适配器的
getView()
View
实例。您应该检查getView()中的视图实例是否为null。请参考这个链接@Rohit:我正在发送我的代码。你能告诉我哪里出了问题吗?list2.setOnItemClickListener(new OnItemClickListener(){@Override public void onItemClick(AdapterView a,View v,int position,long id){String s2=((TextView)((LinearLayout)((RelativeLayout)a.getChildAt(position)).getChildAt(0)).getChildAt(0)).getText().toString()字符串ID2=((TextView)((LinearLayout)((RelativeLayout)a.getChildAt(position)).getChildAt(0)).getChildAt(1)).getText().toString();字符串类型=((TextView)((LinearLayout)((RelativeLayout)a.getChildAt(position)).getChildAt(2)).getText().toString=((TextView)((LinearLayout))((RelativeLayout)a.getChildAt(位置)).getChildAt(0)).getChildAt(3)).getText().toString();dbHelper db=new dbHelper(ActiveMenu.this);mylist=new ArrayList();map=new HashMap();