Android设置列表视图,其中包含来自数据库字符串的drawable图像

时间:2013-05-23 19:13:24

标签: android

我希望用图像填充listview,可绘制的名称存储在sqlite中......

有我的代码:

 setContentView(R.layout.proba);
    ListView ekipe_list = (ListView) findViewById (R.id.list);      
    Cursor cursor = baza.query("ekipe", null, null, null, null, null,
            "_id" + " ASC");  
    SimpleCursorAdapter adapter = new SimpleCursorAdapter(
            this, 
            R.layout.ekipe_layout, 
            cursor, 
            new String[] {"ime_ekipe","image"}, 
            new int[] {R.id.ime_ekipe,R.id.image});
    ekipe_list.setAdapter(adapter);
    ekipe_list.setCacheColorHint(Color.TRANSPARENT);

我的数据库:

String sql = "CREATE TABLE IF NOT EXISTS ekipe (" +
                    "_id INTEGER PRIMARY KEY AUTOINCREMENT, " + 
                    "ime_ekipe TEXT, " +                                            
                    "image TEXT )";
    baza5.execSQL(sql);


    ContentValues values = new ContentValues();

    values.put("ime_ekipe", "Ukraine");             
    values.put("image", "ukraine"); //ukraine.png in my drawable folder
    baza5.insert("ekipe", null, values);

    values.put("ime_ekipe", "Great Britain");       
    values.put("image", "unitedkingdom"); //unitedkingdom.png in my drawable folder
    baza5.insert("ekipe", null, values);

my_layout:

<TextView
    android:id="@+id/ime_ekipe"
    android:layout_width="wrap_content"
    android:textColor="#0000ff"
   android:textSize="20dp"
    android:layout_height="wrap_content"/>  


<ImageView
            android:id="@+id/image"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:scaleType="fitCenter"
           />

这仅适用于文本字段,但不会在图像视图中显示图像...

1 个答案:

答案 0 :(得分:0)

使用Resources.getIdentifier()。

int resID = getResources().getIdentifier("org.anddev.android.testproject:drawable/bug", null, null);
// or
int resID = getResources().getIdentifier("bug", "drawable", "org.anddev.android.testproject");

请参阅http://www.anddev.org/viewtopic.php?p=17846