从SQLite数据库列表视图中显示标志图像视图、图像、标志、数据库

由网友(被偏爱的都有恃无恐)分享简介:我有一个flagname字段的数据库。我想要做的是显示相关标志,与数据库中的字段所对应的图像视图列表视图,沿一侧的文本字段。从数据库中的文本字段显示不错,但我无法显示相关标志旁边。是否有人可以让我知道我要去哪里错了。我的code为如下:公共类的收藏夹扩展活动{光标指针;ListView的LV;ImageView的I...

我有一个flagname字段的数据库。我想要做的是显示相关标志,与数据库中的字段所对应的图像视图列表视图,沿一侧的文本字段。从数据库中的文本字段显示不错,但我无法显示相关标志旁边。是否有人可以让我知道我要去哪里错了。我的code为如下:

公共类的收藏夹扩展活动{

 光标指针;
ListView的LV;
ImageView的IV;

保护无效的onCreate(包savedInstanceState){

    super.onCreate(savedInstanceState);
    的setContentView(R.layout.listmain);
    IV =(ImageView的)findViewById(R.id.imgFlag);


    DBAdapter DB =新DBAdapter(本);
    db.open();

    LV =(ListView控件)findViewById(R.id.list);

    //获取光标列表项
    光标= db.getAllRadioStations();

    //指定字段列
    的String []列=新的String [] {DBAdapter.KEY_STATION_NAME.toString()};
    INT []到=新INT [] {R.id.txtFlag};

    startManagingCursor(光标);

    SimpleCursorAdapter适配器=新SimpleCursorAdapter(这一点,R.layout.flagchoice,光标,列,到);

    lv.setAdapter(适配器);
    lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);



}
 

解决方案

您需要做的是覆盖Adapter.setViewBinder()。试试这个:

  SimpleCursorAdapter适配器=新SimpleCursorAdapter(这一点,R.layout.flagchoice,光标,列,到);

adapter.setViewBinder(新ViewBinder(){

公共布尔setViewValue(查看诚挚的范例,光标aCursor,诠释aColumnIndex){

字符串全国= aCursor.getString(aColumnIndex);

INT ID = getResources()则getIdentifier(yourpackagename:绘制/+国家,NULL,NULL);。

IV =(ImageView的)findViewById(R.id.imgFlag);

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

TextView的电视=(TextView中)诚挚的范例;
tv.setText(aCursor.getString(aColumnIndex));
返回true;

}
});
lv.setAdapter(适配器);
 
个人博客数据库的连接 表的建立 注册应用

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

阅读全文

相关推荐

最新文章