1

私はグーグルとstackoverflowを介して改訂しました。

Android-2つの異なる色のListView
この投稿は背景色の変更に役立ちます。しかし、ロジックに従って背景色を変更したい場合はどうすればよいですか?

たとえば、私のコードはすでに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; 
    }  

誰かが修正する方法を知っていますか?ありがとう!

4

3 に答える 3

1
convertView.setBackgroundColor((restaurant.get(RestaurantList.TAG_NAME).equals("MacDonal") && restaurant.get(RestaurantList.TAG_AREA).equals("newYork")) ? Color.GREEN : Color.WHITE);

ここで使用されている三項演算子を理解しているのかわかりません。説明が必要な場合は教えてください

于 2013-03-22T04:36:50.017 に答える
1

カテゴリのようなものを使用します。例: あるカテゴリを 1 つの色として、別のカテゴリを 1 つの色として定義する場合、 と を定義int category1 = 1category2 = 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);
}

これがお役に立てば幸いです。

于 2013-03-22T04:38:52.607 に答える
0

これは、必要に応じてアダプタ 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; 
    }
于 2013-03-22T05:17:50.750 に答える