android - SimpleCursorAdapter 如何显示图像?

标签 android imageview simplecursoradapter android-cursoradapter

我正在制作一个使用数据库的安卓食谱应用程序。 在数据库中有一个名为“图像”的列,我在其中存储我存储在可绘制文件夹中的食谱图片的文件名。 现在我想用食谱制作一个列表,显示: 1)食谱的标题 2)简短描述 和 3) 食谱的图片 为此,我使用了 Simplecursoradaptor。

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

我想从“图像”列中读取文件名,然后将图像设置在我的 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

我在网上搜索了很多,但找不到可以帮助我的东西。

我是否可以使用 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;
         } 

关于android - SimpleCursorAdapter 如何显示图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13330654/

相关文章:

android - Mapbox 离线和同步

android - 如何以编程方式更改通知栏中的应用程序名称

android - 使用 ViewPager + 放大 ImageViews 的图片库

android - 如何在 Android 的 Pull Parser 中获取具有匹配标签的 XML 内容?

java - 如何知道应用程序使用的是软键盘还是硬件键盘?

android - 如何在 NetBeans 的 Android 项目中使用外部 jar?

Android View 问题 - ImageView 和两个 TextView

android - 从子类 SimpleCursorAdapter 启动 AlertDialog

Android 的 SimpleCursorAdapter 使用 DISTINCT 进行查询

android - 如何使游标适应具有大量自定义行为的 ListView?