0

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.

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.

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. Maybe i will get it then. Thanks again, Matija

  • Use custom Adapter for custom view changes. – Rohit Goswami May 14 '14 at 11:58
  • override getView() method of custom Adapter and give conditions there for background color – Hemant Patel May 14 '14 at 12:01
  • you can do that but for which textview. don't understand Opis Polazak .. – Raghunandan May 14 '14 at 12:22
  • it's on croatian :) Opis=description, polazak=bus departure, kreceza=diffrence between bus departure time and system time... – user3613833 May 14 '14 at 12:35
  • @user3613833 ok check my answer it should work. So if polazak is passed then change the color – Raghunandan May 14 '14 at 12:43
  • @Raghunandan ok I will try it later, i'm at college now. Thank you – user3613833 May 14 '14 at 12:56
  • @user3613833 did you try? working? – Raghunandan May 15 '14 at 17:33
  • @Raghunandan yes, i had some problem but now it's working fine. Example of problem: I set String value=tv3.getText().toString;/*tv3 is third column or third textview*/ if(value=="passed") { tv.setTextColor(Color.BLACK); tv2.setTextColor(Color.BLACK); tv3.setTextColor(Color.BLACK);} And result was that all text in listview was colored BLACK, not only where value=="passed"; And than i put else (set other color to all 3 tv) after if and that work just fine. I don't know why i need that else but whitout it result is wrong. Thank you for you help :) – user3613833 May 16 '14 at 01:18
  • 1. you need the else part. 2. use .equlas to compare strings both mentioned in the code posted in my post. if it helps do accept the same – Raghunandan May 16 '14 at 03:53

2 Answers2

0

You should use GridView instead of ListView. In this case you'll be able to customize any item you want.

Ivan Rylach
  • 249
  • 1
  • 8
0

Override getView of Adapter

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;
           }
       };
Raghunandan
  • 132,755
  • 26
  • 225
  • 256