如何检测列表视图上的点击并将数据发送到另一个活动?

时间:2014-09-09 19:58:57

标签: android listview android-intent send

基本上我想确保一旦用户点击列表视图,就会将youtube视频的网址发送到另一个活动,以显示youtube api。

我还想知道如何发送指向其他活动的链接

看到至少有十几个要插入的链接我不知道该怎么做。

第一项活动

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
  //creazione fullscreen activity
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
    WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.episodi_activity);

    //rimozione action bar

        if (Build.VERSION.SDK_INT < 11){
            getSupportActionBar().hide(); 
                }

        String[] titoli = getIntent().getStringArrayExtra("Titoli");

        ListView mylist = (ListView) findViewById(R.id.list1);

        List<ViewModel> models = new ArrayList<ViewModel>();
        for (int i = 0; i < titoli.length; i++) {
            String name = titoli[i];

            ViewModel model = new ViewModel(name);
            models.add(model);
        }

        final ArrayAdapter<ViewModel> adapter = new ArrayAdapter<ViewModel>(this, android.R.layout.simple_list_item_1, models);

        final String[] values = new String[] { "Gnu94B6Edrs", "PUTenLr0vpI"};

        mylist.setAdapter(adapter);
        mylist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> arg0, View v, int position, long id) {
                Intent video = new Intent(EpisodiActivity.this,VideoActivity.class);

                video.putExtra("tagLink", values[position]);
                startActivity(video);
            }
        });

第二次活动

public class VideoActivity extends YouTubeBaseActivity implements
YouTubePlayer.OnInitializedListener {

static private final String DEVELOPER_KEY = "AIzaSyBR-QH8hCO8U_WE_sIPGhEQIGmBEYCi7pQ";
private String VIDEO;



   @Override
   protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.videoactivity);
          YouTubePlayerView youTubeView = (YouTubePlayerView)
                  findViewById(R.id.youtube_video);
       youTubeView.initialize(DEVELOPER_KEY, this);

       Bundle VIDEO = getIntent().getExtras();
       if(VIDEO != null && VIDEO.containsKey("tagLink")){
           VIDEO.getString("tagLink");

       }
   }


   public void onInitializationFailure(Provider provider,
YouTubeInitializationResult error) {
            Toast.makeText(this, "Oh no! Errore di Connessione, provi a rientrare"+error.toString(),
Toast.LENGTH_LONG).show();
   }
   @Override
   public void onInitializationSuccess(Provider provider, YouTubePlayer player,
boolean wasRestored) {
          player.loadVideo(VIDEO);
   }

}

1 个答案:

答案 0 :(得分:0)

这很容易。你几乎拥有一切。

首先要做的是通过加载一个listview以及你需要的几个链接和一个ArrayAdapter来传递intent中的链接:

ListView mList = (ListView)findViewById(R.id.mlist);

    String[] labels = new String[] { "name 1", "name 2", "name 3"};
    final String[] links = new String[] { "http://youtube.com/1", "http://youtube.com/2", "http://youtube.com/3"};
    ArrayAdapter<String> ArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, labels);
    mList.setAdapter(ArrayAdapter);
    mList.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            // TODO Auto-generated method stub
            Intent mIntent = new Intent(LocalActivity.this, DestinyActivity.class);
            mIntent.putExtra("tagLink", links[position]);
            startActivity(mIntent);

        }
    });

然后,就是在DestinyActivity中读取是在onCreate方法中使用bundle:

Bundle mBundle = getIntent().getExtras();
        if(mBundle != null && mBundle.containsKey("tagLink")){
            VIDEO = mBundle.getString("tagLink");
        }

使用您的(私人字符串视频)过去该值。