如何在ListView中突出显示某些行项?

时间:2015-03-13 10:44:29

标签: android sqlite listview simpleadapter

我正在使用SQLite数据库中的数据填充ListView,然后使用SimpleAdapter来显示它。每行的一个字段是String,它指的是颜色。我想根据String设置每行的背景颜色。我怎么能这样做?

编辑:我添加了用于将数据附加到ListView的代码:

listView_commentaires = (ListView) findViewById(R.id.liste_favoris);
        daofavoris = new DAOFavoris(FavorisActivity.this);
        daofavoris.open();
        List<Commentaire> response = new ArrayList<Commentaire>();
        response = daofavoris.getAllFavoris();
        try {
            favoris = new ArrayList<HashMap<String, Object>>();
            for (Commentaire commentaire : response) {
                HashMap<String, Object> element;
                element = new HashMap<String, Object>()
                byte[] miniature = commentaire.decodeMiniature();
                Bitmap bitmap = BitmapFactory.decodeByteArray(miniature, 0, miniature.length);
                element.put("BITMAP", bitmap);
                element.put("CONTENT", commentaire.getContent());
                element.put("AUTHOR", commentaire.getAuthor());
                element.put("DATE", Constantes.getRealDate(commentaire.getDate()));
                favoris.add(element);
            }
            simpleAdapter = new SimpleAdapter(FavorisActivity.this,
                    favoris, R.layout.favoris_liste, new String[] {
                            "BITMAP", "CONTENT", "AUTHOR", "DATE" },
                    new int[] { R.id.media_favoris,
                            R.id.favoris_content,
                            R.id.favoris_author,
                            R.id.favoris_timestamp });
            simpleAdapter.setViewBinder(new SimpleAdapter.ViewBinder() {
                @Override
                public boolean setViewValue(View view, Object data,
                        String textRepresentation) {
                    if ((view instanceof ImageView)
                            & (data instanceof Bitmap)) {
                        ImageView iv = (ImageView) view;
                        Bitmap bm = (Bitmap) data;
                        iv.setImageBitmap(bm);
                        return true;
                    }
                    return false;
                }
            });
            listView_commentaires.setAdapter(simpleAdapter);

2 个答案:

答案 0 :(得分:0)

而不是使用SimpleAdapter,您可以像这样使用SimpleCursorAdapter

  SimpleCursorAdapter adapter =
    new SimpleCursorAdapter(
      getActivity(),
      R.layout.yourLayout,
      yourCursor,
      new String[] {
        YourColumnName
      },
      new int[]{
        R.id.your_root_layout_element
      }, 0
    );

  adapter.setViewBinder(
    new SimpleCursorAdapter.ViewBinder() {
      @Override
      public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
        LinearLayout rootLayout = (LinearLayout) view;
        setBackgroundColor(
          Color.parseColor(
            cursor.getString(0)));
        return true;
      }

    });

其中:

  • R.layout.yourLayout是您用于ListView的每一行的布局资源;
  • yourCursor是包含从数据库收到的数据的Cursor;
  • yourColumnName是您拥有颜色的列名称的String表示形式;
  • R.id.your_root_layout_element是分配给布局根元素的ID。在这个例子中,我假设它是LinearLayout;
  • Color.parseColor(cursor.getString(0))根据从数据库中获取的String生成颜色标识符。

之后,您必须将此SimpleCursorAdapter分配给ListView,如下所示:listView.setAdapter(adapter);

我希望这对你有所帮助。

答案 1 :(得分:0)

在适配器类内部放置此代码:

convertView.setBackgroundColor(mContext.getResources().getColor(R.color.message_selector_holo_blue));

根据条件改变行的颜色