1

我需要用备用颜色制作一个 ListView。

代码:

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

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

但我不知道如何在我的适配器中使用它:

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

有人可以帮助我如何做到这一点吗?

4

4 回答 4

2

覆盖getView适配器

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


    };
于 2014-04-08T11:41:28.707 回答
0

这是我的看法。我更改了一个图像背景,您只需从if(position %2==0)此处简单的逻辑更改特定位置的父布局颜色。我只给你这个技巧,而不是完整的代码

@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;
    }
于 2014-04-08T11:41:21.490 回答
0

用于在以下方式中ListView实现您的getView()方法中 显示替代颜色Adapter

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;
}
于 2014-04-08T11:43:33.300 回答
0
 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;  
        }



}
于 2014-04-08T11:44:45.463 回答