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

时间:2015-03-10 05:56:42

标签: android listview android-activity onitemclicklistener

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

2 个答案:

答案 0 :(得分:3)

在“EXTRAS”中添加tvCOuntry并在下一个活动中检索该内容,该活动对于每个列表视图都是相同的,并相应地执行操作。 使用以下代码开始新活动

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”中使用以下代码

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

在下一个活动中并根据“passedArg”字符串

执行操作

答案 1 :(得分:0)

您想开始哪项活动? 通常的方法是:

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