Android SimpleCrsorAdapter如何显示图像?

Android SimpleCrsorAdapter如何显示图像?,android,imageview,simplecursoradapter,android-cursoradapter,Android,Imageview,Simplecursoradapter,Android Cursoradapter,我正在制作一个android菜谱应用程序,其中我使用了一个数据库。 在数据库中有一个名为“images”的列,我将配方图片的文件名存储在drawable文件夹中。 现在,我想列出一些食谱,其中包括: 1) 食谱的标题 2) 简短的描述 和 3) 食谱的图像 为此,我使用了一个SimpleCorsorAdaptor 我的问题是无法显示图像。 我想从“images”列中读取文件名,然后在我的imageview(R.id.imageview1)中设置图像 这是我到现在为止的代码: public cla

我正在制作一个android菜谱应用程序,其中我使用了一个数据库。 在数据库中有一个名为“images”的列,我将配方图片的文件名存储在drawable文件夹中。 现在,我想列出一些食谱,其中包括: 1) 食谱的标题 2) 简短的描述 和 3) 食谱的图像 为此,我使用了一个SimpleCorsorAdaptor

我的问题是无法显示图像。

我想从“images”列中读取文件名,然后在我的imageview(R.id.imageview1)中设置图像

这是我到现在为止的代码:

public class RecipesMainActivity extends Activity 
{

public static final String ROW_ID = "row_id"; //Intent extra key
private ListView esodaListView;  // the ListActivity's ListView
private SimpleCursorAdapter esodaAdapter; // adapter for ListView
DatabaseConnector databaseConnector = new DatabaseConnector(RecipesMainActivity.this);


// called when the activity is first created
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_recipes_main);
    esodaListView = (ListView)findViewById(R.id.esodaList);
    esodaListView.setOnItemClickListener(viewEsodaListener);

    databaseConnector.open();

    // map each esoda to a TextView in the ListView layout
    // The desired columns to be bound
    String[] from = new String[] {"title","ingredients","image"}; // built an String array named "from"
    //The XML defined views which the data will be bound to
    int[] to = new int[] { R.id.esodaTextView, R.id.amountTextView, R.id.imageView1}; // built an int array named "to"
    // EsodaMainActivity.this = The context in which the ListView is running
    // R.layout.esoda_list_item = Id of the layout that is used to display each item in ListView
    // null = 
    // from = String array containing the column names to display
    // to = Int array containing the column names to display
    esodaAdapter = new SimpleCursorAdapter (this, R.layout.recipe_list_item, null, from, to);
    esodaListView.setAdapter(esodaAdapter); // set esodaView's adapter
} // end of onCreate method

@Override
protected void onResume()
{
    super.onResume(); // call super's onResume method

    // create new GetEsodaTask and execute it
    // GetEsodaTask is an AsyncTask object
    new GetEsodaTask().execute((Object[]) null);
} // end of onResume method

// onStop method is executed when the Activity is no longer visible to the user
@Override
protected void onStop()
{
    Cursor cursor= esodaAdapter.getCursor(); // gets current cursor from esodaAdapter

    if (cursor != null) 
        cursor.deactivate(); // deactivate cursor

    esodaAdapter.changeCursor(null); // adapter now has no cursor (removes the cursor from the CursorAdapter)
    super.onStop();
} // end of onStop method

// this class performs db query outside the GUI
private class GetEsodaTask extends AsyncTask<Object, Object, Cursor>
{
    // we create a new DatabaseConnector obj
    // EsodaMainActivity.this = Context
    DatabaseConnector databaseConnector = new DatabaseConnector(RecipesMainActivity.this);

    // perform the db access
    @Override
    protected Cursor doInBackground(Object... params)
    {
        databaseConnector.open();

        // get a cursor containing call esoda
        return databaseConnector.getAllEsoda(); 
        // the cursor returned by getAllContacts() is passed to method onPostExecute()
    } // end of doInBackground method

    // here we use the cursor returned from the doInBackground() method
    @Override
    protected void onPostExecute(Cursor result)
    {
        esodaAdapter.changeCursor(result); // set the adapter's Cursor
        databaseConnector.close();
    } // end of onPostExecute() method
} // end of GetEsodaTask class
public类recipesmain活动扩展活动
{
公共静态最终字符串ROW\u ID=“ROW\u ID”;//意图额外键
私有ListView esodaListView;//ListActivity的ListView
私有SimpleCursorAdapter esodadapter;//ListView的适配器
DatabaseConnector DatabaseConnector=新的DatabaseConnector(RecipesMainActivity.this);
//首次创建活动时调用
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u recipes\u main);
esodaListView=(ListView)findViewById(R.id.esodaList);
setOnItemClickListener(viewsodalistener);
databaseConnector.open();
//将每个esoda映射到ListView布局中的TextView
//要绑定的所需列
String[]from=newstring[]{“title”、“components”、“image”};//构建了一个名为“from”的字符串数组
//数据将绑定到的XML定义的视图
int[]to=new int[]{R.id.esodaTextView,R.id.amountTextView,R.id.imageView1};//构建了一个名为“to”的int数组
//EsodaMainActivity.this=运行ListView的上下文
//R.layout.esoda_list_item=用于在ListView中显示每个项目的布局Id
//空=
//from=包含要显示的列名的字符串数组
//to=包含要显示的列名的Int数组
esodadapter=new SimpleCursorAdapter(this,R.layout.recipe_list_项,null,from,to);
esodaListView.setAdapter(ESODAdapter);//设置esodaView的适配器
}//onCreate方法的结束
@凌驾
受保护的void onResume()
{
super.onResume();//调用super的onResume方法
//创建新的getesodask并执行它
//GeteSodask是一个异步任务对象
新建geteSodask().execute((Object[])null;
}//onResume方法的结束
//当活动对用户不再可见时,将执行onStop方法
@凌驾
受保护的void onStop()
{
Cursor Cursor=esodadapter.getCursor();//从esodadapter获取当前光标
如果(光标!=null)
cursor.deactivate();//停用游标
esodadapter.changeCursor(null);//适配器现在没有光标(从光标适配器中删除光标)
super.onStop();
}//onStop方法结束
//此类在GUI外部执行db查询
私有类GeteSodask扩展异步任务
{
//我们创建了一个新的数据库连接器obj
//EsodaMainActivity.this=上下文
DatabaseConnector DatabaseConnector=新的DatabaseConnector(RecipesMainActivity.this);
//执行数据库访问
@凌驾
受保护的游标doInBackground(对象…参数)
{
databaseConnector.open();
//获取包含调用esoda的游标
返回databaseConnector.getAllesda();
//getAllContacts()返回的游标被传递给onPostExecute()方法
}//结束doInBackground方法
//这里我们使用doInBackground()方法返回的游标
@凌驾
受保护的void onPostExecute(游标结果)
{
esodadapter.changeCursor(结果);//设置适配器的光标
databaseConnector.close();
}//onPostExecute()方法的结尾
}//GeteSodask类的结束
我在网上搜索了很多东西,但找不到能帮助我的东西

是否可以使用simplecursoradaptor在imageview中设置图像?


我必须制作自定义光标适配器吗?如果我必须制作自定义光标适配器,我怎么做?

您需要设置适配器的
ViewBinder
,以将
ImageView
的图像设置为从DB接收的值

    esodaAdapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
                 @Override
                 public boolean setViewValue (View view, Cursor cursor, int columnIndex){
                     if (view.getId() == R.id.imageView1) {
                         ImageView IV=(ImageView) view;
                         int resID = getApplicationContext().getResources().getIdentifier(cursor.getString(columnIndex), "drawable",  getApplicationContext().getPackageName());
                         IV.setImageDrawable(getApplicationContext().getResources().getDrawable(resID));
                         return true;
                    }
                     return false;
         } 

谢谢你的回答,但我还是做不到。我必须把上面的代码放在哪里?我应该如何设置columnIndex?你如何在数据库中保存图像?图像都是@drawable mdpi,我将文件名(字符串)存储在名为“image”的列中。你将其保存为类似于R.drawable.xxx的字符串?我只保存图像“xxx”的文件名“。我知道这是一个非常古老的问题,但当我在搜索类似的解决方案时,它出现在搜索中,我想我会在这里加上我的两分钱…您可以使用SimpleCorsorAdaptor在imageView中设置图像。我使用联系人图片使用PHOTO_URI来显示联系人的图片,而无需编写太多代码。请在以下位置查看我的答案…”。。。