在sqlite数据库的列表视图中显示标志图像

问题描述:

我有一个“标志名”字段的数据库。我想要做的是在图像视图中显示与数据库字段相对应的相关标志的列表视图,以及文本字段。数据库中的文本字段显示正常,但我无法在旁边显示相关标志。有人能让我知道我哪里出错了。我的代码如下:在sqlite数据库的列表视图中显示标志图像

公共类收藏延伸活动{

Cursor cursor; 
ListView lv; 
ImageView iv; 

protected void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.listmain); 
    iv = (ImageView)findViewById(R.id.imgFlag); 


    DBAdapter db = new DBAdapter(this); 
    db.open(); 

    lv = (ListView)findViewById(R.id.list); 

    //Get cursor for list items 
    cursor = db.getAllRadioStations(); 

    //assign fields to columns 
    String[] columns = new String[]{DBAdapter.KEY_STATION_NAME.toString()}; 
    int[] to = new int[]{R.id.txtFlag}; 

    startManagingCursor(cursor); 

    SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.flagchoice, cursor, columns, to); 

    lv.setAdapter(adapter); 
    lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE); 



} 
+0

从我看到的,你正在加载一列“DBAdapter.KEY_STATION_NAME”到一个单一的TextView“R.id.txtFlag”。你能澄清你在哪里存储你的旗帜,你如何加载它们?或者这是你需要做什么? – 2011-12-29 13:57:57

+0

我将我的标志存储在res \ drawable文件夹中。基本上我想要做的是将图像匹配到数据库中的文本标志字段,这意味着如果文本显示“巴西”,那么在列表视图中的图像视图应该是巴西国旗,或者如果文本显示“西班牙” ,listview中文本框旁边的图像应该是西班牙国旗。希望这可以澄清事情。 – user615099 2011-12-29 19:15:04

你需要做的是覆盖Adapter.setViewBinder()。试试这个:

+0

我试过了,文本部分工作,但由于某些原因,当我尝试按照您的代码引用图像时,出现“异常 - 资源未找到”错误。下面是代码:int id = getResources()。getIdentifier(“com.PecApps:drawable-hdpi/france_flag”,null,null); \t \t \t \t iv =(ImageView)findViewById(R.id.imgFlag); \t \t \t \t iv.setImageDrawable(getResources()。getDrawable(id));我在目录中使用了法国国旗来测试图像视图是否显示正确,并且最终出现错误。 – user615099 2011-12-30 16:05:10

+0

删除“-hdpi”(只是可绘制的/) – 2011-12-30 21:29:28

+0

我试过了,我仍然得到一个资源未找到错误:-( – user615099 2012-01-01 23:43:34