如何将数据从Recyclerview传递到另一视图?

时间:2018-07-22 11:22:56

标签: java android android-recyclerview

我目前正在使用电影评论应用程序。我正在尝试将数据从Recyclerview传递到新活动中的另一个recyclerview。目前,我知道如何将数据从Recyclerview传递到textview和imageview。但是,我在网上查看并尝试了代码,但仍然无法将数据从recyclerview传递到recyclerview。我试图传递的是包含URL的String ArrayList。如何传递数据? TY,请帮我!我最近刚开始使用android studio

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {

    Glide.with(mContext)
            .asBitmap()
            .load(mImageUrls.get(position))
            .into(holder.image);

    holder.name.setText(mNames.get(position));

    holder.image.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d(TAG, "onClick: Clicked on an image: " + mNames.get(position));
            Toast.makeText(mContext, mNames.get(position), Toast.LENGTH_SHORT).show();
            //Intent intent = new Intent(v.getContext(), MovieDetails.class);
            //v.getContext().startActivity(intent);

            Intent intent = new Intent(mContext, MovieDetails.class);
            intent.putExtra("image_url", mImageUrls.get(position));
            intent.putExtra("image_name", mNames.get(position));
            intent.putExtra("director_name", mDirector.get(position));
            intent.putStringArrayListExtra("movieTrailer", mTrailer);
            mContext.startActivity(intent);


        }
    });

}

private void setImage(String imageUrl, String imageName, String directorName, ArrayList trailerUrl) {
    Log.d(TAG, "setImage: setting the image and name to widgets.");

    TextView name = findViewById(R.id.text_movie_original_title);
    name.setText(imageName);

    ImageView image = findViewById(R.id.image_movie_detail_poster);
    Glide.with(this)
            .asBitmap()
            .load(imageUrl)
            .into(image);

    TextView director = findViewById(R.id.text_movie_director_name);
    director.setText(directorName);

    RecyclerView trailer = findViewById(R.id.movie_videos);
    trailer.set;

2 个答案:

答案 0 :(得分:0)

bundle.putParcelableArrayList("urlList", list)

通过intent

发送

通过

获取arraylist
getParcelableArrayList("urlList")

答案 1 :(得分:0)

我认为这些步骤会有所帮助: 1.创建数组

  1. 将相同的值放入数组,放在recyclerView上。

  2. 使用意图传递数组。示例:

    Bundle bundle = new Bundle();
    bundle.putStringArray("arrayOfName", new String[]{name1, name2});
    bundle.putStringArray("arrayOfImageUrls", new String[]{url1, url2});
    Intent intent=new Intent(context, SecondActivity.Class);
    intent.putExtras(bundle);
    
  3. 然后参加第二个活动:

    Bundle bundle = this.getIntent().getExtras();
    String[] names=bundle.getStringArray("arrayOfName");
    String[] urls=bundle.getStringArray("arrayOfImageUrls");
    
  4. 将此数组添加到SecondActivity中的recyclerView适配器

  5. 将适配器设置为您的recyclerView