I need to make a ListView with alternate color.

code:

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

  if(position %2==0)
  {
      view.setBackgroundColor(Color.BLUE);
  }
  return view;
}

but I don't know how to use it in my Adapter:

SimpleAdapter adapter = new SimpleAdapter(this, propositions, android.R.layout.simple_list_item_2,
    new String[] {"Date", "Trajet"},
    new int[] { android.R.id.text1, 
    android.R.id.text2});
lvTrajets.setAdapter(adapter);

Can someone help me about how to do this please ?

有帮助吗?

解决方案

Override getView for the adapter

SimpleAdapter adapter = new SimpleAdapter(this, propositions, android.R.layout.simple_list_item_2,
new String[] {"Date", "Trajet"},
new int[] { android.R.id.text1, 
android.R.id.text2})
    {
        @Override
        public View getView (int position, View convertView, ViewGroup parent)
        {
            View v = super.getView(position, convertView, parent);
             if(position %2==0)
             {
                  v.setBackgroundColor(Color.BLUE);
             }
             else
             {
                  v.setBackgroundColor(Color.WHITE);
             }

            return v;
        }


    };

其他提示

For showing alternate colors in the ListView Implement your getView() method of Adapter in the following way

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

if(convertView==null){
//Initialize the convertView and all its view's
}
  if(position %2==0)
  {
      view.setBackgroundColor(Color.BLUE);
  }else{
      view.setBackgroundColor(Color.Red);
  }
  return view;
}
 call from your activity :
ListAdapteradapter = new ListAdapter(this, fillMaps, R.layout.listmain, from, to);

ListAdapter class:

public class ListAdapter  extends SimpleAdapter {

    public ListAdapter(Context context, List<? extends Map<String, ?>> data,int resource, String[] from, int[] to)
    {
        super(context, data, resource, from, to);

    }

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

     View view = super.getView(position, convertView, parent); 

        if (position % 2 == 1)
        {

             view.setBackgroundResource(R.color.blue); 


        } 
        else {
            view.setBackgroundResource(R.color.lavendar);
        }
        return view;  
        }



}

This is my get view. I change one image background, you just change parent layout color for particular position from your logic if(position %2==0) simple here. I only give you trick for this, not complete code

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

        View v = convertView;

        if (v == null) {
            v = mInflater.inflate(R.layout.devicesnameshow_item, null);
        }


        ImageView imgIcon = (ImageView) v.findViewById(R.id.deviceimage);

        Resources res = v.getResources();
        String mDrawableName = data[position].umt;
        int resID = res.getIdentifier(mDrawableName , "drawable", context.getPackageName());
        Drawable drawable = res.getDrawable(resID);
        imgIcon.setImageDrawable(drawable);




          if(position %2==0)
        {
            imgIcon.setBackgroundColor(Color.parseColor(colorcounter_array[colorcounter]));// u can give any color i just take it from my array 
        } else {
            imgIcon.setBackgroundColor(Color.parseColor(colorcounter_array[colorcounter]));// u can give any color i just take it from my array 
        }




        }
         return v;
    }
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top