2011-12-28 99 views
0

我有一個「標誌名」字段的數據庫。我想要做的是在圖像視圖中顯示與數據庫字段相對應的相關標誌的列表視圖,以及文本字段。數據庫中的文本字段顯示正常,但我無法在旁邊顯示相關標誌。有人能讓我知道我哪裏出錯了。我的代碼如下:在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

回答

0

你需要做的是覆蓋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

相關問題