Search code examples
androidlistviewlistadapter

Dynamically changing the background of ListView child


I read many other posts, but most, if not all of them tell you to override one of the methods and put change the background then. I want to change it outside of the ListView Adapter class, so I tried doing this. It doesn't seem to work. I do however get the object, nut it wont change its background.

RelativeLayout item;
        item = (RelativeLayout) listView.getAdapter().getView(0, null, listView);
        item.setBackgroundColor(Color.YELLOW);

Thank you.


Solution

  • Find the nearest time save it in a variable and call adapter.notifyDataSetChanged()

    public View getView(int position, View convertView, ViewGroup parent){
      if(time is nerest time) {
        convertView. setBackgroundColor(Color. YELLOW)
      } else {
        convertView. setBackgroundColor("default color")
      }
    }