문제

I have a database with 5 columns, 1 column which is a TEXT with the name of a drawable that is /res/drawable folder.

    private void fillData() {

    mCursor = db2.getAllAchievements();
    startManagingCursor(mCursor);


    String[] from = new String[]{achHelper.ROW_NAME, achHelper.ROW_DESCRIPTION, achHelper.ROW_POINTS, achHelper.ROW_TROPHY};


    int[] to = new int[]{R.id.achTitle, R.id.achDescription, R.id.achPoints, R.id.trophy};

    SimpleCursorAdapter classes =
            new SimpleCursorAdapter(this, R.layout.ach_row, mCursor, from, to);
    setListAdapter(classes);
}

R.id.trophy is a ImageView, how can I set the background image based on the data that is being pulled from achHelper.ROW_TROPHY?

도움이 되었습니까?

해결책

the simpleCursorAdapter need Strings, so your StringArray "from" must get String objects from the column achHelper.ROW_TROPHY when you setup your database it has to look like this:

 private static final String TABLE_CREATE = "CREATE TABLE " here your other colums
                                            + ROW_TROPHY + " TEXT NOT NULL);";
 db.execSQL(TABLE_CREATE);

So when you make your entry into your Database you have to convert the ID of your TropyImage (whitch is Integer) R.drawable.yourTropyImage to a string:

ContentValues cv = new ContentValues();
cv.put( your other columns, your other input);
cv.put(ROW_TROPHY, Integer.toString(R.drawable.yourTrophyImage));

return db.insert(DATABASE_TABLE, null, cv);

Your String[] from, int[] to and simpleCursorAdapter seem to be correct. You just must have the right DataType and ID in the ROW_TROPY column.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top