Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
653 views
in Technique[技术] by (71.8m points)

android - How to display data from one activity's recyclerView to another activity's recyclerView using parcelable

I have been trying to display model Arraylist from one activity (data displayed in recyclerview) to another activity's recyclerView by using parcelable. I have managed to implement the parcelable part. I want to know, how to display it in the recyclerView for second activity.

FirstActivity.java

//Adding data to 1st activitiy's recyclerview

ArrayList<PersModel> persModels = new ArrayList<PersModel>();

private void addPers(int perID, String perRole, int perCS, int perCC) {

    PersModel PersDataModel = new PersModel();
    PersDataModel.setPerId(perID);
    PersDataModel.setPerCat(perRole);
    PersDataModel.setPerCleanStatus(perCS);
    PersDataModel.setPerCleanCode(perCC);
    persModels.add(PersDataModel);
    recyclerViewAdapter.updateModels(persModels);

}

//Starting next activity

        btnReview.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(AddPersonActivity.this, ReviewActivity.class);
            Bundle bundle = new Bundle();
            bundle.putParcelableArrayList("mylist", persModels);
            intent.putExtras(bundle);
            startActivity(intent);
        }
    });

SecondActivity.java

//Data passed from FirstActivity.java
Bundle bundle = getIntent().getExtras();
ArrayList<PersModel> arraylist = bundle.getParcelableArrayList("mylist");


//Add data to new model..THIS IS WRONG

    private void displayPersList( ArrayList arrayList){

    List<PersDisplayModel> newModels = new ArrayList<>();
    for (int i = 0; i < arrayList.size(); i++) {
        PersDisplayModel GetJobsDataModel = new PersDisplayModel();

        try {
            GetJobsDataModel.setPerId(arrayList.get());
            GetJobsDataModel.setPerCat(perRole);
            GetJobsDataModel.setPerCleanStatus(perCS);
            GetJobsDataModel.setPerCleanCode(perCC);

            newModels.add(GetJobsDataModel);
        } catch (JSONException e) {

            e.printStackTrace();
        }
    }
    if (array.length() != 0) {
        recyclerViewAdapter.updateModels(newModels);
    }



}

PersModel.java

public class PersModel implements Parcelable {
int perId;
int perCleanStatus;
int perCleanCode;
String perCat;

public PersModel() {

}


public int getPerId() {
    return perId;
}

public void setPerId(int perId) {
    this.perId = perId;
}

public int getPerCleanStatus() {
    return perCleanStatus;
}

public void setPerCleanStatus(int perCleanStatus) {
    this.perCleanStatus = perCleanStatus;
}

public int getPerCleanCode() {
    return perCleanCode;
}

public void setPerCleanCode(int perCleanCode) {
    this.perCleanCode = perCleanCode;
}

public String getPerCat() {
    return perCat;
}

public void setPerCat(String perCat) {
    this.perCat = perCat;
}


@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeInt(perId);
    dest.writeInt(perCleanStatus);
    dest.writeInt(perCleanCode);
    dest.writeString(perCat);
}

public PersModel(Parcel in) {
    perId = in.readInt();
    perCleanStatus = in.readInt();
    perCleanCode = in.readInt();
    perCat = in.readString();
}


public static final Creator<PersModel> CREATOR = new Creator<PersModel>() {
    @Override
    public PersModel createFromParcel(Parcel in) {
        return new PersModel(in);
    }

    @Override
    public PersModel[] newArray(int size) {
        return new PersModel[size];
    }
};
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I am going to assume that PersModel and PersDisplayModel have the same setters and getters.

You need to change this:

GetJobsDataModel.setPerId(arrayList.get(i));
GetJobsDataModel.setPerCat(perRole);
GetJobsDataModel.setPerCleanStatus(perCS);
GetJobsDataModel.setPerCleanCode(perCC);

To this:

GetJobsDataModel.setPerId(arrayList.get(i).getPerId());
GetJobsDataModel.setPerCat(arrayList.get(i).getPerCat());
GetJobsDataModel.setPerCleanStatus(arrayList.get(i).getPerCleanStatus());
GetJobsDataModel.setPerCleanCode(arrayList.get(i).getPerCleanCode());

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...