onItemClick将项目ID传递给intent

时间:2014-05-08 21:50:04

标签: android android-intent onitemclick

我从第一个意图中打印出正确的id,但没有正确地向第二个意图发送第二个意图。

首次点击项目点击: (对于以后的观看者,此问题已使用正确答案进行了更新):

    gridView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> a, View v, int i, long l) {

            Intent intent = new Intent(videoChannelActivity.this,
                    PlayVideoActivity.class);
            intent.putExtra("videoId", l);
            System.out.println("video id intent1 = " + l);
            startActivity(intent);
        }
    });

日志:05-08 17:40:40.505: I/System.out(13938): video id intent 1: 2

创建的第二个意图:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    final VideoView videoView;

    setContentView(R.layout.activity_play_video);

    Bundle extras;
    long videoLong;

    if (savedInstanceState == null) {
        extras = getIntent().getExtras();
        if (extras == null) {
            videoLong = 0;
        } else {
            videoLong = extras.getLong("videoId");
            System.out.println("video id intent 2 =  " + videoLong);
        }
    }

日志:05-08 17:46:19.166: I/System.out(14382): video id intent 2 = null

2 个答案:

答案 0 :(得分:1)

您传递long作为额外内容,但尝试将其视为String.

答案 1 :(得分:1)

l 类型为 ...

所以你需要提取long而不是像

这样的String
 videoLong = extras.getLong("videoId");

所以第二个意图的完整实现是

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    final VideoView videoView;

    setContentView(R.layout.activity_play_video);

    Bundle extras;
    Long videoLong;

    if (savedInstanceState == null) {
        extras = getIntent().getExtras();
        if (extras == null) {
            videoLong= null;
        } else {
            videoLong= extras.getLong("videoId");
            System.out.println("video id intent 2 =  " + videoLong);
        }
    }