【发布时间】:2014-03-10 20:29:44
【问题描述】:
我有一个现有的 ListView,其中填充了来自 sqlite 数据库的一些信息。 用户可以创建狗的档案。 创建一些配置文件后,列表如下所示:
Example (抱歉链接,但我需要 10 Rep 才能直接拍照)
如您所见,这里有放置图片(狗图片)的地方。
在创建配置文件时,用户可以从他的狗中添加一张图片,该图片保存在内部存储 /Android/data/Files/*.jpg 中(我不想将其添加到 sqlite 数据库中)。 用于保护它的名称等于 SQLite 数据库中表行的 id。
假设我添加了一条新狗,行的 id 是 134,图片的名称是“134.jpg”
嗯,但是 atm 我不知道如何将这张图片添加到 ListView 中。
欢迎任何帮助!
最后但并非最不重要的是我如何从 sqlite 获取数据的代码和平:
private void fillData() {
mNotesCursor = helper.fetchAllData();
String[] from = new String[] { MyDatabaseAdapter.MySQLiteHelper.NAME,
MyDatabaseAdapter.MySQLiteHelper.PASSWORD,
MySQLiteHelper.CB_GETREIDE, MySQLiteHelper.CB_FASTENTAG,
MySQLiteHelper.CB_WOCHENPLAN, MySQLiteHelper.CB_DIET,
MySQLiteHelper.SP_ART };
// Fields on the UI to which we map
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
};
adapter = new SimpleCursorAdapter(getActivity(),
R.layout.hundeliste_item, mNotesCursor, from, to, 0);
mMyListView.setAdapter(adapter);
}
如果您需要更多信息,请询问,但我认为我需要或多或少的提示/想法如何将 pivture 添加到列表视图中
编辑: (因为 Merlevede 的回答)
感谢@Merlevede
我尝试了以下方法:
//... see post #1 for the upper code part
adapter = new SimpleCursorAdapter(getActivity(),
R.layout.hundeliste_item, mNotesCursor, from, to, 0);
adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder(){
// Binds the Cursor column defined by the specified index to the specified view
public boolean setViewValue(View view, Cursor cursor, int columnIndex){
LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.hundeliste_item, null);
if(view.getId() == R.id.imageView1){
File mediaStorageDir = new File(Environment.getExternalStorageDirectory()
+ "/Android/data/"
+ getActivity().getPackageName()
+ "/Files");
Uri uri = Uri.parse(mediaStorageDir.getPath() + File.separator + item_id +".jpg");
((ImageView)view.findViewById(R.id.imageView1)).setImageDrawable(Drawable.createFromPath(uri.toString()));
return true; //true because the data was bound to the view
}
return false;
}
});
mMyListView.setAdapter(adapter);
但它仍然没有向我显示图片。
【问题讨论】:
-
我没听懂。它没有显示任何东西?还是只有图片?
-
其余的显示在第一个屏幕中。只是图片仍然没有显示
标签: java android sqlite listview