Java 根据从Json解析的项更改listView行背景颜色

Java 根据从Json解析的项更改listView行背景颜色,java,android,json,listview,getview,Java,Android,Json,Listview,Getview,嗨,我相当了解编程,但这里的问题是我正在解析来自json的数据。因此,我的目标是根据以下条件设置每行的背景色:当项目为 狗、猫、大象、犀牛或狮子它们各自的列表视图背景颜色应为 蓝色,红色,黄色,绿色 { "pets" : { "dog_id" : 1, "dog_name" : "Dave", "cat_id" : 2, "cat_name" : "Prudie"

嗨,我相当了解编程,但这里的问题是我正在解析来自json的数据。因此,我的目标是根据以下条件设置每行的背景色:当项目为

狗、猫、大象、犀牛或狮子它们各自的列表视图背景颜色应为 蓝色,红色,黄色,绿色

       {
        "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"

        }
      }
请帮忙,我可以解析这个JSON,但我希望listView显示不同的颜色。到目前为止,我可以更改listView的整个背景、每个项目的文本,但无法有条件地更改行颜色。

您必须

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.

您需要创建自定义适配器类

使用以下内容更改
getView
方法:

@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;
}
希望有帮助。

您必须使用


正如其他人所解释的,这相当简单。 但是,请记住,在回收视图时,背景不会自动恢复为默认颜色。您必须将背景色设置为透明或任何您想要的颜色。要做到这一点,简单的
if-else
语句就足够了。很容易忘记这一点,也很难弄明白为什么你会得到错误的颜色

谢谢大家

我知道了,工作正常,我用了宠物的身份证

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