4

我对 Android 世界比较陌生,但我有一个简单的问题。

我在一个页面上有一个列表,其中可能有无限数量的行,这些行将由数据库填充。这些将根据其类型(如客户等)进行不同的活动。是否可以根据用户点击的活动将用户发送到特定活动?

我见过一些基于 switch/case 的解决方案,但如果你有一个有限项的列表,并且你事先知道这些项的名称,这似乎是使用的。

所以,我要说的是,如果我使用 switch/case 解决方案,应用程序如何知道哪个按钮转到哪个 case?这需要在列表本身或数据库中定义吗?

我目前无法提供任何代码,因为它的开发还没有那么远,而且我也在该项目的 NDA 之下!

4

1 回答 1

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                       
  });
于 2012-06-20T13:39:36.760 回答