0
我有GridView並使用SimpleCursorAdapter來顯示數據庫中的數據。 我想單擊GridView上的項目時獲取與當前元素關聯的數據。我如何做到這一點? 這是我的適配器代碼:如何從與GridView關聯的光標獲取數據
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;
}
當bindView工作我看到光標改變它的位置。但是當我按下項目,我不知道什麼方法給我目前的光標位置。例如在公共long getItemId(int position)我得到參數「位置」,顯示我在網格中的當前位置。
對於GridView和光標中的當前元素位置,每次「位置」是否相同? – user2247712 2013-04-05 06:03:11