Pregunta

I have a listview with 3 columns(timeschedule). 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.

Is there some method which specifies items? Or i must change adapter settings?

Thank you,

Matija

EDIT: Too complex for me :( I started with android just 3 weeks ago. Can anyone try implement Bhaskar answer in this code, so maybe i will get it.

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

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

Thanks again, Matija

¿Fue útil?

Solución

You Have to do it in Adapter getView() method ...

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;
}

}

You can also change color in listview item xml, if you want to set color to the columns.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top