更改ListView中仅选定条目的背景颜色

时间:2011-10-07 07:59:05

标签: android listview android-listview listactivity listadapter

我有一个ListActivity,其中我显示了一些条目。选择一个条目后,我会显示另一个包含条目内容的活动。当我返回ListActivity时,我想,未读条目的颜色与读取条目的颜色不同。

我有一个存储条目的DataBase,当我选择一个DataBase字段时,COLUMN_READ会被更新。

我有自定义ListAdapter:

 public class CustomListAdapter extends SimpleCursorAdapter{

        private Context context;
        private int layout;

        public CustomListAdapter(Context context, int layout, Cursor c,
                String[] from, int[] to) {
            super(context, layout, c, from, to);
            this.context = context;
            this.layout = layout;
        }


        @Override
        public View newView(Context context, Cursor cursor, ViewGroup parent) {

            Cursor c = getCursor();
            final LayoutInflater inflater = LayoutInflater.from(context);
            View v = inflater.inflate(layout, parent, false);


            int titleCol = c.getColumnIndex(DataBaseSchema.EntrySchema.COLUMN_TITLE);
            int authorCol=c.getColumnIndex(DataBaseSchema.EntrySchema.COLUMN_AUTHOR_NAME);
            int readCol= c.getColumnIndex(DataBaseSchema.EntrySchema.COLUMN_READ);

            String title = c.getString(titleCol);
            String author = c.getString(authorCol);
            long read= c.getLong(readCol);

            TextView name_text = (TextView) v.findViewById(R.id.title);
            if (name_text != null) {
                name_text.setText(title);
            }

            TextView content_text = (TextView) v.findViewById(R.id.subtitle);
            if (content_text != null) {
                content_text.setText(author);
            }


            if (read!=0){
                name_text.setBackgroundColor(Color.GRAY);
            }

            return v;

        }

        @Override
        public void bindView(View v, Context context, Cursor c) {

            int titleCol = c.getColumnIndex(DataBaseSchema.EntrySchema.COLUMN_TITLE);
            int authorCol=c.getColumnIndex(DataBaseSchema.EntrySchema.COLUMN_AUTHOR_NAME);
            int readCol= c.getColumnIndex(DataBaseSchema.EntrySchema.COLUMN_READ);

            String title = c.getString(titleCol);
            String author = c.getString(authorCol);
            long read= c.getLong(readCol);

            TextView name_text = (TextView) v.findViewById(R.id.title);
            if (name_text != null) {
                name_text.setText(title);
            }

            TextView content_text = (TextView) v.findViewById(R.id.subtitle);
            if (content_text != null) {
                content_text.setText(author);
            }


            if (read!=0){
                name_text.setBackgroundColor(Color.GRAY);
            }
        }
   }

我所做的是:

 CustomListAdapter present = new CustomListAdapter(context,
                R.layout.atom_list_row, loadingCursor, new String[] {DataBaseSchema.EntrySchema.COLUMN_TITLE,DataBaseSchema.EntrySchema.COLUMN_AUTHOR_NAME, DataBaseSchema.EntrySchema.COLUMN_READ}, new int[]{R.id.title,R.id.subtitle,R.id.row_container});
        setListAdapter(present);

问题是突出显示的条目没有正确显示,我不明白为什么。事实上,背景颜色会对所选条目进行更改,但对于其他一些条目,它似乎是随机的,当我滚动其他条目时也会更改它们的颜色。 这是Android的bug吗?是否有一些解决方法,或者我遗漏了什么?

提前致谢..

EDIT 我找到了解决方案!!!! 我在代码末尾添加了v.refreshDrawableState();。 此代码有效:

 public class CustomListAdapter extends SimpleCursorAdapter{

        private Context context;
        private int layout;

        public CustomListAdapter(Context context, int layout, Cursor c,
                String[] from, int[] to) {
            super(context, layout, c, from, to);
            this.context = context;
            this.layout = layout;
        }

        @Override
        public View newView(Context context, Cursor cursor, ViewGroup parent) {

            Cursor c = getCursor();
            final LayoutInflater inflater = LayoutInflater.from(context);
            View v = inflater.inflate(layout, parent, false);


            int titleCol = c.getColumnIndex(DataBaseSchema.EntrySchema.COLUMN_TITLE);
            int authorCol=c.getColumnIndex(DataBaseSchema.EntrySchema.COLUMN_AUTHOR_NAME);
            int readCol= c.getColumnIndex(DataBaseSchema.EntrySchema.COLUMN_READ);

            String title = c.getString(titleCol);
            String author = c.getString(authorCol);
            long read= c.getLong(readCol);

            TextView name_text = (TextView) v.findViewById(R.id.title);
            if (name_text != null) {
                name_text.setText(title);
            }

            TextView content_text = (TextView) v.findViewById(R.id.subtitle);
            if (content_text != null) {
                content_text.setText(author);
            }


            if (read!=0){
                v.setBackgroundColor(Color.GRAY);
            }
            else v.setBackgroundColor(Color.BLACK);
            v.refreshDrawableState();
            return v;

        }

        @Override
        public void bindView(View v, Context context, Cursor c) {

            int titleCol = c.getColumnIndex(DataBaseSchema.EntrySchema.COLUMN_TITLE);
            int authorCol=c.getColumnIndex(DataBaseSchema.EntrySchema.COLUMN_AUTHOR_NAME);
            int readCol= c.getColumnIndex(DataBaseSchema.EntrySchema.COLUMN_READ);

            String title = c.getString(titleCol);
            String author = c.getString(authorCol);
            //long read= c.getLong(readCol);

            TextView name_text = (TextView) v.findViewById(R.id.title);
            if (name_text != null) {
                name_text.setText(title);
            }

            TextView content_text = (TextView) v.findViewById(R.id.subtitle);
            if (content_text != null) {
                content_text.setText(author);
            }

            //LinearLayout linear =(LinearLayout) v.findViewById(R.id.row_container);
            if (c.getLong(readCol)!=0){
                v.setBackgroundColor(Color.GRAY);
            }else v.setBackgroundColor(Color.BLACK);
            v.refreshDrawableState();
        }



   }

1 个答案:

答案 0 :(得分:2)

阅读本文,您可以有一个想法Colored item in listview