我的SQLite列這樣定義:如何使用BaseAdapter從sqlite獲取ItemID?
public final static String S_ID = "_ID";
public final static String S_PHOTOPATH = "Photopath";
public final static String S_NAME = "Name";
public final static String S_POS = "Pos";
public final static String S_SCORE = "Score";
public final static String S_FIXED = "Fixed";
我想從sqlite的輔助類來getItemID與適配器類擴展了BaseAdapter。這裏是我的代碼:
public class Page1ListAdapter extends BaseAdapter {
private LayoutInflater adapterInflater;
public Cursor mCursor;
TagView tag;
public Page1ListAdapter(Context context, Cursor cursor) {
super();
mCursor = cursor;
adapterInflater=LayoutInflater.from(context);
}
@Override
public int getCount() {
return mCursor.getCount();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
tag = new TagView();
convertView = adapterInflater.inflate(R.layout.page1listview_item, null);
tag.imgV = (ImageView) convertView.findViewById(R.id.ItemImage);
tag.titleV = (TextView) convertView.findViewById(R.id.ItemTitle);
tag.descriptionV = (TextView) convertView.findViewById(R.id.ItemText);
convertView.setTag(tag);
} else {
tag = (TagView) convertView.getTag();
}
mCursor.moveToPosition(position);
Bitmap bmp = BitmapFactory.decodeFile(mCursor.getString(mCursor.getColumnIndex(DBHelper.S_PHOTOPATH)));
BitmapDrawable bmpDrawable = new BitmapDrawable(getActivity().getResources(), bmp);
tag.imgV.setBackground(bmpDrawable);
tag.titleV.setText(mCursor.getString(mCursor.getColumnIndex(DBHelper.S_NAME)));
tag.descriptionV.setText(mCursor.getString(mCursor.getColumnIndex(DBHelper.S_POS)));
return convertView;
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
Log.i("mCursor.getCount() --->", String.valueOf(mCursor.getCount()));
mCursor.move(position);
long id = mCursor.getLong(0); // <--- error here
return id;
}
public void setTag (Drawable dwb, String title, String description) {
tag.imgV.setBackground(dwb);
tag.titleV.setText(title);
tag.descriptionV.setText(description);
}
}
但是,getItemID()總是會得到錯誤或錯誤的ID。
我怎麼能得到光標的_ID列值?
在此先感謝!
爲什麼不使用SimpleCursorAdapter? – pskink
我需要BaseAdapter的getview()來自定義列表視圖。不熟悉SimpleCursorAdapter,它可以做同樣的事情嗎? – TRX
當然,在大多數情況下,你甚至不必重寫任何方法 – pskink