[英]How to make each ListItem go to a different activity
我對Android世界相對較新,但我有一個簡單的問題。
我在一個頁面上有一個列表,該列表具有可能無限量的行,這些行將由數據庫填充。 這些將根據其類型(如客戶等)進行不同的活動。是否可以根據用戶點擊哪個活動將用戶發送到特定活動?
我已經看到了一些基於開關/案例的解決方案,但如果您有一個有限項列表,並且事先知道這些項的名稱,這似乎就會被使用。
所以,我所說的是,如果我使用開關/案例解決方案,應用程序如何知道哪些按鈕適用於哪種情況? 這是否需要在列表本身或數據庫中的某些內容中定義?
我目前無法提供任何代碼,因為它還沒有進入開發階段,而且我也在NDA下關於該項目!
您可以在setonitemclicklistener()
。 只要單擊列表視圖的任何行,就會調用此函數。 您單擊的項目的位置/行號也會傳遞給此函數。
由於您使用數據庫填充列表,因此可以執行以下操作:
setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, COUNTRIES));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
//SEE THIS FUNCTION
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
/*
"position" is the position/row of the item that you have clicked
Query your table. Then move the cursor to "position" and get the
details. THen based on the details call a new activity.
*/
Cursor cur=db.query("your table name",null,null,null,null,null.null);
cur.moveToPosition(position); // see the argument int position
//Extract details from this row,process it and send to the respective activiy
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.