Rohit Tiwari
Rohit Tiwari

Reputation: 31

How to pass List of list of objects from one activity to another

I need to pass an arraylist which contains object with a string and arraylist. I am unable to pass the arraylist to other activity. In the Activity1 the list contains value that I have verified.

DepositBn:

package support;

import android.os.Parcel;
import android.os.Parcelable;
import com.google.gson.annotations.SerializedName;
import java.util.ArrayList;

public class DepositBn implements Parcelable {

private String limit;
@SerializedName("period")
private ArrayList<Rate> rateList;

protected DepositBn(Parcel in) {
    limit = in.readString();
}

public String getLimit() {
    return limit;
}

public void setLimit(String limit) {
    this.limit = limit;
}

public ArrayList<Rate> getRateList() {
    return rateList;
}

public void setRateList(ArrayList<Rate> rateList) {
    this.rateList = rateList;
}

public class Rate {
    private String rate;

    public String getRate() {
        return rate;
    }

    public void setRate(String rate) {
        this.rate = rate;
    }

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

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(limit);
}
public static final Creator<DepositBn> CREATOR = new Creator<DepositBn>() {
    @Override
    public DepositBn createFromParcel(Parcel in) {
        return new DepositBn(in);
    }

    @Override
    public DepositBn[] newArray(int size) {
        return new DepositBn[size];
    }
};

}

In the Activity1:

@Override
    protected void onPostExecute(ArrayList<DepositBn> result) {
        super.onPostExecute(result);
        System.out.println("result = " + result);
        for (DepositBn depositBn : result) {
            System.out.println("----" + depositBn.getLimit());
        }
        try {
            Intent intent = new Intent(DepositRates.this, GenericRateDisplay.class);
            Resources res = DepositRates.this.getResources();

            Bundle bundle = new Bundle();
            bundle.putParcelableArrayList("depositBnArrayList",result);
            bundle.putString("pageName", res.getString(R.string.domesticRates));
            intent.putExtras(bundle);


            startActivity(intent);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

In Activity2:

package in.co.sbm.sbmvirtual;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.util.ArrayList;

import support.DepositBn;

public class GenericRateDisplay extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_generic_rate_display);

    Bundle b = this.getIntent().getExtras();

    ArrayList<DepositBn> depositBnArrayList = b.getParcelableArrayList("depositBnArrayList");
    //String pageName = b.getString("pageName");

    ArrayList<String> rateList = new ArrayList();

    for (DepositBn depositBn : depositBnArrayList) {
        for(DepositBn.Rate rate : depositBn.getRateList()){
            rateList.add(rate.toString());
        }
    }

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(GenericRateDisplay.this,
            android.R.layout.simple_list_item_1, rateList);
    ListView listView = (ListView) findViewById(R.id.genericListView);
    listView.setAdapter(adapter);
}

}

Upvotes: 1

Views: 1222

Answers (1)

Rohit Tiwari
Rohit Tiwari

Reputation: 31

I have fixed it. I have used inner class as well which I did not serialize hence I was unable to use putSerializable. I have serialized both the outer and inner class and used intent.putSerializable.

Upvotes: 1

Related Questions