定义listview并获取cur:
listNote = (ListView)findViewById(android.R.id.list);
cur = getContentResolver().query(uri,null,null,null,null);
使用SimpleCursorAdapter
adapter = new SimpleCursorAdapter(this,R.layout.note_item,cur,
new String[]{"title","date"},
new int[]{R.id.note_list_title,R.id.note_list_time});
listNote.setAdapter(adapter);
如何使用自定义的CursorAdapter呢?
adapter = new myCursorAdapter(this, cur);
listNote.setAdapter(adapter);
class myCursorAdapter extends CursorAdapter{ private LayoutInflater mInflater; private Context mContext; public myCursorAdapter(Context context, Cursor c) { super(context, c); mContext = context; mInflater = LayoutInflater.from(context); // TODO Auto-generated constructor stub } @Override public void bindView(View view, Context context, Cursor cursor) { TextView noteTitle = (TextView) view.findViewById(R.id.note_list_title); TextView noteData = (TextView) view.findViewById(R.id.note_list_time); Log.d("xxxx", "cur="+cursor.getCount()+",c_count="+cursor.getColumnCount()); noteTitle.setText(cur.getString(3)); noteData.setText(cur.getLong(2)); //super.bindView(view, context, cursor); } @Override public View newView(Context arg0, Cursor arg1, ViewGroup arg2) { return mInflater.inflate(R.layout.note_item, arg2, false); } }
Cursor c = getContentResolver().query(uri, null, "_id="+uri.getPathSegments().get(1), null, null);
c.moveToFirst();