问题
I want to pass ArrayList of an Object from one Activity to Activity.
Here is the sample Code:
{
ArrayList<object> list=new ArrayList<>();
Object value[]=new Object[m.size()];
int n=0;
value[]=new Object[n];
value[n]=data.getName("name");
value[n]=data.getLocation("location");
list.add(value[n]);
n++; //Since there are "n" number of object
}
//here the value a which is ArrayList Object
// which I want to pass it from here to another activity.
So now how will I pass the object from here to another activity. However, I am able to get the value using Gson. By converting the value to json and pass it as String to SharedPreference and retrieving from another activity then convert back from Json using the Type to back it's Original form.
I want to know whether is it possible to pass the value using Parceable. Because I am getting null value when I used it.
here is the code which I tried to make my Object variable Parceable:
public class ProductList implements Parcelable {
private String NAME;
private String LOCATION;
public ProductList() {
}
protected ProductList(Parcel in) {
LOCATION= in.readString();
NAME= in.readString();
}
public static final Creator<ProductList> CREATOR = new Creator<ProductList>() {
@Override
public ProductList createFromParcel(Parcel in) {
return new ProductList(in);
}
@Override
public ProductList[] newArray(int size) {
return new ProductList[size];
}
};
public String getNAME() {
return NAME;
}
public void setNAME(String NAME) {
this.NAME= NAME;
}
public String getLOCATION() {
return LOCATION;
}
public void setITEM_IMAGE(String LOCATION) {
this.LOCATION= LOCATION;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(NAME);
dest.writeString(LOCATION);
}
}
回答1:
You can use activity result
link https://developer.android.com/training/basics/intents/result
code example
Code in activity principal
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
// check if the request code is same as what is passed here it is 2
if(requestCode==2)
{
String message=data.getStringExtra("MESSAGE");
textView1.setText(message);
}
}
In other activity
You can use the json format to pass the data
Gson gson = new Gson(); String json = gson.toJson(myObj);
Intent intent=new Intent();
intent.putExtra("MESSAGE",json);
setResult(2,intent);
finish();//finishing activity
if you don't want to use activity result, you can use singleton class
class MyClass{
companion object {
val staticField = "This is an example of static field Object Decleration"
fun getStaticFunction(): String {
return "This is example of static function for Object Decleration"
}
}
}
回答2:
Refer this link for understanding.
Activity one:
Intent intent = new Intent(getApplicationContext(), SecondActivity.class);
ArrayList<object> list=new ArrayList<>();
intent.putExtra("LIST", list);
startActivity(intent);
Activity Two:
ArrayList<object> list = (ArrayList<object>)getIntent().getSerializableExtra("LIST");
Also implement Serializable in your custom class.
public class ProductList implements Parcelable, Serializable
来源:https://stackoverflow.com/questions/62643429/how-do-i-pass-arraylistobject-from-one-activity-to-another-activity