如何使用POJO类将RecyclerView中的值传递给AnotherActivity? [英] How do I pass values from RecyclerView to AnotherActivity with POJO class?

查看:134
本文介绍了如何使用POJO类将RecyclerView中的值传递给AnotherActivity?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个POJO类.它包含一些字符串,我想将值从RecyclerView传递到Activity.我尝试了一些但失败了.我该怎么办?

I have a POJO class. It holds some Strings and I want to pass values from RecyclerView to an Activity. I tried something and failed. How do I do that?

型号

public class DetailModel {
    String title;

    public DetailModel() {
    }

    public DetailModel(String title) {
        this.title = title;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }
}

适配器,onClick

Adapter, onClick

holder.cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                detailModel=new DetailModel();
                detailModels= new ArrayList<>();

                detailModel.setTitle(holder.headerText.getText().toString());

                Intent detailIntent = new Intent(context.getApplicationContext(), DetailActivity.class);

                context.startActivity(detailIntent);
            }
        });

另一项活动

Another Activity

DetailModel detailModel=new DetailModel();

        detailBody.setText(detailModel.getTitle());

推荐答案

有几种方法可以做到这一点,但我将向您展示最简单的方法:

There is several ways to do that, but I will show you simplest one :

在您的onBindViewHolder中:

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder,final int position) {
    final MyView myHolder = (MyView) holder;
    myHolder.cardView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            detailModel.setTitle(holder.headerText.getText().toString());

            Intent detailIntent = new Intent(context, DetailActivity.class);
            //To pass your class:
            intent.putExtra("mypojo", details.get(position));
            context.startActivity(detailIntent);

        }
    });

}

这样,您必须在POJO类中实现Serializable:

In this way you must implement Serializable in your POJO class :

public class DetailModel implements Serializable{
String title;

public DetailModel() {
}

public DetailModel(String title) {
    this.title = title;
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}
}

然后在第二个Activity中使用此代码获取类:

And in second Activity use this code to get class :

getIntent().getExtras().getSerializable("mypojo");
//also you can cast extra 
DetailModel detailmodel=(Detailmodel) getIntent().getExtras().getSerializable("mypojo");
//if you implemented Parcelable
DetailModel detailmodel=(Detailmodel) getIntent().getExtras().getParcelable("mypojo");

这是第一种方式,以另一种更快的方式使用Parcelable,签出此答案,但是您必须在Parcelable中付出一些努力:

This is the first way, in another and faster way is using Parcelable , checkout this answer but you must make some effort in Parcelable : how-to-send-an-object-from-one-android-activity-to-another-using-intent

我建议您使用Parcelable,也许您需要一些时间来学习它,但是在较大的课程中您会感觉到速度差异.

I suggest you to use Parcelable maybe you need some time to learn it but you can feel speed difference in bigger classes.

此外,如果POJO中的字符串数量较少,请尝试使用Intent Extras,而不是像这样发送类:

Also if you have small amounts of Strings in your POJO, try to use intent extras instead of sending class like that :

intent.putExtra("model_name",pojo.getName());
//then get
getIntent().getExtras().getString("model_name");

这篇关于如何使用POJO类将RecyclerView中的值传递给AnotherActivity?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆