如何在SimpleCursorAdapter中实现onitem click监听器

时间:2012-01-27 11:53:52

标签: android simplecursoradapter onitemclicklistener

我有以下代码段。

public class ImageStoreActivity extends ListActivity {
private DBHelper mDB;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mDB = new DBHelper(this);

    mDB.Reset();

    Bitmap img = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);

    mDB.createItemEntry(new ListObject(img, "x",   "999999", "blah"));
    mDB.createItemEntry(new ListObject(img, "y",     "56789", "blah blah"));
    mDB.createItemEntry(new ListObject(img, "Pfirsich", "4112344", "blaflakf"));
    mDB.createItemEntry(new ListObject(img, "Zitrone",  "4023232", "511131"));

    String[] columns = {mDB.KEY_ID, mDB.KEY_IMG, mDB.KEY_NAME, mDB.KEY_PHONE, mDB.KEY_RELATION};
    String   table   = mDB.RELATION_TABLE;

    Cursor c = mDB.getHandle().query(table, columns, null, null, null, null, null);

    startManagingCursor(c);

    SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
            R.layout.main,
            c,
            new String[] {mDB.KEY_IMG, mDB.KEY_NAME, mDB.KEY_PHONE, mDB.KEY_RELATION},
            new int[] {R.id.img, R.id.txt, R.id.textview,R.id.textview1});

    adapter.setViewBinder(new ItemViewBinder());
    setListAdapter(adapter);
  } 
}

如何添加此代码

public void onItemClick(AdapterView parentView, View v,
                int position, long id) {} 

到上面的代码。请帮帮我

2 个答案:

答案 0 :(得分:2)

在setListAdapter(adapter);

之后添加此代码
final ListView lv = getListView();
lv.setTextFilterEnabled(true);  
lv.setOnItemClickListener(new OnItemClickListener() {
   public void onItemClick(AdapterView<?> parent, View view, int position, long id) {                    
    // add the array list here..    
    }        
}); 

答案 1 :(得分:0)

覆盖onListItemClick()方法,如下所示,

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
        // Do something here
        ...
}