【问题标题】:ListView Background Color using logics使用逻辑的 ListView 背景颜色
【发布时间】:2013-03-22 04:26:18
【问题描述】:

我已经通过 google 和 stackoverflow 进行了修改..

Android - ListView with 2 different Colors
我发现这篇文章对更改背景颜色很有用.. 但是如果我想根据逻辑更改背景颜色怎么办?

例如,我的代码已经设置了一个 convertView。如下所示

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

        if(convertView==null) 
            convertView = inflater.inflate(R.layout.rest_list, null); 

        TextView id = (TextView)convertView.findViewById(R.id.id); // title 
        TextView name = (TextView)convertView.findViewById(R.id.name); // artist name 
        TextView area = (TextView)convertView.findViewById(R.id.area); // duration 
        TextView type = (TextView)convertView.findViewById(R.id.type); // duration 
        ImageView image=(ImageView)convertView.findViewById(R.id.image); // thumb image 

        HashMap<String, String> restaurant = new HashMap<String, String>(); 
        restaurant = data.get(position); 

        // Setting all values in listview 
        if(name != null){
        id.setText(restaurant.get(RestaurantList.TAG_ID)); 
        name.setText(restaurant.get(RestaurantList.TAG_NAME)); 
        area.setText(restaurant.get(RestaurantList.TAG_AREA));
        type.setText(restaurant.get(RestaurantList.TAG_TYPE));
        imageLoader.DisplayImage(restaurant.get(RestaurantList.TAG_IMAGE), image); 
        }

    //this line is example.. 
        convertView.setBackgroundColor(position % 2 == 0 ? Color.WHITE : Color.GREEN);

    // i wanna do like this (if there is name MacDonal in NewYork , set the background to green. otherwise leave it white.

     // it gives me red underline at the 'logic operation' sentence. 
        convertView.setBackgroundColor(name == MacDonal && area == newYork ? Color.WHITE : Color.GREEN);
        return convertView; 
    }  

有人知道怎么解决吗?谢谢!

【问题讨论】:

    标签: android listview


    【解决方案1】:
    convertView.setBackgroundColor((restaurant.get(RestaurantList.TAG_NAME).equals("MacDonal") && restaurant.get(RestaurantList.TAG_AREA).equals("newYork")) ? Color.GREEN : Color.WHITE);
    

    我不知道你是否理解这里使用的三元运算符。如果您需要解释,请告诉我

    【讨论】:

    • 完美。此代码有效!非常感谢纳文!我会消化的。 =D
    【解决方案2】:

    使用类似类别的东西。例如:如果您将某个类别作为一种颜色,而将另一个类别作为一种颜色,则定义 int category1 = 1 和 category2 = 2。并覆盖 getview 方法。那么你的适配器可能是

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;
    if (v == null) {
    LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    v = vi.inflate(R.layout.list, null);
    }
    TextView text = (TextView) v.findViewById(R.id.text);
    if (category1 == 1)
        text.setTextColor(Color.GREEN);
    if (category2 == 2)
        text.setTextColor(Color.RED);
    return super.getView(position, v, parent);
    }
    

    我希望这对你有用。

    【讨论】:

      【解决方案3】:

      这可能会对您有所帮助,根据需要在您的适配器 setTextcolor,Backgroundcolor 中

       public View getView(int position, View view, ViewGroup parent) {
              ViewHolder holder;
              if(view == null)
              {           
                  view = inflater.inflate(R.layout.myfriends_item, null); 
                  holder =  new ViewHolder();
                  holder.performerImage = (ImageView)view.findViewById(R.id.mfitem_iv);
                  holder.performerStar = (ImageView)view.findViewById(R.id.star);
                  holder.performerName = (TextView)view.findViewById(R.id.mfitem_tv_name);
      
                  holder.performerAge = (TextView)view.findViewById(R.id.mfitem_tv_age);
                  holder.performerPoints = (TextView)view.findViewById(R.id.mfitem_tv_pts);
      
                  holder.performStatus = (TextView)view.findViewById(R.id.mfitem_tv_status);
      
                  holder.performLookingfor = (TextView)view.findViewById(R.id.mfitem_tv_lookingfor);
                  holder.txtid = (TextView)view.findViewById(R.id.txtid);     
                  view.setTag(holder);
              }
              else
              {
                  holder = (ViewHolder)view.getTag();
              }       
      
      
                 //here you can set color as per require
                  try
                  {                                                                                           
      
                      if(topPerformersSetGet.getPerformerDataSetGets().get(position).getOnline() == 1)
                      {
                          holder.performStatus.setText(HotSpotSettings.ONLINE);   
                          holder.performStatus.setTextColor(mContext.getResources().getColor(R.color.green));
                      }
                      else
                      {
                          holder.performStatus.setText(HotSpotSettings.OFFLINE);
                          holder.performStatus.setTextColor(mContext.getResources().getColor(R.color.clrred));
      
                      }           
      
      
                  }catch(Exception ex)
                  {
                      ex.printStackTrace();
                  }                       
      
                  return view;
          }
      
          class ViewHolder
          {
              ImageView performerImage;
              ImageView performerStar;
              ImageView perforonlinestatus;       
              TextView performerName;
              TextView performStatus;     
              TextView performerAge;
              TextView performLookingfor;
              TextView performerPoints;
              TextView txtid; 
          }
      

      【讨论】:

        猜你喜欢
        • 2023-03-10
        • 1970-01-01
        • 2019-11-02
        • 2023-03-06
        • 2012-09-29
        • 2011-10-13
        • 1970-01-01
        • 1970-01-01
        • 2011-05-09
        相关资源
        最近更新 更多