2

我正在开发一个 android 应用程序,但在使用ListActivity. Activity根据单击列表中的哪个项目,我想有一个不同的开始。

我制作了一个列表并setListAdapter在 java 中引用了它,但我不确定如何在OnListItemClick. 我假设我需要引用列表中的位置。

使用Activityand Buttons,我可以设置OnClickListenerand 使用switch带有casefor each的语句Button。什么是等价物ListActivity

这是我的代码:

public class Options extends ListActivity implements {

    String myHistory[]= { "Item 1", "Item 2", "Item 3" };

    //---Set ListView---
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setListAdapter(new ArrayAdapter<String> ( Options.this,
                                   android.R.layout.simple_list_item_1, myHistory)));
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        // TODO Auto-generated method stub
        super.onListItemClick(l, v, position, id); 

        //--if click position 1, do below
        //--Intent a = new Intent("show Item 1's corresponding page");
        //--startActivity(a);

        //--if click item in position 2, do below
        //--Intent b = new Intent("show Item 2's corresponding page");
        //--startActivity(b);

        //--if click item in position 3, do below
        //--Intent c = new Intent("show Item 3's corresponding page");
        //--startActivity(c);
    }
}
4

4 回答 4

5

我不确定还有其他方法可以做到这一点,但我是这样做的:我在活动类上设置了点击侦听器(而不是在适配器上,我认为这更有意义)。保存您希望调用的类的数组:

Class[] classList = new Class[]{class1.class, class2.class....};

将侦听器添加到列表视图

    lv.setOnItemClickListener(listviewClickListener());

然后是 onItemClickListener 方法:

private OnItemClickListener listviewClickListener() {
    OnItemClickListener clickListener = new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {


            //And then call the corresponing one
            Intent I= new Intent(thisActivity, classList[i]);
            int id = listOfObjects.get(position).getId();//do whatever you want with the object  on the postition "position"
            Bundle bundle = new Bundle();
            bundle.putInt("id", id);
            i.putExtras(bundle);
            thisActivity.startActivity(i);
        }
    };
    return clickListener;
}

我没有测试类文字数组,但我想它应该可以工作..

编辑:我正在考虑您想为每个项目创建不同的活动(考虑它没有多大意义(没有上下文),因为列表视图上的所有项目都属于同一个组,因此可能是以同样的方式使用)。否则,您不需要活动列表,只需将您希望的活动添加到意图中即可。

于 2012-06-19T07:58:48.717 回答
2

看起来列表中只有很少的某些数据,而不是在运行时,因为每个项目都有一个单独的活动(如游戏的菜单列表),所以我建议简短而简单.......

    protected void onListItemClick(ListView l, View v, int position, long id) {
        // TODO Auto-generated method stub
        super.onListItemClick(l, v, position, id); 

        Intent a;
         switch(position){

        case 1:
            a = new Intent("show Item 1's corresponding page");
            break;
         case 2:
           a  = new Intent("show Item 2's corresponding page");
           break;
        case 3:
           a = new Intent("show Item 3's corresponding page");
           break;

        if(null!=a)
        startActivity(a);
     }
}
于 2012-06-19T07:59:19.023 回答
0

您必须使用 Adapter 对象并将其设置为 ListView 对象。

ListView listView= getListView();
Adapter  medicineListAdapter = new ArrayAdapter<String>(
        Options.this, android.R.layout.simple_list_item_1, myHistory);

listView.setAdapter(medicineListAdapter);

添加这个并在类中 实现 onItemClickListener。这指的是 onItemClick 方法。

于 2012-06-19T07:58:28.133 回答
0
onListItemClick(ListView l, View v, int position, long id) 
position Corresponding your item of myHistory
you can judge  position  to do something :
switch (position) {
 case 1:
      Intent a = new Intent("show Item 1's corresponding page");
    //--startActivity(a);

等等...

于 2012-06-19T07:58:36.933 回答