2

我正在做一個android食譜應用程序,我在哪裏使用數據庫。 在數據庫中有一個名爲」,‘圖像’,在那裏我存儲的配方,其中i存儲在繪製文件夾中的圖片的文件的名稱列 現在我想打一個列表和配方,可見: 1)的配方 2)的簡短描述 和 3)圖像配方 的爲了做到這一點,我使用一個Simplecursoradaptor。的標題SimpleCursorAdapter如何顯示圖像?

我的問題是我不能顯示圖像。

我想讀從柱「圖片」的文件名,然後設置圖像在我的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在圖像視圖中設置圖像嗎?

我必須製作一個自定義光標適配器嗎?如果我必須定製一個,我該怎麼做?

+0

我知道這是一個非常古老的問題,但它在搜索出來了,而我一直在尋找類似的解決方案,不過,我覺得我會增加我在這裏的兩分錢...... 您可以在設定的圖像帶有SimpleCursorAdaptor的imageView。我已經使用PHOTO_URI的聯繫人圖片來顯示聯繫人的圖片,而無需編寫太多的代碼。 請檢查我的答案在... http://stackoverflow.com/a/37710199/1209544 – Nashe

回答

10

您需要設置適配器的ViewBinder,以將ImageView的圖像設置爲從數據庫接收的值。

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; 
     } 
+0

謝謝你的答案,但我仍然無法做到這一點。我必須把上面的代碼放在哪裏?我應該如何設置columnIndex? –

+0

如何將圖像保存到數據庫中? –

+0

圖像都是@ drawable-mdpi,我在名爲「image」的列上存儲文件名(字符串) –