android - How to get data from Cursor associated with GridView -
i have gridview , using simplecursoradapter show data database. want data associated current element when clicked item on gridview. how can it? that's adapter code:
public class receiptadapter extends simplecursoradapter { private context context; private cursor cursor; private int layout; private map<object,string> receipt_id_cursor_id=new hashmap<object,string>(); //public receiptadapter(context context, arraylist<receipt> receiptvalues) { public receiptadapter(context _context, int _layout, cursor _cursor, string[] _from, int[] _to){ super(_context, _layout, _cursor, _from, _to); cursor = _cursor; layout = _layout; this.context = _context; } @override public view newview(context _context, cursor _cursor, viewgroup parent) { layoutinflater inflater = (layoutinflater) _context.getsystemservice(_context.layout_inflater_service); view view = inflater.inflate(layout, null); return view; } @override public void bindview(view view, context _context, cursor _cursor) { receipt_id_cursor_id.put(_cursor.getposition(), _cursor.getstring(_cursor.getcolumnindex(receiptentry.column_name_receipt_name))) ; textview textview = (textview) view .findviewbyid(r.id.grid_item_label); textview.settext(_cursor.getstring(_cursor.getcolumnindex(receiptentry.column_name_receipt_name))); // set image based on selected text imageview imageview = (imageview) view .findviewbyid(r.id.grid_item_image); imageview.setimageresource(app.getinstance().getresources().getidentifier(_cursor.getstring(_cursor.getcolumnindex(receiptentry.column_name_ic_name)) , "drawable", app.getinstance().getpackagename())); } @override public int getcount() { //return receiptvalues.size()-1; return cursor.getcount(); } @override public cursor getitem(int position) { //return receiptvalues.get(position); return this.getcursor(); //return cursor; } @override public long getitemid(int position) { return cursor.getposition(); //return position; }
when bindview working see cursor change position. when pressed item don't know method give me current position of cursor. example in public long getitemid(int position)i argument "position" show me current position in grid.
in public long getitemid(int position) method write below code.
cursor.movetoposition(position);
then in listener access cursor directly.
Comments
Post a Comment