android 在列表视图中单击项目时开始另一个 activity

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);
    }

在 "EXTRAS" 中添加 tvCOuntry 并在下一个 activity 中检索它,这对于每个列表视图都是相同的,并相应地执行操作。 使用以下代码开始新的 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);

    }
    });

之后在 "onCreate" 中使用下面的代码在下一个 activity

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

在下一个activity中根据"passedArg"字符串

执行动作

你想从哪个activity开始? 正常的方法是:

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