在网上找了一个例子,可是不知道怎么用, 各位高手麻烦帮忙看看这个例子怎么能跑通,大家看看下面的代码在onCreate 中怎么调用 在线等啊
来个能运行的例子 感激不尽啊import java.util.HashMap;
import java.util.List;import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SimpleAdapter;public class SpecialAdapter extends SimpleAdapter {
        private int[] colors = new int[] { 0x30FF0000, 0x300000FF };        public SpecialAdapter(Context context, List<HashMap<String, String>> items, int resource, String[] from, int[] to) {
                super(context, items, resource, from, to);
        }        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
          View view = super.getView(position, convertView, parent);
          int colorPos = position % colors.length;
          view.setBackgroundColor(colors[colorPos]);
          return view;
        }
}

解决方案 »

  1.   


    类似这样的做法:listView = (ListView) findViewById(R.id.listname);specialAdapter = new SpecialAdapter(this, items, R.layout.name, from, to);listView.setAdapter(specialAdapter );
      

  2.   

    楼主你好,我的做法是在 listadapter 的getView(int position, View convertView, ViewGroup parent)中进行如下判断:view.setBackgroundColor(Color.DKGRAY);
    if (position % 2 == 1) {
        view.setBackgroundColor(Color.BLACK);
    } 但是,当滚动了列表,背景就变的乱七八糟了,
    请问楼主是 怎么解决这个问题的呢,望给个思路,十分谢谢!
      

  3.   

    3楼的,你应该这样写吧
    view.setBackgroundColor(Color.DKGRAY);
    if (position % 2 == 1) {
       view.setBackgroundColor(Color.BLACK);
    }  
    else//这里要加个其他的条件,不然最后,颜色就都变成COLOR.BLACK
    {
    //
    view.setBackgroundColor(Color.White);}