如何使每个ListItem转到不同的活动

时间:2012-06-20 13:25:14

标签: android eclipse listview

我对Android世界相对较新,但我有一个简单的问题。

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

我见过一些基于开关/案例的解决方案,但如果您有一个有限项列表,并且事先知道这些项的名称,这似乎就会被使用。

所以,我所说的是,如果我使用开关/案例解决方案,应用程序如何知道哪些按钮适用于哪种情况?这是否需要在列表本身或数据库中的某些内容中定义?

我目前无法提供任何代码,因为它在开发方面并不是很远,而且我也在NDA下关于该项目!

1 个答案:

答案 0 :(得分: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                       
  });