【问题标题】:Change the background color of only selected entries in a ListView更改 ListView 中仅选定条目的背景颜色
【发布时间】:2011-10-07 07:59:05
【问题描述】:

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

我有一个存储条目的数据库,当我选择一个时,数据库字段 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);

问题是突出显示的条目没有正确显示,我不明白为什么。实际上,所选条目的背景颜色会发生变化,但对于其他一些条目来说,它似乎是随机的,并且当我滚动其他条目时也会改变它们的颜色。 是安卓的bug吗?是否有一些解决方法,或者我遗漏了什么?

提前谢谢..

编辑 我找到了解决办法!!!! 我在代码末尾添加了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();
        }



   }

【问题讨论】:

  • 尝试在 newView 中使用 c.getLong(readCol)!=0,而不是 read!=0,我只想说,避免使用新变量。这就是导致问题的原因。
  • 它不会改变 ListView 的行为,我想补充一点,它看起来像背景颜色周期性变化(例如每 5 个条目)。

标签: android listview android-listview listactivity listadapter


【解决方案1】:

阅读本文,你会有一个想法Colored item in listview

【讨论】:

  • 我使用的是 CursorAdapter 而不是 SimpleAdapter,因为我需要处理一个数据库。本教程中的 getView 方法应该与我的代码中的 bindView 具有相同的功能。奇怪的是它不能正常工作。
  • 你能把你的主课贴出来吗,我认为它缺少 id 位置,所以结果是随机的
  • 在这里发布所有内容有点复杂抱歉。我认为我已经发布的内容是唯一需要的。
猜你喜欢
  • 1970-01-01
  • 2012-03-10
  • 1970-01-01
  • 1970-01-01
  • 2013-06-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多