2012-01-27 124 views
0

我有以下代碼片段。如何在SimpleCursorAdapter中實現onitem點擊監聽器

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

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..  
    }   
}); 
+0

well..I在我的XML沒有的ListView。我只有行(R.layout.main)作爲xml內容。上面的代碼只有在父xml權限中定義了一個listview時纔有效?那正是我的問題 – user1002448 2012-01-27 12:13:24

+0

OK,那爲什麼你的使用ListActivity()中的代碼,這就是爲什麼我用這個代碼。 – 2012-01-27 12:16:25

+0

simplecursordapter數據綁定到一個列表視圖。所以我們必須在這裏使用列表活動 – user1002448 2012-01-27 12:19:58

0

覆蓋的onListItemClick()方法,這樣,

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