2

나는 데이터베이스를 사용하는 안드로이드 요리법 응용 프로그램을 만들고 있습니다. 데이터베이스에는 ""images "라는 이름의 열이 있습니다. 여기서 나는 파일의 이름을으로 그릴 수있는 폴더에 저장하는 조리법 그림 이제는 조리법 목록을 만들고 싶습니다. 1)SimpleCursorAdapter 이미지 표시 방법?

내 문제 나 이미지를 표시 할 수있는 레시피 2) 간단한 설명 및 3) 내가 Simplecursoradaptor 사용을 수행하려면 회사 화상 레시피 의. 제목.

내가 열 "이미지"에서 파일 이름을 읽을 수 다음 내 이미지 뷰 (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 

가 나는 수색 제비 뽑기 온라인 그러나 couldnt는 tha가 나를 도울 수 있었던 무엇인가 발견한다.

simplecursoradaptor를 사용하여 이미지 뷰에서 이미지를 설정할 수 있습니까?

사용자 정의 커서 어댑터를 만들어야합니까? 그리고 내가 관습을 만들어야한다면 어떻게 할 수 있습니까?

+0

나는 그것을 아주 오래된 질문을 알고 있지만 비슷한 솔루션을 찾고있었습니다하면서 검색에 등장하면서, 그래도 난 내가 당신은에서 이미지를 설정할 수 있습니다 ... 여기 내 두 센트를 추가합니다 SimpleCursorAdaptor를 사용한 imageView입니다. 많은 코드를 작성하지 않고 연락처 사진을 표시하려면 PHOTO_URI를 사용하여 연락처 사진을 사용했습니다. 내 대답을 확인하십시오 ... http://stackoverflow.com/a/37710199/1209544 – Nashe

답변

10

어댑터의 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; 
     } 
+0

답변 해 주셔서 감사합니다.하지만 여전히 할 수 없습니다. 위 코드를 어디에 넣어야합니까? 어떻게 columnIndex를 설정해야합니까? –

+0

DB에 이미지를 어떻게 저장합니까? –

+0

이미지는 모두 @ drawable-mdpi이고 "image"라는 이름의 열에 파일 이름 (문자열)을 저장합니다. –