Android: how to change the background colors of listitems in a listview, managed by SimpleCursorAdapter

元气小坏坏 提交于 2019-12-08 09:06:44

问题


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.

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

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

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!!! :)


回答1:


Try this code sample

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

         };


来源:https://stackoverflow.com/questions/27931121/android-how-to-change-the-background-colors-of-listitems-in-a-listview-managed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!