每个列表视图行设置不同的颜色 [英] set different color on each listview row

查看:150
本文介绍了每个列表视图行设置不同的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到了相关的答案,我不知道ehich一个是适合我。 我有一个ListView和每行有一个TextView。我要给予一定的条件下,每行得到不同的颜色(想象一下,我是从数据库中获取数据,并给予我得到的价值,我想文字设置为不同的颜色)我的code如下所示:

I have seen related answers and I do not know ehich one is the appropriate for me. I have a listView and each row has a textview. I want given some conditions, each row to get different color.(Imagine that I am getting data from a DB, and given the value I get, i want text set to different color) My code is shown below:

public class TrailsConditionScreen extends ListActivity {
 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        seeTrails();
    }
 private void seeTrails() {
    String[] help=new String[] {"foo","bar"};
    ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,R.layout.row,R.id.text1,help);
    setListAdapter(adapter);
 }

} 这code到目前为止只是它们打印就行了。

} This code so far just prints them on the list.

现在我想从数据库中检索值时,给我得到设置不同的颜色各行的值。可想而知,我的codeS从表中读取的第一线,给设置相应的颜色上的列表视图的第一行中的数字时,检索​​数据的我想要的。然后进入第二个孤独的分贝,为列表等第二行设置的颜色..我已经成功地实现从数据库读取,但我不知道如何设置颜色的特定行。在伪code,它看起来是这样的。

Now I want when retrieving values from the db, given the value i get set a different color on each row. imagine that my codes retrieves data from a table an I want when reading the first line , given the number set the appropriate color on the first line of the listview. Then go to second lone of the db,set color for the second line of the list and so on.. i have successfully implemented the reading from the db but i do not know how to set color on specific row. In pseudo-code it looks like this.

 for i=0 to number of lines in db{
   read_data();
   if key_value=0 then color_of_line(i)=red;
  else color_of_line_i)=green;
}

这任何帮助吗?

Any help for this?

推荐答案

您必须创建一个自定义的适配器来处理,而不是使用一个ArrayAdapter这一点。扩展一个ArrayAdapter并重写 getView 方式。而在 getView 基于状态的方法,你可以改变文本的颜色在您的 textView.setTextColor

You must create a custom adapter to handle this instead of using the ArrayAdapter. Extend the ArrayAdapter and override the getView method. And inside the getView method based on the condition you can change the color of the text on your textView.setTextColor.

要编写自定义适配器这里查看教程(6教程:实现自己的适配器) 。这个例子不使用支架模式,但你应该。

To write a custom adapter check the tutorial here (6. Tutorial: Implementing your own adapter). This example doesn't use the holder pattern but you should.

这篇关于每个列表视图行设置不同的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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