【问题标题】:SimpleCursorAdapter how to show an image?SimpleCursorAdapter 如何显示图像?
【发布时间】:2012-10-31 02:32:23
【问题描述】:

我正在制作一个使用数据库的 android 食谱应用程序。 在数据库中有一个名为“图像”的列,我在其中存储了我存储在可绘制文件夹中的食谱图片的文件名称。 现在我想用食谱制作一个列表,显示: 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 中设置图像?

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

【问题讨论】:

  • 我知道这是一个非常古老的问题,但是当我在寻找类似的解决方案时在搜索中出现了它,我想我会在这里加两分钱......你可以将图像设置为带有 SimpleCursorAdaptor 的 imageView。我使用了使用 PHOTO_URI 的联系人图片来显示联系人的图片,而无需编写太多代码。请查看我的答案...stackoverflow.com/a/37710199/1209544

标签: android imageview simplecursoradapter android-cursoradapter


【解决方案1】:

您需要设置适配器的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”的文件名。
猜你喜欢
  • 2013-04-14
  • 2011-10-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-12
  • 2021-12-20
  • 1970-01-01
相关资源
最近更新 更多