更改 android 列表视图中指定项目的颜色 [英] Change the color of a specified item in a listview for android

查看:43
本文介绍了更改 android 列表视图中指定项目的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想更改列表视图中一项的文本颜色.

I would like to change the text color of only one item in a listview.

此更改将由正在运行的异步任务的结果触发.

This change will be triggered by the result of a running asynctask.

到目前为止,我在谷歌上搜索,我发现的只是覆盖适配器的 getView() 函数,但是这种方法有点困难,因为我需要将要着色的行的 id 保留在全局中getView() 将访问的变量.

So far I searched on google and all I found was to overwrite the getView() function of the adapter, but this approach is kind of hard since I would need to keep the id of the rows I want to color in a global variable that will be accessed by getView().

是否有另一种方法可以在事件发生时简单地从列表视图中设置项目的文本颜色?

Is there another way to simply set the text color of an item from a listview when an event happens ?

编辑

我以这种方式创建列表视图:

I create the listview this way:

myListView = (ListView) findViewById(R.id.listView);
listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow);
listAdapter.add("test");
myListView.setAdapter(listAdapter);

推荐答案

要为列表项设置颜色,您肯定需要重写 AdaptergetView() 方法代码>.这里有一个小例子,在不使用项目的 id 的情况下更新列表项目的颜色.

For setting a color for a list item definitely you need to override the getView() method of the Adapter. Here is a small example for updating the color of the list item without using the id of the item.

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.simplerow) {
                @Override
                public View getView(int position, View convertView, ViewGroup parent) {
                    View view = super.getView(position, convertView, parent);
                    if (position % 2 == 0) {  //Place the condition where you want to change the item color.
                        view.setBackgroundColor(Color.GRAY);
                    } else {
                         //Setting to default color.
                        view.setBackgroundColor(Color.WHITE);
                    }
                    return view;
                }
            };

在上面的例子中,偶数位置的所有列表项都是GREY颜色,其他的都是WHITE颜色.如果不实现 getView(),我们就无法做到这一点.供参考 点击这里

In the above example, all the list item at even number positions will be in GREY color and others will be WHITE color. We cannot do this without implementing the getView(). For reference Click Here

这篇关于更改 android 列表视图中指定项目的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆