我有一个来自sqlite数据库的现有listview。我希望用户可以添加图片从他们的画廊到listview。这些图片以“.jpg”的形式保存在内部存储器中,其中是创建新帐户时的特定行id。
示例:在sqlite数据库中创建一个表(id)编号为8的新条目。然后,如果用户选择了一个图片,它将以名称“8.jpg”保存在内部存储器中
我的问题是如何在listview的确切位置显示图片。。。这是我目前的代码:
编辑:(更新代码)
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;
}
});
欢迎任何帮助。变得疯狂。。在这里坐了一个星期没有解决问题
编辑:(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
1条答案
按热度按时间ovfsdjhp1#
好吧,在你的情况下,最好
getView
我想,自从setViewValue
为光标中的列调用,而您的图像不是(这样,您会用每一列覆盖图像,性能很差)。下面是一个如何在中创建视图的示例
getView
:编辑更改为
bindView
以及newView
```private void fillData() {
}
private class MyAdapter extends SimpleCursorAdapter{
}