如何在Android中的两个活动之间传递数据?

时间:2014-09-07 18:48:24

标签: android listview youtube

视频活动

你给我的代码包含在videoactivity中,我在行私有字符串VIDEO = url的网址上出现错误;告诉我,url无法解析为变量

public class VideoActivity extends YouTubeBaseActivity implements
YouTubePlayer.OnInitializedListener {

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

private String VIDEO = url;

   @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 bundle = getIntent().getExtras();
       final String url = bundle.getString("url1");

   }


   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 :(得分:1)

使用:

Intent video = new Intent(EpisodiActivity.this,VideoActivity.class);
video.putExtra("url", "value");
startActivity(video);

同样在你上课:

mylist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    public void onItemClick(AdapterView<?> arg0, View v, int position, long id) {
        Intent video = new Intent(EpisodiActivity.this,VideoActivity.class);
        String urlFromListView=(String)arg0.getItemAtPosition(position);
        video.putExtra("url",urlFromListView);
        startActivity(video);
    }
});

要在VideoActivity中找回它:

onCreate(Bundle savedInstanceState)

之后的setContentView(R.layout.videoactivity);

Bundle bundle = getIntent().getExtras();
final String url = bundle.getString("url");