2011-04-09 8 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); 

    } 
... 

나는 (토스트 만 작동하는지 확인을위한) 참고 ID를 얻을 수 onItemClick에 무엇을 삽입해야합니까? 나는 대답을 찾고 있었어요하지만 난 찾지 못함/

사전에 감사

그렉

답변

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

다른 활동에서 의도를받는 방법을 알려주시겠습니까? 다른 활동의 edittext에 listview 항목의 내용을 설정하고 싶습니다. 귀하의 답변을 따라했지만 다른 활동에서 선택한 항목의 ID 및 데이터베이스 내용을 얻는 방법을 모르겠습니다. – Apurva

관련 문제