开发者

How to switch database string to drawable resource in Android?

I have this codesnippet that gives me the fetched KEY_COLOR from database, and puts it at R.id.text_color for attachment in xml..

KEY_COLOR equals atm "Red" and "Blue". I dont want "red" and "blue" i want images instead, a little dot.

private void fillData() {
    Cursor notesCursor = mDbHelper.fetchAllNotes();
    startManagingCursor(notesCursor);

    // Create an array to specify the fields we want to display in the list (only TITLE)

    String[] from = new String[]{NotesDbAdapter.KEY_TITLE, NotesDbAdapter.KEY_COLOR};

    // and an array of the fields we want to bind those fields to (in this case just text1)

    int[] to = new int[]{R.id.text1, R.id.text_color};

    // Now create a simple cursor adapter and set it to display
    SimpleCursorAdapter notes =
        new SimpleCursorAdap开发者_开发技巧ter(this,
                R.layout.notes_row, notesCursor, from, to);
    setListAdapter(notes);

}

I simply want to change the outcome of NotesDbAdapter.KEY_COLOR to R.drawable.red_dot or blue_dot... But i cant figure ot how...

Do you understand? :/


Use the same fillData that you have.

However, you need to have the URI to the drawables in the database with column name NotesDbAdapter.KEY_COLOR when inserting them into the database.

For the sample code below change image to your drawables, if its blue or red.

String nameToInsert = "android.resource://my.package.name/"+R.drawable.image;

Of course change my.package.name to the correct packagename.

Just let R.id.text_color be the id of the imageview and not a textview.


From the docs for SimpleCursorAdapter:

Binding occurs in two phases. First, if a SimpleCursorAdapter.ViewBinder is available, setViewValue(android.view.View, android.database.Cursor, int) is invoked. If the returned value is true, binding has occured. If the returned value is false and the view to bind is a TextView, setViewText(TextView, String) is invoked. If the returned value is false and the view to bind is an ImageView, setViewImage(ImageView, String) is invoked.

From the docs for SimpleCursorAdapter.setViewImage (ImageView v, String value):

By default, the value will be treated as an image resource. If the value cannot be used as an image resource, the value is used as an image Uri.

So basically if you make it so the KEY_COLOR column of your DB contains Uris to your images and replace R.id.text_color in your to array with the resource id of an ImageView, in theory your SimpleCursorAdapter will automatically fill the ImageView with the image resolved from the Uri.

Never tried it myself but it seems like how it should work.

See this example of how to use an image with a textview in list rows, Fancy ListViews Part One. It's written by Mark Murphy aka CommonsWare here on SO. It actually uses a different method to what I was suggesting with a view binder and uses the getView(...) method to set images and text for each row.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜