我如何在交替的列表视图的颜色? [英] How do I alternate colors in between Listviews?

查看:123
本文介绍了我如何在交替的列表视图的颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我添加简单的数据到一个ListView(名称和时间),我希望能够一个白色和灰色的背景每个表行之间交替。我目前在设置XML文件的背景颜色。

I am adding simple data into a listview (name and time) and I want to be able to alternate between a white and grey background for each table row. I am currently setting the background color in the xml file.

我使用的是code从的http://saigeethamn.blogspot.com/2010/04/custom-listview-android-developer.html

I am using the code from http://saigeethamn.blogspot.com/2010/04/custom-listview-android-developer.html

感谢

修改
我的code看起来像这样

EDIT my code looks like this

  //R.layout.custom_list_view is just a list view with grey background
   //(I want it white for every other one)
  //On Create section
  setContentView(R.layout.custom_list_view);

    SimpleAdapter adapter = new SimpleAdapter(
            this,
            list,
            R.layout.custom_row_view,
            new String[] {"pen","price"},
            new int[] {R.id.text1,R.id.text2}
            );
    //see function below
    populateList();
    setListAdapter(adapter);
}

static final ArrayList<HashMap<String,String>> list = 
    new ArrayList<HashMap<String,String>>(); 

private void populateList() {
    //for loop would go here
    HashMap<String,String> temp = new HashMap<String,String>();
    temp.put("pen","MONT Blanc");
    temp.put("price", "200.00$");
    list.add(temp);

    HashMap<String,String> temp2 = new HashMap<String,String>();
    temp2.put("pen","Parker");
    temp2.put("price", "400.00$");
    list.add(temp2);
   //theres no place to change the background of my listview

最后的编辑。下面是一个简单的解决方案<一href=\"http://ykyuen.word$p$pss.com/2010/03/15/android-%E2%80%93-applying-alternate-row-color-in-listview-with-simpleadapter/\" rel=\"nofollow\">http://ykyuen.word$p$pss.com/2010/03/15/android-%E2%80%93-applying-alternate-row-color-in-listview-with-simpleadapter/
更改SimpleAdapter适配器在网站上找到自定义SpecialAdapter。谢谢大家的帮助。

Final Edit. Here is an easy solution http://ykyuen.wordpress.com/2010/03/15/android-%E2%80%93-applying-alternate-row-color-in-listview-with-simpleadapter/ Change SimpleAdapter adapter to the custom SpecialAdapter found in the website. Thank you all for helping

推荐答案

您应该做的实现自定义适配器,并设置背景颜色有:

You should do implement custom adapter and set background color there:

public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        View convertViews;
        if (convertView == null) {
            convertViews = mInflater.inflate(R.layout.startingsquadlistview, null);
            holder = new ViewHolder();
            holder.text = (TextView) convertView.findViewById(R.id.textview);
            convertViews.setTag(holder);
        } else {
            convertViews = convertView;
            holder = (ViewHolder) convertViews.getTag();
        }

        if(position % 0) {
            convertView.setBackground(context.getResources().getColor(R.color.col1);
        else
            convertView.setBackground(context.getResources().getColor(R.color.col2);

        return convertViews;
    }

这篇关于我如何在交替的列表视图的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆