0

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);
}
}
Athos Tokbi
  • 195
  • 1
  • 17

2 Answers2

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"
        }
    }
}
Alejandro Gonzalez
  • 1,221
  • 4
  • 15
  • 30
1

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
learner
  • 3,092
  • 2
  • 21
  • 33
  • There is one thing which I don't get it, in my case I have two App, one for **Receiver** and the other one for **Updater**. The **Receiver** pulls the data updated by the **updater**, there this when the **updater** makes some changes in the server the **Receiver** app it get Crash returning exception * Attempt to invoke null value reference* on the **Arraylist** which I have passed it RecyclerAdapter. – Athos Tokbi Jun 30 '20 at 11:59
  • I have found the solution for the one which I have commented. Thanks Bro!!! – Athos Tokbi Jun 30 '20 at 12:17
  • 1
    @AthosTokbi Mark the answer as accepted if you felt this helped you. – learner Jul 02 '20 at 14:03
  • Your resolution it helps but didn't resolve my solution when I tried I get null value reference. However, it helps me to go in deep about it. I have tried the other way around with shared preferences. – Athos Tokbi Jul 03 '20 at 10:51