Android数据库设置ImageView

发布于 2024-09-25 20:55:17 字数 628 浏览 1 评论 0原文

我有一个包含 5 列的数据库,其中 1 列是一个文本,其名称为 /res/drawable 文件夹。

    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.tropy 是一个 ImageView,如何根据从 achHelper.ROW_TROPHY 提取的数据设置背景图像?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

弱骨蛰伏 2024-10-02 20:55:17

simpleCursorAdapter 需要字符串,因此当您设置数据库时,您的 StringArray“from”必须从列 achHelper.ROW_TROPHY 获取 String 对象,它必须如下所示:

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

因此,当您进入数据库时​​,您必须将 TropyImage 的 ID(整数)R.drawable.yourTropyImage 转换为字符串:

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

您的 String[] from、int[] tosimpleCursorAdapter 似乎是正确的。您只需在 ROW_TROPY 列中提供正确的数据类型和 ID 即可。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文