简体   繁体   English

Listview项目颜色更改

[英]Listview item color change

Sorry for asking simmilar question again. 抱歉再次询问类似的问题。 Answer was to complex for me because i started with android 3 weeks ago and i don't know why i can't reply and past code in question. 对于我来说,答案很复杂,因为我三周前开始使用android,而且我不知道为什么我无法回复以及过去的代码有问题。

I have a listview with 3 columns(timeschedule). 我有一个3列(时间表)的列表视图。 First is time when bus leaves station, second is description of bus line and third is difference between the time when the bus leaves the station and the system time. 第一个是公共汽车离开车站的时间,第二个是公共汽车线路的描述,第三个是公共汽车离开车站的时间与系统时间之间的差。

So I want to change the color of items (bus lines) which are passed. 所以我想更改通过的项目(总线)的颜色。 I know that listview has a method setBackgroundColor, but it works only for all items. 我知道listview有一个setBackgroundColor方法,但是它仅适用于所有项目。

And this is my code: 这是我的代码:

        mylist = new ArrayList<HashMap<String, String>>();
       mylistglava = new ArrayList<HashMap<String, String>>();

/********** Display the headings ************/
map1 = new HashMap<String, String>();
map1.put("Polazak", "Polazak:");
map1.put("Opis", "Napomena:");
map1.put("Kreceza", "Krece za:");
mylistglava.add(map1);

try {
    adapterglava = new SimpleAdapter(this, mylistglava,
            R.layout.vrijemebus, new String[] { "Polazak", "Opis",
                    "Kreceza" }, new int[] { R.id.Polazak, R.id.Opis,
                    R.id.Kreceza });
    list_glava.setAdapter(adapterglava);
} catch (Exception e) {
    e.printStackTrace();

}

/********************************************************/

/********** Display the contents ************/

for (int i = 0; i < bus.vrijeme.length; i++) {
    map2 = new HashMap<String, String>();
    map2.put("Polazak", bus.vrijeme[i]);
    map2.put("Krece za", kreceza[i]);
    if (i < bus.opis.length)
        map2.put("Opis", bus.opis[i]);
    else
        map2.put("Opis", " ");
    mylist.add(map2);
}

try {
    adapter = new SimpleAdapter(this, mylist, R.layout.vrijemebus,
            new String[] { "Polazak", "Opis", "Krece za" }, new int[] {
                    R.id.Polazak, R.id.Opis, R.id.Kreceza });
  list.setAdapter(adapter);
} catch (Exception e) {
    e.printStackTrace();

}

} }

So can anyone implement Adapter getview() and describe me how to set this on my previous code. 因此,任何人都可以实现Adapter getview()并描述我如何在之前的代码中进行设置。 Maybe i will get it then. 也许我会明白的。 Thanks again, Matija 再次感谢,Matija

You should use GridView instead of ListView . 您应该使用GridView而不是ListView In this case you'll be able to customize any item you want. 在这种情况下,您将可以自定义所需的任何项目。

Override getView of Adapter 覆盖适配器的getView

adapter = new SimpleAdapter(this, mylist, R.layout.vrijemebus,
        new String[] { "Polazak", "Opis", "Krece za" }, new int[] {
                R.id.Polazak, R.id.Opis, R.id.Kreceza })
       {
           @Override
           public View getView (int position, View convertView, ViewGroup parent)
           {
               View v = super.getView(position, convertView, parent);
               TextView tv = (TextView) v.findViewById(R.id.Polazak);
               // based on condition. set color to  the required textview
               String value = tv.getText().toString();
               if(value.equals("passed"))
               {
               tv.setTextColor(Color.RED);
               }else
               {
               tv.setTextColor(Color.BLACK);
               }

               return v;
           }
       };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM