我想看看simplecursoradapter和内置ListView的联系人列表,显示他们的姓名,电子邮件和图片(全部保存在sqlite中)
有这个代码(注意我没有使用光标,我使用null因为我以后会提供光标):
private CursorAdapter contactAdapter;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
contactList = getListView();
contactList.setOnItemClickListener(viewContactListener);
String[] from = new String[] { "name","email","image" };
int[] to = new int[] { R.id.contactTextView,R.id.emailTextView2,R.id.imageView1};//imageView1 is a ImageView
contactAdapter = new SimpleCursorAdapter(
MyFirstActivity.this, R.layout.contact_list_item, null, from, to);
setListAdapter(contactAdapter);
}
然后:
@Override
protected void onResume()
{
super.onResume();
new StartPopulate().execute((Object[]) null);
}
private class StartPopulate extends AsyncTask<Object, Object, Cursor>
{
MyDatabaseConnector myDatabaseConnector =
new MyDatabaseConnector(myActivity.this);
// perform the database access
@Override
protected Cursor doInBackground(Object... params)
{
myDatabaseConnector.open();
return databaseConnector.getAllContacts(); //it is a cursor where I have the fields which I want to return
}
@Override
protected void onPostExecute(Cursor result)
{
contactAdapter.changeCursor(result);
myDatabaseConnector.close();
}
}
如果你不知道我使用changeCursor将光标放在onPostExecute中,而不是在开头,那么我的问题是:1。如何以这种方式使用viewBinder?Images in SimpleCursorAdapter 我想在里面添加一个图像(int [] to = new int [] {R.id.contactTextView,R.id.emailTextView2,R.id.imageView1}; // imageView1是一个ImageView) 所以这样我就可以看到姓名,电子邮件,图片
我的数据库还有以下内容:字段:
"CREATE TABLE wholeContacts" +
"(_id integer primary key autoincrement," +
"name TEXT, email TEXT, +
"image BLOB);";
2.-我是一个很好的做法,使用sqlite保存图片(在数据库内),如果可以包含近5000条记录,但图片是2.6kb(非常小的图片)???? 感谢
答案 0 :(得分:0)
使用BitmapFactory.decodeByteArray,其中字节数组取自Cursor.getBlob()