开发者

Change the background color of only selected entries in a ListView

I have a ListActivity in which I show some entries. Once an entry is selected, I show another Activity with the content of the entry. I'd like, when I return back to the ListActivity, that the unread entries had a different color than the read ones.

I have a DataBase that stores the entries and when I select one the DataBase field COLUMN_READ is updated.

I have a custom 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_te开发者_运维技巧xt.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);
            }
        }
   }

And what I do is:

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

The problem is that the highlighted entries are not correctly shown, and I don't understand why. The background color in fact changes for the selected entry, but also for some other, it seems randomly, and also when I scroll other entries change their color. Is it an Android bug? Are there some workaround, or I am missing something?

Thanks in advance..

EDIT I've found a solution!!!! I've added v.refreshDrawableState(); at the end of my code. This code works:

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



   }


read this, you can have an idea Colored item in listview

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜