简体   繁体   English

android在单击列表视图中的项目时启动另一个活动

[英]android start another activity on click on item in a listview

 OnItemClickListener itemClickListener = new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View container, int position, long id) {
                // Getting the Container Layout of the ListView
                LinearLayout linearLayoutParent = (LinearLayout) container;

                // Getting the inner Linear Layout
                LinearLayout linearLayoutChild = (LinearLayout ) linearLayoutParent.getChildAt(1);

                // Getting the Country TextView
                TextView tvCountry = (TextView) linearLayoutChild.getChildAt(0);
            //here insted on toast i want to start different activity for different items   
                Toast.makeText(getBaseContext(), tvCountry.getText().toString(), Toast.LENGTH_SHORT).show();                
            }           
        };

        // Setting the item click listener for the listview
        listView.setOnItemClickListener(itemClickListener);
    }

Add tvCOuntry in "EXTRAS" and retrieve that in next activity which is same for every listview and perform action accordingly. 在“ EXTRAS”中添加tvCOuntry,并在下一个活动中检索该内容,每个列表视图都相同,并相应地执行操作。 Use following code to start new activity 使用以下代码开始新活动

listview.setOnItemClickListener(new OnItemClickListener(){

    @Override
    public void onItemClick(AdapterView<?> parent, View view,
            int position,long id) {
         LinearLayout linearLayoutParent = (LinearLayout) container;

            // Getting the inner Linear Layout
            LinearLayout linearLayoutChild = (LinearLayout ) linearLayoutParent.getChildAt(1);

            // Getting the Country TextView
            TextView tvCountry = (TextView) linearLayoutChild.getChildAt(0);


        Intent intent=new Intent(THIS_ACTIVITY.this,ACTIVITY_TO_START.class);
        intent.putExtra("Country",tvCountry.getText().toString());
        startActivity(intent);

    }
    });

After that use following code in "onCreate" in next activity 之后,在下一个活动的“ onCreate”中使用以下代码

        String passedArg = getIntent().getExtras().getString("country");

in next activity and perform action according to "passedArg" string 在下一个活动中,并根据“ passedArg”字符串执行操作

Which activity do you want to start? 您要开始哪个活动? The normal method is: 正常方法是:

Intent intent = new Intent(); Intent intent = new Intent(); StartActivity(intent); StartActivity(intent);

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

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