2012-08-26 5 views
0

각 ListItem에 2 개의 TextView가있는 ListView가 있습니다. ListItem 클릭 할 때 해당 TextViews 값을 다른 활동에 전달하고 싶습니다. 내 모든 시도는 실패했습니다. 그래서 어떻게 할 수 있습니까? SQLite ROW_ID, R.id.text1 및 R.id.text2 값을 어떻게 전달할 수 있습니까?ListView에서 다른 액티비티로 데이터 전달하기

Cursor cursor = database.getAllNames(); 
    adapter = new SimpleCursorAdapter(this, R.layout.listrow, cursor, 
      new String[] { Database.KEY_DATE , Database.KEY_NAME }, 
      new int[] {R.id.text1, R.id.text2}, 0); 

    listView = (ListView) findViewById(R.id.list); 
    listView.setAdapter(adapter); 

    listView.setOnItemClickListener(new OnItemClickListener() { 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
      Intent intent = new Intent(SendingData.this, ReceivingData.class); 
      intent.putExtra("id", id); //this should pass the SQLite ROW_ID 
      intent.putExtra("date", date); //this should pass the value of R.id.text1 
      intent.putExtra("name", name); //this should pass the value of R.id.text2 
      startActivity(intent); 
     } 
     }); 

답변

0

SQLLite 데이터베이스에서 데이터를 검색 할 때 사용합니다.

cursor.getString(c.getColumnIndex(Database.columns[i])) // i is the index of the column whose details you want to fetch 

intent.putExtra에서 매개 변수로 전달할 수 있습니다.

listView.setOnItemClickListener(new OnItemClickListener() { 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
      Cursor cursor = null; 
      cursor = (Cursor) parent.getItemAtPosition(position); 
      Intent intent = new Intent(SendingData.this, ReceivingData.class); 
      intent.putExtra("id", cursor.getInt(cursor.getColumnIndex(Database.columns[0]))); //assuming that the first column in your database table is the row_id 
      intent.putExtra("date", cursor.getString(cursor.getColumnIndex(Database.columns[1]))); //assuming that the second column in your database table is the text1 
      intent.putExtra("name", cursor.getString(cursor.getColumnIndex(Database.columns[2]))); //assuming that the third column in your database table is the text2 
      startActivity(intent); 
     } 
     }); 
+0

하지만 그것은 전체 칼럼을 통과합니다. 그렇죠? 선택한 항목의 데이터 만 전달하려고합니다. – user1617102

+0

예, 커서를 제대로 설정하지 않았기 때문일 수 있습니다. 모든 행을 검색하는 데 익숙했을 수도 있으므로 전체 열을 전달하는 것입니다. – Swayam

+0

업데이트 된 답변을 확인하십시오. onItemClick 내에서 커서를 사용하여 해당 행만 검색하는 코드를 추가했습니다. – Swayam

0
(이미하고있는로와 onItemClick 방법에 전달 된 행 ID를 사용하여)

당신은 커서를 액세스 할 필요없이보기에서 직접 데이터를 복용하여 작업을 수행 할 수 있습니다 :

listView.setOnItemClickListener(new OnItemClickListener() { 
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 

     TextView text1 = (TextView) view.findViewById(R.id.text1); 
     TextView text2 = (TextView) view.findViewById(R.id.text1); 
     String date = text1.getText().tostring(); 
     String name = text2.getText().tostring(); 

     Intent intent = new Intent(SendingData.this, ReceivingData.class); 
     intent.putExtra("id", id); //this should pass the SQLite ROW_ID 
     intent.putExtra("date", date); //this should pass the value of R.id.text1 
     intent.putExtra("name", name); //this should pass the value of R.id.text2 
     startActivity(intent); 
    } 
}); 
관련 문제