2011-06-27 18 views
0

私はちょっと渋滞しています。私は同様の質問がされていることを知っているが、私は助けて何かを見つけることができませんでした。SQLiteデータベースからListViewに画像を表示

私は、アンドロイドアプリケーション内の内部データベースからListViewに表示されるイメージを取得しようとしています。私は141行のデータベースを持っており、各行は1,2,3のラベルが付けられています.1,2、または3に応じて表示するために、別のpngイメージ(res/drawableフォルダに保存)が必要です。クエリ。アドバイスは大歓迎です。私は必要な情報を表示するためのより良い方法があるかもしれないことを認識しています。

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

私は、あなたは間違いなくSimpleCursorAdapterを拡張する必要があり、あなたの項目のレイアウトが十分に単純であれば、あなたがする必要がある唯一のことは、単にあなたsetViewImage()メソッドをオーバーライドすることで、カスタマーの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を追加し、今、私のリストにテキストを表示画像があるべき場所を包み込むように調整しましたが、私の画像が表示されていません。私の主な活動に必要な電話がありますか、それとも画像を描くためにどのようなフォルダがあるのか​​をシステムに伝える必要がありますか?これまであなたの助けに感謝します。 – KSol

0

を記述します指定された値をデータベースから変換し、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

私はまだ問題があります。最初に投稿したコードで使用しているSimpleCusorAdapterにImageViewを呼び出すにはどうすればいいですか?ここに、私の完全な主な活動コードを持つgdocへのリンクがあります。 "_https://docs.google.com/document/d/140t6QKBjT14c4mEcRitCw4Wb8FbVg3FF6DxgGot8RKM/edit?hl = en_US&authkey = CI-W29MC" – KSol

+0

これは私の問題の根源になるかもしれません。ここで私は私のSQLite DBにアクセスするために使用しているDatabaseHelperです。 "_https://docs.google.com/document/d/1LWmx1vLI2NljDNjVq5nwNpdnFAieY1aHfkiuWf-RqL0/edit?hl = en_US&authkey = CIWV08wG" – KSol

+0

あなたのアクティビティコードへのリンクを修正できますか?あなたのデータベーステーブルには、アダプタで使用されている_id'ですか?どのような問題がありますか?あなたはlogcat出力を投稿できますか? – wjeshak

関連する問題