Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 572
TRX Avatar asked Jan 26 '26 00:01

TRX


1 Answers

Cursor.move() moves the cursor by relative amount. You want an absolute position, use moveToPosition() instead.

like image 165
laalto Avatar answered Jan 27 '26 13:01

laalto



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!