Android的:如何改变listItems中的背景色在列表视图,通过SimpleCursorAdapter管理 [英] Android: how to change the background colors of listitems in a listview, managed by SimpleCursorAdapter

查看:153
本文介绍了Android的:如何改变listItems中的背景色在列表视图,通过SimpleCursorAdapter管理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我还挺新的Andr​​oid,我想知道,同时通过一个ListView迭代我怎样改变listItems中的背景颜色。我一直在寻找计算器4天,但我没有找到我的情况下,任何解决方案。

Hi I'm kinda new to Android and i want to know how i can change the background color of listitems while iterating through a listview. I was looking at stackoverflow for 4 days but i didnt find any solutions for my case.

所以这里是我的code:

so here is my code:

    public class ExercisesActivity extends ListActivity implements AdapterView.OnItemClickListener {

private static final String TAG="ExerciseActivity";
private static final int REQ_ADD_EXERCISE =1;
private static final int REQ_RENAME_EXERCISE=2;
private String exercise;
private  long itemId;
SimpleCursorAdapter adapter;
ListView lv;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_exercises);

    lv=getListView();
    lv.setOnItemClickListener(this);

    Cursor cursor = getContentResolver().query(Exercise.CONTENT_URI, new String[]{Exercise.Columns._ID, Exercise.Columns.EXERCISE_NAME, Exercise.Columns.DONE_LAST},
            "",null, Exercise.Columns.DONE_LAST);

    adapter = new SimpleCursorAdapter(this,android.R.layout.simple_list_item_2,cursor,
            new String[] { Exercise.Columns.EXERCISE_NAME,  Exercise.Columns.DONE_LAST},
            new int[]{     android.R.id.text1,              android.R.id.text2},1);

    this.setListAdapter(adapter);
}

@Override
protected void onResume() {
    this.setBackgroundColors();
    super.onResume();
}

和这里是我的setBackgroundColors梅索德:

and here is my setBackgroundColors methode:

public void setBackgroundColors(){
    View listItem;
    TextView tv;

    for (int i =0;i<lv.getCount();i++){

        //listItem=lv.getChildAt(i); --> doesnt work
        listItem=lv.getAdapter().getView(i,null,lv);
        tv = (TextView) listItem.findViewById(android.R.id.text2);
        String color =calculateColor(tv.getText().toString());
        Log.d(TAG,color);

        if(color.equals("green")){
            listItem.setBackgroundColor(getResources().getColor(R.color.green));
        }else if(color.equals("yellow")){
            listItem.setBackgroundColor(getResources().getColor(R.color.yellow));
        }else if (color.equals("red")){
            listItem.setBackgroundColor(getResources().getColor(R.color.red));
        }
    }
}

在code看起来对我好。 calculateColor返回一个有效的字符串值(绿色,黄色,红色)。香港专业教育学院在colors.xml创建的那些资源的颜色。没有错误,而调试,code正在运行,但listItems中不改变自己的主题色。也许做了主题颜色总是涵盖了编程设置颜色?我必须调整个什么东西?

The Code looks good for me. "calculateColor" returns a valid string value (green, yellow, red). Ive created those resource colors in colors.xml. There are no Errors while debugging, the code is running but the listitems dont change their theme color. Maybe does the Theme color always covers the programmatically set color? Do i have to adjust something in that way?

非常感谢您的帮助! :)

Many thanks for your help!!! :)

推荐答案

试试这个code样品

private int[] colors = new int[] { 0xAA1A4C80, 0xAA5F82A6 };

adapter = new SimpleCursorAdapter(this,android.R.layout.simple_list_item_2,cursor,
            new String[] { Exercise.Columns.EXERCISE_NAME,  Exercise.Columns.DONE_LAST},
            new int[]{     android.R.id.text1,              android.R.id.text2},1){
             @Override
                public View getView(int position, View convertView, ViewGroup parent) {
                    View view = super.getView(position, convertView, parent);
                    TextView text = (TextView) view.findViewById(android.R.id.text1);
                    text.setTextColor(Color.WHITE);
                    int colorPos = position % colors.length;    
                    text.setBackgroundColor(colors[colorPos]);

                    return view;
                }

         };

这篇关于Android的:如何改变listItems中的背景色在列表视图,通过SimpleCursorAdapter管理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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