【问题标题】:Change listView row backgroundcolor based on item parsed from Json根据从 Json 解析的项目更改 listView 行背景颜色
【发布时间】:2014-11-14 07:02:43
【问题描述】:

您好,我相当了解编程,但这里的问题是我正在解析来自 json 的数据。因此,我的目标是根据项目为任一情况设置每一行的背景颜色;

Dog、Cat、Elephant、Rhino 或 Lion,它们各自的列表视图背景颜色应为 蓝、红、黄、绿

       {
        "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 的整个背景,每个项目的文本,但未能有条件地执行行颜色。

【问题讨论】:

    标签: java android json listview getview


    【解决方案1】:

    你必须

    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.
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

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

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

        希望对你有帮助。

        【讨论】:

          【解决方案4】:

          你必须使用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;
              }
          

          【讨论】:

            【解决方案5】:

            谢谢大家,

            我知道它工作正常,我使用了宠物的 id

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

            【讨论】:

              猜你喜欢
              • 2012-03-10
              • 1970-01-01
              • 2023-03-18
              • 1970-01-01
              • 2019-02-08
              • 2016-03-13
              • 2011-01-14
              • 2013-06-03
              • 1970-01-01
              相关资源
              最近更新 更多