tags:

views:

41

answers:

1

I have a main activity that takes elements from a database and displays them in a clickable listview. I use this method to accomplish the task:

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

I am wondering, is it possible to access a boolean field inside the database, and bold the specific element is the field is marked as unread? The elements are each in a textview, and are then placed in a listview. Thanks

Edit: Used the suggestion to extend the CursorAdapter Class, but when any element in the list is bolded, the first element is also bolded. Once all the elements are marked as read, the first element goes back to unbolded. Any ideas?

    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;
}
+2  A: 

Unfortunately I think you're going to have to implement your own CursorAdapter in order to achieve this functionality. It's not so bad though; you can make subclass of ResourceCursorAdapter, and then all you need to do is write your own implementation of bindView(). Inside of this implementation, you can read the Cursor to determine if you should bold the row or not.

Daniel Lew
Thanks, I'll update if I can get it working.
mbauer14
I got this for the most part, but I updated the main question with my problem: the first request is always bolded if any other request is. Do you have any experience with this? Thanks
mbauer14
The reason you're getting that is because of how ListView optimizes its displays. It reuses views as much as possible. I'm not exactly sure how to solve this problem for CursorAdapters, but basically you'll want to denote a few different types of rows, and only use the appropriate views for each type.
Daniel Lew