简体   繁体   English

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

[英]displaying flag image in listview from sqlite database

I have a database with a "flagname" field. 我有一个带有“标志名”字段的数据库。 What I want to do is display a listview with the relevant flag in the image view that corresponds with the database field, along side a text field. 我想做的是在与数据库字段相对应的图像视图中显示带有相关标志的列表视图,以及一个文本字段。 The text field from the database is being displayed ok, but I'm having trouble displaying the relevant flag beside it. 来自数据库的文本字段显示正常,但是我无法在其旁边显示相关标志。 Can someone please let me know where I'm going wrong. 有人可以让我知道我要去哪里了。 My code is listed below: 我的代码如下:

public class favourites extends Activity{ 公共课程的最爱扩展了活动{

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);



}

What you need to do is override the Adapter.setViewBinder(). 您需要做的是重写Adapter.setViewBinder()。 Try this: 尝试这个:

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

adapter.setViewBinder(new ViewBinder() {

public boolean setViewValue(View aView, Cursor aCursor, int aColumnIndex) {

String country=aCursor.getString(aColumnIndex);

int id = getResources().getIdentifier("yourpackagename:drawable/" + country , null, null);

iv = (ImageView)findViewById(R.id.imgFlag);

iv.setImageDrawable(getResources().getDrawable(id));

TextView tv=(TextView) aView;
tv.setText(aCursor.getString(aColumnIndex));
return true;

}
});
lv.setAdapter(adapter);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM