Categories
android baseadapter listview sqlite

How to getItemID from sqlite with BaseAdapter?

I have sqlite column is defined like this:

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";

I’d like to getItemID from a sqlite helper class with a adapter class extends BaseAdapter. Here is my code:

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);
}
}

However, the getItemID() always get error or wrong ID.

How could I get the _ID column value with cursor?

Thanks in advance!