2011-06-27 44 views
0

我現在有點卡住了。我知道類似的問題已經被問到,但我一直無法找到任何有幫助的東西。在SQLite數據庫的ListView中顯示圖像

我試圖讓我的android應用程序內部的數據庫中的圖像出現在ListView中。我有一個有141行的數據庫,每行都標記爲1,2或3.我需要一個不同的PNG圖像(保存在我的res/drawable文件夾中)根據1,2或3顯示。這是我的當前查詢。任何的建議都受歡迎。我意識到可能有更好的方式來顯示我需要的信息。

public void whosnext(View view) { 
    // || is the concatenation operation in SQLite 
    cursor = db.rawQuery("SELECT * FROM eventsv1 WHERE start_time > (DATETIME('now')) AND title LIKE ? ORDER BY date ASC, time DESC", 
        new String[]{"%" + searchText.getText().toString() + "%"}); 
    adapter = new SimpleCursorAdapter(
      this, 
      R.layout.artist_list_item, 
      cursor, 
      new String[] {"title", "time", "date", "title3", "style"}, 
      new int[] {R.id.title, R.id.time, R.id.date, R.id.title3, R.id.style}); 
    setListAdapter(adapter); 
} 

回答

1

我會寫的是定製的CursorAdapter

public class WhosNextAdapter extends CursorAdapter 
{ 
    public WhosNextAdapter(Context context, Cursor cursor, boolean autoRequery) { 
     super(context, cursor, autoRequery); 
    } 

    @Override 
    public void bindView(View view, Context context, Cursor cursor) { 
     //This is were you would check your cursor for style (I think that is 1,2,3 your column) 
     int style = cursor.getInt(cursor.getColumnIndex("style")); 

     ImageView img = (ImageView) view.findViewById(R.id.yourImageViewId); 

     switch (style) { 
      case 1: 
       img.setImageResource(R.drawable.yourImageForStyle1); 

      break; 
      case 2: 
       img.setImageResource(R.drawable.yourImageForStyle2); 

      break; 

//etc. 
     } 
    } 

    @Override 
    public View newView(Context context, Cursor cursor, ViewGroup parent) { 
     //Inflate layout R.layout.artist_list_item 

     //Call bindView passing inflated layout, context, and cursor 

     //return layout 
    } 
} 
+0

我添加的新WhosNextAdapter,現在我的列表中查看文本得到了調整,以環繞在圖像應該的,但我的形象沒有顯示。有沒有需要把我的主要活動,或者我需要告訴系統,它是一個PNG或什麼文件夾來繪製圖像?並感謝您的幫助。 – KSol

0

你一定要延長SimpleCursorAdapter,如果你的項目佈局是很簡單的,你需要做的唯一事情是重寫setViewImage()方法下,你根本從數據庫轉換提供的值並在ImageView對象上調用setImageResource()方法。那麼你將不必交替太多附加的代碼。

public class MySimpleCursorAdpater extends SimpleCursorAdapter { 

    public MySimpleCursorAdpater(Context context, int layout, Cursor c, 
      String[] from, int[] to) { 
     super(context, layout, c, from, to); 
    } 

    @Override 
    public void setViewImage(ImageView v, String value) { 
     /*your code to convert value from database to drawable resource id*/ 
     v.setImageResource(resourceId); 
    } 

} 
+0

我仍然有問題。我如何將ImageView調用到我最初發布的代碼中使用的SimpleCusorAdapter中。這是一個鏈接到我的完整的主要活動代碼的gdoc。 「_https://docs.google.com/document/d/140t6QKBjT14c4mEcRitCw4Wb8FbVg3FF6DxgGot8RKM/edit?hl = en_US&authkey = CI-W29MC」 – KSol

+0

我不確定這是否有幫助,它可能是我的問題的根源。這裏是我用來訪問我的SQLite數據庫的DatabaseHelper。 「_https://docs.google.com/document/d/1LWmx1vLI2NljDNjVq5nwNpdnFAieY1aHfkiuWf-RqL0/edit?hl = en_US&authkey = CIWV08wG」 – KSol

+0

您是否可以修復鏈接到您的活動代碼?,您是否確定您的數據庫表有名爲' _id'是由適配器使用的嗎?你有什麼樣的問題?你可以發佈你的logcat輸出嗎? – wjeshak

相關問題