如何从列表中选择项目后启动活动?

时间:2018-04-05 09:49:59

标签: android listview android-activity

我想从列表中选择一个项目。单击此项目时,我想打开一个新活动,并在下一个活动中显示相关项目。这是我的点击监听器的代码:

listView.setOnClickListener(new AdapterView.OnClickListener() {
    String item = (String) adapterView.getItemAtPosition(i);
    public onClick(AdapterView<?> adapterView, View view, int i, long l) {
        // Getting item text to be able to show it in a toast.
        String item = (String) adapterView.getItemAtPosition(i);

        Intent intent = new Intent(MainActivity.this, AddPlaneActivity.class);
        startActivity(intent);
    }
});

2 个答案:

答案 0 :(得分:0)

来自here

初始活动:

context.auth.uid

在下一个活动中访问该意图的信息:

Intent intent = new Intent(getBaseContext(), AddPlaneActivity.class);
intent.putExtra("EXTRA_STUFF_HERE", item);
startActivity(intent);

答案 1 :(得分:0)

尝试

 lv.setOnItemClickListener(new OnItemClickListener()
    {
        @Override 
        public void onItemClick(AdapterView<?> arg0, View arg1,int position, long arg3)
        { 
            Intent intent = new Intent(getBaseContext(), AddPlaneActivity.class);
            intent.putExtra("Item", item);
            startActivity(intent);
        }
    });

布局中带有android:descendantFocusability="blocksDescendants"