在Recyclerview中将从一个活动中选择的多个图像发送到第二个活动

时间:2016-05-21 08:48:49

标签: android

我想发送从FirstActivity中选择的多个图像,然后将它发送到SecondActivity,在我的onCreate方法(FirstActivity)中,我已经将我的recyclerview setAdapter声明为photoAdapter。

        recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(new StaggeredGridLayoutManager(4, OrientationHelper.VERTICAL));
        photoAdapter = new PhotoAdapter(this, selectedPhotos);
        recyclerView.setAdapter(photoAdapter);

在FirstActivity中按钮发送中的onClick方法将图像传递给SecondActivity,如下所示

     public void send(View v){
        Intent intent=new Intent(this, SecondActivity.class);
        intent.putExtra("ABCD",selectedPhotos)
        startActivity(intent);
        }

在SecondActivity中使用OnCreate方法,我声明了

         newRecyclerView = (RecyclerView)findViewById(R.id.newRV);
         newRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(4, OrientationHelper.VERTICAL));
         Intent new = getIntent();
         new.getExtra("ABCD");

程序运行良好,在FirstActivity RecyclerView上显示多个图像,但是当我单击按钮"发送"时,没有图像发送到SecondActivity RecyclerView。我怎么能解决这个问题?

2 个答案:

答案 0 :(得分:0)

您可以发送Arraylist图片名称或ID。像..

ArrayList<String> object = new ArrayList<String>(); //you can send image names or int resource id.
Intent intent = new Intent(Current.class, NextActivity.class);
Bundle args = new Bundle();
args.putSerializable("ARRAYLIST",(Serializable)object);
intent.putExtra("BUNDLE",args);
startActivity(intent);

并在下一个Activity获取Bundle

Intent intent = getIntent();
Bundle args = intent.getBundleExtra("BUNDLE");
ArrayList<String> object = (ArrayList<String>) args.getSerializable("ARRAYLIST");

未经测试但您可以尝试让我知道是否有任何问题。

答案 1 :(得分:0)

FirstActivity onclick方法

 public void send(View v){
        Intent intent = new Intent(this,Confirmation.class);
        Bundle args = new Bundle();
        args.putSerializable("ARRAYLIST", selectedPhotos);
        intent.putExtra("BUNDLE",args);

        startActivity(intent);

    }

和SecondActivity onCreate方法

    Intent intent = getIntent();
    Bundle args = intent.getBundleExtra("BUNDLE");
    selectedPhotos = (ArrayList<String>)args.getSerializable("ARRAYLIST");
    newRecyclerView=(RecyclerView)findViewById(R.id.recycler_view3);
    photoAdapter=new PhotoAdapter(this,selectedPhotos);
    newRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(4, OrientationHelper.VERTICAL));
    newRecyclerView.setAdapter(photoAdapter);