2012-06-20 69 views
4

我相對較新的Android世界,但我有一個快速的問題。如何讓每個ListItem去一個不同的活動

我在一個頁面上有一個列表,可能有無限量的行將被數據庫填充。這些將根據其類型(如客戶等)進行不同的活動。是否可以根據用戶點擊哪一個來將用戶發送到特定活動?

我已經看到了一些基於開關/盒子的解決方案,但是如果您有一個有限項目的列表,並且您事先知道這些項目的名稱,這似乎就可以使用。

所以,我說的是,如果我使用開關/案例解決方案,應用程序如何知道哪些按鈕去哪個案例?這是否需要在列表本身或數據庫中定義?

我目前無法提供任何代碼,因爲它沒有進入開發階段,我也在項目的NDA之下!

+0

開關似乎一個可能的解決方案,如果你有一個定義的所有可能的類型的列表。如果你不這樣做,試着做一個。有解決方案,但它們比交換機更復雜。 – purtip31

+0

請看看這個........ http://stackoverflow.com/a/11096757/804447 –

回答

3

您可以在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      
    }); 
+0

謝謝!完美工作。 –

+0

@ user1463725:歡迎您:) – Ashwin

相關問題