2012-12-29 139 views
0

我的ListView顯示來自數據庫的數據,我想要的是當我點擊一個特定的ListView選項時,一個新的活動開始,並在該活動中顯示與所選選項相關的數據。如何在ListView上選擇一個選項後顯示數據?

我在打開新的活動時沒有問題,但無法讓該活動知道在ListView上選擇了哪個選項。

代碼的ListView:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    database = new projectdatabase(ProjectExplorer.this); 

    openDatabase(); 
} 

private void openDatabase() { 

    database.open(); 
    cursor = database.getDataforDisplay(); 
    adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, cursor, new String[] {"project_name"}, new int[] { android.R.id.text1 }, 0); 
    setListAdapter(adapter); 

} 

@Override 
public void onListItemClick(ListView parent, View view, int position, long id) { 

    super.onListItemClick(parent, view, position, id); 

      Intent intent = new intent(this, openactivity.class); 
      startActivity(intent); 


    //What else to add here? 

}  

我只是想以「PROJECT_NAME」發送到其他活動,讓我查詢和檢索等信息。

回答

2

列表項點擊獲取project_name爲:

@Override 
public void onListItemClick(ListView parent, View view, int position, long id) { 

    super.onListItemClick(parent, view, position, id); 

     Cursor c = ((SimpleCursorAdapter)parent.getAdapter()).getCursor(); 
     c.moveToPosition(position); 

     // get project name here 

     String str_projectname= c.getString(c.getColumnIndex("project_name")); 

     Intent intent = new intent(this, openactivity.class); 

     intent.putExtra("project_name", str_projectname); 

     startActivity(intent); 


    //What else to add here? 

} 
相關問題