简体   繁体   中英

Change listView row backgroundcolor based on item parsed from Json

Hi i fairly know programming but the issue here is that i am parsing data from json. Therefore my goal is to set the background color of each row based on the condition that when the item is either;

Dog or Cat or Elephant or Rhino or Lion their respective list view background color should be blue, red, yellow, green

       {
        "pets" : {
          "dog_id"   : 1,
          "dog_name" : "Dave",
          "cat_id"   : 2,
          "cat_name" : "Prudie"
            "elephant_id" : 3,
            "elephant_name" : "Poncho",
            "lion_id ": 4
            "lion_name" : "King"

        }
      }

Kindly help, I can parse this JSON but I want the listView to show different colors. So far I can change the whole background of the listView, text of each item but failed to do the row colors conditionally.

You have to

1. Create a custom adapter class
2. With custom adapter, you will create custom view for each row,
3. In getView method of adapter you can change background color as you wish, just like you did to listView.

As others have explained it is fairly simple. However, remember that when views are recycled, the background wont go back automatically to the default color. You have to set the background color to transparent or whatever color you want. To do this simple if else statements are enough. Its easy to forget this and hard to figure out why you are getting the wrong colors.

You need to create your custom adapter class.

Change getView method with something like:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view = super.getView(position, convertView, parent);
    TextView textView = (TextView) view.findViewById(R.id.textView);
    if(textView.getText().toString().equalsIgnoreCase("elephant"))  //condition to check its text
    {
        //set color to blue
    }
    else if(textView.getText().toString().equalsIgnoreCase("lion"))
    {
        //set color to brown
    }
    return view;
}

Hope it helps.

You have to use Custom Adapter

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // assumed pet_name holds your string to check
        // Then set color according to your requirement
        if(pet_nane.equalsIgnoreCase("dog"))
        {
            convertView.setBackgroundColor(android.R.color.black);
        }else if(pet_nane.equalsIgnoreCase("cat"))
        {
            convertView.setBackgroundColor(android.R.color.white);
        }
        else{
            convertView.setBackgroundColor(android.R.color.transparent);
        }
        return convertView;
    }

Thanks you all,

I got it its working fine, i used the id of the pets

@Override
    public View getView(int position, View convertView, ViewGroup parent) {

        Pets petId = getItem(position);

        if(petId.dog_id == 1)
        {
            convertView.setBackgroundColor(Color.BLUE);
        }else if(petId.cat_id == 2)
        {
            convertView.setBackgroundColor(Color.RED);
        }
        }else if(petId.elephant_id == 3)
        {
            convertView.setBackgroundColor(Color.YELLOW);
        }
        }else if(petId.lion_id == 4)
        {
            convertView.setBackgroundColor(Color.GREEN);
        }
        else{
            convertView.setBackgroundColor(Color.WHITE);
        }
        return convertView;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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