【问题标题】:How to set color to the selected item in listview and at the same time the other items should not be selected (in android)?如何为列表视图中的选定项目设置颜色,同时不应选择其他项目(在android中)?
【发布时间】:2011-03-05 07:12:09
【问题描述】:

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

提前致谢, 尼哈

【问题讨论】:

    标签: android listview


    【解决方案1】:

    在您的列表视图上注册onItemClickListener() 并获取视图并像这样设置colot

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

    【讨论】:

    • @Rohit:我已经尝试过这种方式,但它不能完全正常工作。当我从列表视图中选择一行时使用它,它会改变列表中其他行的颜色。我不知道为什么会这样。你能帮帮我吗?谢谢,内哈
    • 现在我很确定问题出在适配器的getView() 方法中。您似乎没有管理适配器的getView()View 实例。您应该检查 getView() 中的 View 实例是否为空。请参考此链接android.amberfog.com/?p=296
    • @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(); String ID2 = ((TextView)((LinearLayout) ((RelativeLayout)a.getChildAt(position) ).getChildAt(0)).getChildAt(1)).getText().toString();
    • 字符串类型 = ((TextView)((LinearLayout) ((RelativeLayout)a.getChildAt(position)).getChildAt(0)).getChildAt(2)).getText().toString() ; String MenuItemID = ((TextView)((LinearLayout) ((RelativeLayout)a.getChildAt(position)).getChildAt(0)).getChildAt(3)).getText().toString(); dbHelper db = new dbHelper(ActiveMenu.this); mylist = new ArrayList>(); map = new HashMap();
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-13
    • 1970-01-01
    • 2011-08-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多