2011-04-12 3 views
0

나는 목록 뷰로 db와리스트에 추가 할 수있다. onListItemClick을 사용하여 목록 항목을 클릭하면 어떤 문장을 사용해야합니까? 값을 가져올 필요가 있습니까?onListItemClick, 값을 얻기 위해 필요한 성명은 무엇입니까?

미리 감사드립니다. 먼저

public class Main extends ListActivity { 
private static String[] FROM = { _ID, DESCRIPTION, UN }; 
private static String ORDER_BY = DESCRIPTION + " ASC"; 
private static int[] TO = {R.id.itemrowid, R.id.itemdescription, R.id.itemun }; 
private EventsData events; 

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    events = new EventsData(this); 
    try { 
     Cursor cursor = getEvents(); 
     showEvents(cursor); 
    } finally { 
     events.close(); 
    } 

**public void onListItemClick(ListView parent, View v, int position, long id) { 
    // What statement to put here to get the value of _ID,DESCRIPTION, UN 
    // selected**? 
} 

private Cursor getEvents() { 
    // Perform a managed query. The Activity will handle closing 
    // and re-querying the cursor when needed. 
    SQLiteDatabase db = events.getReadableDatabase(); 
    Cursor cursor = db.query(TABLE_NAME, FROM, null, null, null, 
      null, ORDER_BY); 
    startManagingCursor(cursor); 
    return cursor; 
} 

private void showEvents(Cursor cursor) { 
    // Set up data binding 
    SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, 
      R.layout.item, cursor, FROM, TO); 
    setListAdapter(adapter); 
} 
+0

참조 http://stackoverflow.com/questions/5603429/how-to-get-value-from-database-onlistitemclick/5603584# 5603584 – rajath

답변

8

, 당신은 클릭 한 위치에 항목을 얻을 필요가 : 당신은 그래서 그냥 예를 들어, 사용합니까 어떤 종류의 모르는 (

Cursor cursor = getListAdapter().getItem(position); 

그런 다음이 커서에서 데이터를 얻을 수 있습니다 그것은 intString)입니다 :

int id = cursor.getInt(cursor.getColumnIndex(_ID)); 
String description = cursor.getString(cursor.getColumnIndex(DESCRIPTION)); 

가 커서의 가능한 방법에 대한 documentation를 참조하십시오.

0

sergey 응답에 추가하면 어떤보기가 클릭되었는지 직접 볼 수 있으며 findViewById()을 사용하여 원하는 값을 찾을 수 있습니다.

이 방법 세르게이 훨씬 적은 '누덕 누덕 기운을'있는 단지 대안

관련 문제