单个列表视图项目颜色更改

时间:2014-05-13 20:42:08

标签: android listview android-listview colors

我有一个listview有3列(时间表)。 首先是公交车离开车站的时间,第二个是公交线路的描述,第三个是公交车离开车站的时间和系统时间之间的差异。

所以我想改变传递的物品(公交线路)的颜色。我知道listview有一个方法setBackgroundColor,但它仅适用于所有项目。

是否有指定项目的方法?或者我必须更改适配器设置?

谢谢,

Matija

编辑: 对我来说太复杂了:(我在3周前开始使用android。任何人都可以尝试在此代码中实现Bhaskar答案,所以也许我会得到它。

             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();

    }
   list.setSelection(pozicija);

    /********************************************************/
}

再次感谢, Matija

1 个答案:

答案 0 :(得分:0)

您必须在Adapter getView()方法中执行此操作...

MyAdapter extends SimpleAdapter {
private ArrayList<Integer> coloredItems = new ArrayList<Integer>();

public MyAdapter(...) {
    super(...);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View v = super.getView(position, convertView, parent);

   //Here you can add your method/code to change the color.
    return v;
}

}

如果要将颜色设置为列,也可以在listview项目xml中更改颜色。

相关问题