android - 如何使用 SimpleCursorAdapter 将数据库中的特定元素加粗

标签 android

我有一个主要 Activity ,它从数据库中获取元素并将它们显示在可点击的 ListView 中。我用这个方法来完成任务:

private void fillData() {
    // Get all of the notes from the database and create the item list
    Cursor c = RequestManager.getRequests(getApplicationContext());
    startManagingCursor(c);
    String[] from = new String[] { DataBase.KEY_TITLE, DataBase.KEY_BODY };
    int[] to = new int[] { R.id.text1, R.id.text2 };

    // Now create an array adapter and set it to display using our row
    SimpleCursorAdapter notes =
        new SimpleCursorAdapter(this, R.layout.notes_row, c, from, to);
    setListAdapter(notes);
}

我想知道,是否可以访问数据库内部的 bool 字段,并加粗具体元素是该字段被标记为未读?每个元素都在一个 TextView 中,然后放置在一个 ListView 中。 谢谢

编辑:使用建议扩展 CursorAdapter 类,但是当列表中的任何元素加粗时,第一个元素也加粗。一旦所有元素都标记为已读,第一个元素将恢复为未加粗。有什么想法吗?

    public void bindView(View view, Context context, Cursor cursor) {
    TextView textRequestNo = (TextView) view.findViewById(R.id.text1);
    TextView textMessage = (TextView) view.findViewById(R.id.text2);
    StringBuilder requestNo = new StringBuilder(cursor.getString(cursor
            .getColumnIndex("requestNo")));
    StringBuilder message = new StringBuilder(cursor.getString(cursor
            .getColumnIndex("Message")));

    textRequestNo.setText(requestNo);
    textMessage.setText(message);
    if (cursor.getString(cursor.getColumnIndex("Read")).equals("false"))
    {
        textRequestNo.setTypeface(null, Typeface.BOLD);
        textMessage.setTypeface(null, Typeface.BOLD);
    }   
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    final View view = mInflater.inflate(R.layout.notes_row, parent, false);
    bindView(view, context, cursor);
    return view;
}

最佳答案

不幸的是,我认为您必须实现自己的 CursorAdapter 才能实现此功能。不过还不错;你可以创建 ResourceCursorAdapter 的子类,然后您需要做的就是编写您自己的 bindView() 实现。在此实现中,您可以读取 Cursor 以确定是否应将行加粗。

关于android - 如何使用 SimpleCursorAdapter 将数据库中的特定元素加粗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3168335/

相关文章:

android - 为自托管博客 reshape Wordpress Android 应用程序

android - 从android中的downloadManager查找下载完成

android - Android Studio 1.2 Beta 3 中的 Traceview 在哪里?

Android 如何以编程方式将多个图像映射到多个图像按钮?

android - 透明状态栏android

android - 如何在 fragment 中再次初始化相机?

android - 如何更改导航 View 项目的文本大小?

android - 无法使用 Gitlab-CI 在 Docker 容器中运行 Android 应用程序

java - 通过 parcelable extra 检索时为空对象

javascript - 从网页调用号码