listView点击条目跳转并带参数

listView点击条目跳转并带参数代码记录如下:

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    String url="rtmp://3387.livepla";
    String url1="rtmp://3387.liv";
    String url2="rtmp://3387.liveplay.m";

    @Override
    public void onItemClick(AdapterView parent, View view, int position, long id) {
        Intent intent = null;
        switch(position){
            case 0 :
                intent = new Intent(parent.getContext(), MainActivity.class);
                intent.putExtra("url",url);
                intent.putExtra("title","哈哈");
                break;
            case 1 :
                intent = new Intent(parent.getContext(), MainActivity.class);
                intent.putExtra("url",url1);
                intent.putExtra("title","呵呵");
                break;
            case 2 :
                intent = new Intent(parent.getContext(), MainActivity.class);
                intent.putExtra("url",url2);
                intent.putExtra("title","得得");
                break;
        }
        startActivity(intent);
    }
这是在listView中,现在看一下跳转到MainActivity.class中如何接收

Intent intent = getIntent();
//获取数据
url = intent.getStringExtra("url");
title =intent.getStringExtra("title");
Log.i("abc",url);
Log.i("abc",title);
如果MainActivity中有Fragment的,可以再把数据给过去代码如下:

String u=((MainActivity)getActivity()).getUrl();
注意的是getUrl()方法在MainActivity.class中要有


记录之



你可能感兴趣的:(android)