2011-04-09 50 views
5

我想爲我的適配器設置onItemClickListener,它可以工作,但現在我不知道如何獲取點擊對象?我有一個列表與筆記,並點擊我想開始與點擊筆記ID的新活動。如何獲取來自SimpleCursorAdapter的OnItemClickListener中的數據

private DatabaseHelper dbhelper; 

    SimpleCursorAdapter adapter = null; 
    public OnItemClickListener listener = new OnItemClickListener() { 

     public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, 
       long arg3) { 
      // TODO Auto-generated method stub 
      Toast.makeText(getApplicationContext(), "it works", Toast.LENGTH_SHORT).show(); 
     } 

    }; 
    @Override 
    public void onCreate(Bundle icicle) 
    { 
    super.onCreate(icicle); 

     setContentView(R.layout.first); 

     dbhelper = new DatabaseHelper(getApplicationContext()); 
     Cursor cursor = dbhelper.getAllNotes(); 
     startManagingCursor(cursor); 
     ListView lv = (ListView)findViewById(android.R.id.list); 

     String[] columns = new String[] {"NoteTitle", "NoteDate"}; 

     int[] to = new int[] { R.id.title_entry, R.id.date_entry}; 

     adapter = new SimpleCursorAdapter(this, R.layout.note_entry, cursor, columns, to); 

     lv.setAdapter(adapter); 
     lv.setOnItemClickListener(listener); 

    } 

... 
public Cursor getAllNotes() 
    { 
     SQLiteDatabase db=this.getReadableDatabase(); 

     return db.query(noteTable, new String [] {colID, colTitle, colDesc, colDate}, null, null, null, null, null); 

    } 
... 

我應該在onItemClick中插入什麼來獲取note標識(Toast僅用於檢查它是否工作)?提前/

感謝

格雷格

回答

2

以及最後一個元素參數是點擊行的id,我一直在尋找的答案,但我沒有找到。您可以使用該行獲取數據

@Override 
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
    Intent intent = new Intent(this, MyCustomDetailActivity.class); 
    intent.putExtra("item-identifier", id); 
    startActivity(intent); 
} 
+0

您能否告訴我如何在另一活動中接收意圖?我想在另一個activity的edittext中設置listview項目的內容。我已經按照你的回答,但我不知道如何在另一個活動中獲取所選項目的ID和數據庫內容。 – Apurva 2015-02-01 10:12:03

相關問題