I have an existing ListView from a SQLite database. I want that user can add pictures from their gallery to the listview. These pictures get saved in the internal storage as "*.jpg" where * is the specific row id if a new account gets created.
example: A new entry in the SQLite database gets created with the table (id) number 8. Then if the user has chosen a pictures this gets saved in the internal storage with the name "8.jpg"
My Problem is how I can show that picture in the exact position in the listView... This is my code so far:
edit: (updated code)
private void fillData() {
mNotesCursor = helper.fetchAllData();
String[] from = new String[] { MySQLiteHelper.NAME, MySQLiteHelper.PASSWORD,
MySQLiteHelper.CB_GETREIDE, MySQLiteHelper.CB_FASTENTAG,
MySQLiteHelper.CB_WOCHENPLAN, MySQLiteHelper.CB_DIET,
MySQLiteHelper.SP_ART, MySQLiteHelper.PHOTO };
int[] to = new int[] { R.id.label, R.id.gewicht,
R.id.getreide,
R.id.fastentag,
R.id.wochenplan,
R.id.diet,
R.id.spinner,
R.id.imageButton1};
adapter = new SimpleCursorAdapter(getActivity(),
R.layout.hundeliste_item, mNotesCursor, from, to, 0);
mMyListView.setAdapter(adapter);
adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder(){
public boolean setViewValue(View view, Cursor cursor, int columnIndex){
final int id = mNotesCursor.getInt(mNotesCursor.getColumnIndex(MySQLiteHelper.UID));
LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.hundeliste_item, null);
File mediaStorageDir = new File(Environment.getExternalStorageDirectory()
+ "/Android/data/"
+ getActivity().getPackageName()
+ "/Files");
Uri uri = Uri.parse(mediaStorageDir.getPath() + File.separator + id +".jpg");
Log.v("TEST COMPARISON", "columnIndex=" + columnIndex + " ID = " + id + " URI = " + uri);
// I think here comes my mistake, but I don't know another solution
if(columnIndex == cursor.getColumnIndex(MySQLiteHelper.PHOTO)) {
((ImageView)view.findViewById(R.id.imageView1)).setImageDrawable(Drawable.createFromPath(uri.toString()));
Log.v("Test", "... this Log don't show up, cause columnIndex =/= id");
return true;
}
return false;
}
});
Any help is welcome. Getting crazy with this.. sitting here for a week without solution
edit: (LogOutput)
03-11 12:55:36.784: V/TEST COMPARISON(22456): columnIndex=1 ID = 1 URI = /storage/emulated/0/Android/data/com.example.android.navigationdrawerexample/Files/1.jpg
03-11 12:55:36.794: V/TEST COMPARISON(22456): columnIndex=2 ID = 1 URI = /storage/emulated/0/Android/data/com.example.android.navigationdrawerexample/Files/1.jpg
03-11 12:55:36.804: V/TEST COMPARISON(22456): columnIndex=7 ID = 1 URI = /storage/emulated/0/Android/data/com.example.android.navigationdrawerexample/Files/1.jpg
03-11 12:55:36.814: V/TEST COMPARISON(22456): columnIndex=1 ID = 2 URI = /storage/emulated/0/Android/data/com.example.android.navigationdrawerexample/Files/2.jpg
03-11 12:55:36.824: V/TEST COMPARISON(22456): columnIndex=2 ID = 2 URI = /storage/emulated/0/Android/data/com.example.android.navigationdrawerexample/Files/2.jpg
03-11 12:55:36.834: V/TEST COMPARISON(22456): columnIndex=7 ID = 2 URI = /storage/emulated/0/Android/data/com.example.android.navigationdrawerexample/Files/2.jpg
03-11 12:55:36.844: V/TEST COMPARISON(22456): columnIndex=1 ID = 3 URI = /storage/emulated/0/Android/data/com.example.android.navigationdrawerexample/Files/3.jpg
03-11 12:55:36.844: V/TEST COMPARISON(22456): columnIndex=2 ID = 3 URI = /storage/emulated/0/Android/data/com.example.android.navigationdrawerexample/Files/3.jpg
03-11 12:55:36.854: V/TEST COMPARISON(22456): columnIndex=7 ID = 3 URI = /storage/emulated/0/Android/data/com.example.android.navigationdrawerexample/Files/3.jpg
Ok so in your case it would be better to override
getViewI think, sincesetViewValuegets called for the columns that are in the cursor, which your image isn't (this way you would override the image with every column, having bad performance).Here is an example of how to create your view in
getView:EDIT changed to
bindViewandnewViewI hope this helps.