2012-11-29 65 views
0

我有一個ListView,它顯示靜態數據以及圖像(比如Activity A)。當我點擊一個ListVoew項目時,數據被捕獲並傳遞到下一個Activity(讓我們稱之爲Activity B)。在活動B中,我想將選定的值傳遞給數據庫選擇where子句並獲取數據並顯示與選擇相關的相關信息。這裏是我的代碼片段...傳遞Listview OnClick以從數據庫獲取數據

活動A

String subproduct = ((TextView) view.findViewById(R.id.product_label)).getText().toString(); 
         // Launching new Activity on selecting single List Item 

         SharedPreferences sharedPreferences = getSharedPreferences("pref",MODE_WORLD_READABLE); 
         SharedPreferences.Editor editor = sharedPreferences.edit(); 
         editor.clear(); 
         editor.putString("subproduct", subproduct); 
         editor.commit(); 
         Intent myIntent = new Intent(getApplicationContext(),Details.class);     
         startActivity(myIntent); 

,這裏是我的活動Benter code here

public void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     this.setContentView(R.layout.details1); 

SharedPreferences myPrefs = this.getSharedPreferences("pref", MODE_WORLD_READABLE); 
String subproduct = myPrefs.getString("subproduct","null");  
TextView txtProduct1 = (TextView) findViewById(R.id.detail_item); 
txtProduct1.setText(subproduct); 

,這裏是我的DBHelper.java 。在這裏我想通過選定的值。請幫我,我怎麼能達致這...

public Cursor getData(){ 
     //SharedPreferences myPrefs = this.getSharedPreferences("pref", 0); 
     //String subproduct = myPrefs.getString("subproduct","null"); 
     Cursor c = myDataBase.rawQuery("SELECT * FROM Destmain WHERE destname= " + ? + " ", null); 
     while (c.moveToNext()){ 
      DestInfo q = new DestInfo(); 
      q.setDestination(c.getString(1)); 
      q.setCity(c.getString(2)); 
      q.setCountry(c.getString(3)); 
      q.setPeriod(c.getString(4)); 
      q.setType(c.getString(5)); 
      q.setCurrency(c.getString(6)); 
      q.setBriefhistory(c.getString(7)); 
      q.setHighlights(c.getString(8)); 


     } 
     c.close(); 
     return c; 

誰能幫我,我怎麼能傳遞的ListView(活動A)選擇的值到活動B獲取DB數據並顯示?萬分感謝。

回答

0

通過活動使用方法onItemcClick你可以得到ID,並將其傳遞給第二個活動B.

onItemClick(AdapterView<?> adapterView, View view, int pos, long id) 
newActivity.putExtra("key", id); 

然後在活動B,您可以使用此ID在數據庫中搜索。

Intent newActivity = getIntent(); 
long id = newActivity.getLongExtras("key", -1); 

在其他一些情況下,你可以使用getLunchDetails方法

相關問題