简体   繁体   English

选择 listItem 时如何启动新活动?

[英]How do I start a new activity when a listItem is selected?

My main activity page for the android app is mainly a listView and on clicking an item in that list I would like a new activity to start.我的 android 应用程序的主要活动页面主要是一个 listView,单击该列表中的一个项目时,我希望启动一个新活动。 I have created the intent and such as follows in the onCreate method of my MainActivity class:我在 MainActivity 类的 onCreate 方法中创建了意图,如下所示:

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);
    ListView lv = (ListView) findViewById(R.id.listview);
    lv.setAdapter(adapter);   

    final MainActivity context  = this;

    lv.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long arg3) {
            Intent myIntent = new Intent(context, SubActivity.class);
            myIntent.addFlags(position); //Optional parameters
            context.startActivity(myIntent);
        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub

        }
    });

}

My new activity class looks like this:我的新活动类如下所示:

public class SubActivity extends MainActivity{

ArrayList<String> sublist = new ArrayList<String>();
ArrayAdapter<String> subadapter;

ArrayList<ArrayList<String>> corrReceipts;

@Override
protected void onCreate(Bundle savedInstanceState) {
    Intent intent = getIntent();
    int position = intent.getFlags(); //this gives me the index of the receipt

    corrReceipts = super.addedReceipts;

    TextView tv = new TextView(this);
    tv.setText("Hello");


    subadapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, sublist);
    ListView slv = (ListView) findViewById(R.id.sublistview);
    slv.setAdapter(subadapter); 


    sublist.addAll(corrReceipts.get(position));
    subadapter.notifyDataSetChanged();

    setContentView(slv);

}

} }

I have my subActivity class extending MainActivity because it needs to access an array from which to get the information from.我有扩展 MainActivity 的 subActivity 类,因为它需要访问从中获取信息的数组。 When I run this code, and click a list item, nothing happens.当我运行此代码并单击列表项时,没有任何反应。 Any help as to what I'm doing wrong would be of great help!任何关于我做错了什么的帮助都会有很大帮助!

All you want is an ItemClickListener not an ItemSelectedListener .您想要的只是ItemClickListener而不是ItemSelectedListener

lv.setOnItemClickListener(new OnItemClickListener()
{
    @Override 
    public void onItemClick(AdapterView<?> arg0, View arg1,int position, long arg3)
    { 
        Intent myIntent = new Intent(context, SubActivity.class);
        myIntent.addFlags(position); //Optional parameters
        context.startActivity(myIntent);
    }
});

OnItemSelectedListener is used for Spinners, and OnItemClickListener is used for ListViews. OnItemSelectedListener用于OnItemSelectedListener ,而OnItemClickListener用于 ListView。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM