我在drawable文件夹中有图像,它们的名称存储在sqlite中如何根据那里的名称从drawable中显示这些图像? [复制]

Posted

技术标签:

【中文标题】我在drawable文件夹中有图像,它们的名称存储在sqlite中如何根据那里的名称从drawable中显示这些图像? [复制]【英文标题】:i have images in drawable folder and their names stored in sqlite how to display these images from drawable according to there names? [duplicate] 【发布时间】:2014-06-11 23:39:51 【问题描述】:

我有一个使用SQLite 来存储和检索数据的android 应用程序。

在我的项目中,我在可绘制文件夹中有图像,我将它们的名称存储在SQLite 数据库中,我想要的是根据它们在ListView 中的名称显示这些图像。

我知道我需要将字段名称添加到代码中,但是如何将图像链接到其名称?

这是我的代码:

MatchScheduleList.java

package com.devleb.expandablelistdemo3;

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.app.ListActivity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v4.widget.SimpleCursorAdapter;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class MatchScheduleList extends Activity 

    private static final String DB_NAME = "world_cup.db";
    // *****Tables name**************************//
    private static final String TABLE_NAME = "match_list";

    // *****Tbale name******************************//

    public static final String PLACE_ID = "_id";
    public static final String STAD_NAME = "stad_name";
    public static final String TEAM1 = "team1";
    public static final String TEAM2 = "team2";
    public static final String STAGE = "stage";
    public static final String MATCH_DATE = "match_date";
    public static final String GROUP = "group";

    public static final String[] ALL_KEYS = new String[]  PLACE_ID, STAD_NAME,
            TEAM1, TEAM2, STAGE, MATCH_DATE, GROUP ;
    // *****Tbale name******************************//

    private SQLiteDatabase database;
    private ListView listView;
    private ArrayList<String> matchList;

    private ExternalDbOpenHelper extDB;

    int[] img =  R.drawable.brazil_flag, R.drawable.croatian_flag,
            R.drawable.mexico_flag, R.drawable.cameroon_flag, R.drawable.spain,
            R.drawable.netherlands_flag, R.drawable.czech_republic_flag,
            R.drawable.australia, R.drawable.colombia_flag, R.drawable.gress,
            R.drawable.cote_divoire_flag, R.drawable.japan,
            R.drawable.uruguay_flag, R.drawable.costa_rica_flag,
            R.drawable.england_flag, R.drawable.italy_flag,
            R.drawable.switzerland, R.drawable.ecuador_flag,
            R.drawable.france_flag, R.drawable.honduras_flag,
            R.drawable.argentina_flag, R.drawable.bousna, R.drawable.iran_flag,
            R.drawable.nigeria_flag, R.drawable.germany_flag,
            R.drawable.portugal, R.drawable.ghana_flag,
            R.drawable.united_states_flag, R.drawable.belgium_flag,
            R.drawable.algeria_flag, R.drawable.russia_flag,
            R.drawable.korea_flag ;

    int nextImageIndex = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_match_schedule_list);

        ExternalDbOpenHelper extDB = new ExternalDbOpenHelper(this, DB_NAME);
        database = extDB.openDataBase();

        int imgid = img[nextImageIndex];
        nextImageIndex = (nextImageIndex + 1) % img.length;

        populateLitsFromDB();
    


    public Cursor getAllRows() 
        String where = null;

        Cursor c = database.query(true, TABLE_NAME, ALL_KEYS, where, null,
                null, null, null, null);
        if (c != null) 
            c.moveToFirst();

        

        return c;

    

    private void populateLitsFromDB() 
        // TODO Auto-generated method stub

        Cursor cursor = getAllRows();

        // allo0w activity to manage life cicle of the cursor

        startManagingCursor(cursor);

        // setup mapping from cursor to view fields
        String[] fromFieldNames = new String[]  MATCH_DATE, TEAM1, TEAM2,STAD_NAME, GROUP;
        int[] toViewFields = new int[]  R.id.txtDate, R.id.textName1, R.id.textName2, R.id.textLocation, R.id.textGroup;

        // create adapter to map columns of the DB INTO ELEMENT IN THE LIST
        SimpleCursorAdapter myCursorAdapter = new SimpleCursorAdapter(this,
                R.layout.row_list_match_schedule, cursor, fromFieldNames,
                toViewFields);

        Log.d("in the getAllRows", myCursorAdapter.toString());

        // set the adapter for te listView
        ListView myList = (ListView) findViewById(R.id.list);
        myList.setAdapter(myCursorAdapter);

    

    @Override
    public boolean onCreateOptionsMenu(Menu menu) 
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.match_schedule_list, menu);
        return true;
    


【问题讨论】:

***.com/a/9156893/726863 【参考方案1】:

试试这个:

String imgName = "ic_launcher"; // specify here your image name fetched from db
String uri = "drawable/" + imgName;
int icon = getResources().getIdentifier(uri, "drawable", getPackageName());
imageView.setImageResource(icon);

【讨论】:

在此解决方案中,如何根据我的代码将图像与其存储在 sqlite 中的名称链接起来 您在哪个字段中从 db 获取图像名称? 我将添加 2 个字段名称:FLAGS1、FLAGS2 这 2 个名称字段将包含在 SQLITE 数据库中的图像名称 您不会在populateLitsFromDB() 中获取它们。正确的 ?您将创建另一种方法来获取所有图像名称? 不,实际上我想在 populateListFromDB() 中获取图像是不是错了?因为我需要在 populateListFromDB() 方法中已经创建的同一个列表视图中显示图像

以上是关于我在drawable文件夹中有图像,它们的名称存储在sqlite中如何根据那里的名称从drawable中显示这些图像? [复制]的主要内容,如果未能解决你的问题,请参考以下文章

Android:无法将@drawable/picture 转换为drawable

获取存储在drawable中的图像的URI

我需要从存储在我的 res/drawables 中的图像中将图像设置到我的 ImageView

获取存储在drawable中的图像的URI

将存储在 resources/drawable 文件夹中的图像保存到图库中

如何根据sqlite数据库中的名称从drawable中获取图像,然后在列表视图中显示