简体   繁体   English

将 ArrayList 从 RecyclerView 传递到另一个 Activity

[英]Passing ArrayList from RecyclerView into another Activity

I am trying to pass the data, which is set in the mainactivity.java into another activity(infoPage.java) but i cant seem to get it working.我试图将 mainactivity.java 中设置的数据传递到另一个活动(infoPage.java)中,但我似乎无法让它工作。

MainActivity.java主活动.java

data = new ArrayList<>();
       data.add( new CarData("Toyota Camry 2010 2.4V","Automatic","5 Seater","Petrol/Full","RM 40 /h",R.drawable.camry2010));


       recyclerView= findViewById(R.id.browsedRecycler);
       recyclerView.setLayoutManager(new LinearLayoutManager(this));
       adapter = new Adapter(this,data);
       recyclerView.setAdapter(adapter);

constructor for model class模型类的构造函数

public CarData(String title , String trans, String seats, String fuel, String price, int imgID) {

        this.title = title;
        this.trans = trans;
        this.seats = seats;
        this.fuel = fuel;
        this.price = price;
        this.imgID = imgID;
    }

Adapter class适配器类

public class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder> {


    private LayoutInflater layoutInflater;
    private List<CarData> data;

    public Adapter(Context context, List<CarData>data) {
        this.layoutInflater = LayoutInflater.from(context);
        this.data = data;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        View view = layoutInflater.inflate(R.layout.itemlist,viewGroup,false);
        return new ViewHolder(view);
    }

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

        //to bind the textview with data received

       String title = data.get(position).getTitle();
       String trans = data.get(position).getTrans();
       String seat = data.get(position).getSeats();
       String fuel = data.get(position).getFuel();
       String price = data.get(position).getPrice();
       int imgid = data.get(position).getImgID();
        holder.texttitle.setText(title);
        holder.trans.setText(trans);
        holder.seats.setText(seat);
        holder.fuel.setText(fuel);
        holder.price.setText(price);
        holder.imageView.setImageResource(imgid);

    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder{

        TextView texttitle,trans,seats,fuel,price;
        ImageView imageView;
       public ViewHolder(@NonNull View itemView) {
           super(itemView);

           itemView.setOnClickListener(new View.OnClickListener() {
               @Override
               public void onClick(View view) {

                   Intent i = new Intent(view.getContext(),infoPage.class);
                   i.putExtra("title", (data.get(getAbsoluteAdapterPosition())));
                   view.getContext().startActivity(i);
               }
           });

           texttitle = itemView.findViewById(R.id.textTitle);
           trans = itemView.findViewById(R.id.carTrans);
           seats = itemView.findViewById(R.id.seats);
           fuel = itemView.findViewById(R.id.fuel);
           price = itemView.findViewById(R.id.price);
           imageView=itemView.findViewById(R.id.itemImage);
       }
   }

my infopage.java has nothing much yet basically, just the declaration of the textviews and imageviews infoPage.java我的 infopage.java 基本上什么都没有,只是 textviews 和 imageviews infoPage.java 的声明

 TextView textView;
    ImageView carimage;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cars_info_page);

        textView=findViewById(R.id.detailText);
        carimage=findViewById(R.id.carsimg);

I've figured it out myself thanks for all the help!我已经自己弄明白了,谢谢大家的帮助!

for passing an ArrayList to another activity you should make your model class Parcelable, for doing this you can use the Parcelable plugin that adds some required codes to your model class and make it Parcelable.要将 ArrayList 传递给另一个活动,您应该使您的模型类 Parcelable,为此您可以使用 Parcelable 插件将一些必需的代码添加到您的模型类并使其成为 Parcelable。 you can use different plugins for this work, but the plugin that I use for my projects is "Android ParceLable Code Generator" by Michal Charmas.您可以为这项工作使用不同的插件,但我用于我的项目的插件是 Michal Charmas 的“Android ParceLable 代码生成器”。 You can download this plugin from the below path: File-> Settings-> Plugins After this , you should use putParcelableArrayListExtra() method to pass arraylist to destenation activity.您可以从以下路径下载此插件: File-> Settings-> Plugins 在此之后,您应该使用 putParcelableArrayListExtra() 方法将 arraylist 传递给目标活动。 You can get the Arraylist in destination Activity using getIntent().getParcelableArrayExtra() method that needs one parameter and that parameter is the String key that you specify in the first Activity您可以使用需要一个参数的 getIntent().getParcelableArrayExtra() 方法获取目标 Activity 中的 Arraylist,该方法是您在第一个 Activity 中指定的 String 键

I just realized that you are sending the data in wrong way.我刚刚意识到您以错误的方式发送数据。 The right way is to use getters and setters.正确的方法是使用 getter 和 setter。 If you don't know about them then you can have a look at this - https://www.c-sharpcorner.com/article/recyclerview-in-andriod-with-java/如果你不知道他们,那么你可以看看这个 - https://www.c-sharpcorner.com/article/recyclerview-in-andriod-with-java/

So, you need to do changes in the class where you have created constructors(Let's say Data Class) and use getters and setters there.因此,您需要在创建构造函数的类(假设是数据类)中进行更改,并在那里使用 getter 和 setter。

Initialize Data class in Mainactivity and set your data with the help of setters and then initialize the Data class in Adapter and get your data with the help of getters.在 Mainactivity 中初始化 Data 类并在 setter 的帮助下设置您的数据,然后在 Adapter 中初始化 Data 类并在 getter 的帮助下获取您的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将arraylist从一个活动传递到另一个活动的片段 - Passing arraylist from one activity to fragment of another 在另一个活动中传递 arraylist 时出错 - Error in passing arraylist in another activity 在这个程序中将ArrayList从一个活动传递到另一个活动不起作用 - in this program passing ArrayList from one activity to another not working 在ArrayList项目上单击将数据从一个活动传递到另一个活动 - passing data from one activity to another on ArrayList Item click 在android中将字符串数组的ArrayList从一个活动传递到另一个活动 - Passing ArrayList of string arrays from one activity to another in android 在一个活动中将 Arraylist 保存到 sharedpreferences 并在另一个活动中将其加载到 recyclerview - Save Arraylist into sharedpreferences in one activity and load it in recyclerview in another activity 单击时将数据RecyclerView数据传递到另一个活动 - Passing data RecyclerView data to another activity on click Android:将意图的BasicNameValuePairs的ArrayList传递给另一个Activity - Android:Passing ArrayList of BasicNameValuePairs with intent to another Activity Android-将自定义对象的ArrayList传递给另一个活动 - Android - Passing an ArrayList of Custom Objects to Another Activity 设置多维ArrayList并传递给另一个Activity - Setting multidimensional ArrayList and passing to another Activity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM