简体   繁体   中英

How to pass ArrayList of Custom objects to new activity?

I have created the class textViewTable In this class i am saving data related to TextViews That I want to Pass to Next Activity.

public class TextViewTable implements Serializable {
private String FONT;
private String TEXT;
private float TEXT_SIZE;
private ColorStateList TEXT_COLOR;
private float MARGIN_TOP;
private float MARGIN_BOTTOM;
private float MARGIN_LEFT;
private float MARGIN_RIGHT;
private Boolean BoldFlag;
private Boolean ItalicFlag;
private Boolean NormalFlag;

public TextViewTable(){
}

public TextViewTable(String FONT, String TEXT, float TEXT_SIZE, ColorStateList TEXT_COLOR, float MARGIN_TOP, float MARGIN_BOTTOM, float MARGIN_LEFT, float MARGIN_RIGHT, Boolean boldFlag, Boolean italicFlag, Boolean normalFlag) {
    this.FONT = FONT;
    this.TEXT = TEXT;
    this.TEXT_SIZE = TEXT_SIZE;
    this.TEXT_COLOR = TEXT_COLOR;
    this.MARGIN_TOP = MARGIN_TOP;
    this.MARGIN_BOTTOM = MARGIN_BOTTOM;
    this.MARGIN_LEFT = MARGIN_LEFT;
    this.MARGIN_RIGHT = MARGIN_RIGHT;
    BoldFlag = boldFlag;
    ItalicFlag = italicFlag;
    NormalFlag = normalFlag;
}

}

From my activit i want to send ArrayList of Objects of TextViewTable class. I have use the below function to send the ArrayList. But every time I am getting null pointer exception. Please Help to solve this.

public void onClick(View view)
    {
        Intent intent = new Intent(getApplicationContext(), displayImage.class);            
        Bundle bundleObject = new Bundle();
        bundleObject.putSerializable("key", textViewsData);

        intent.putExtras(bundleObject);
        try {
            startActivity(intent);
        }catch (Exception e){
            System.out.println(e);
        }
    }
};

logcat的

Currently using Bundle.putSerializable for sending TextViewTable class object ArrayList to next Activity but not implementing Serializable interface in TextViewTable class:

public class TextViewTable implement Serializable{

 ....
}

you can follow ρяσѕρєя K's answer OR also you can do like below code:

public class GeneralClass{

  public static ArrayList<TextViewTable> data = new ArrayList<TextViewTable>();

}

and then you can store your data in above arraylist on first activity like below:

Collections.copy(GeneralClass.data,textViewsData);

and now you can use GeneralClass.data arraylist in your second activity;

Sending the POJO from one activity to another acitivity:

Bundle bundle = new Bundle();
ArrayList<StatusData> passData = new ArrayList<StatusData>();
bundle.putSerializable("key", passData);
intent.putExtras(bundle);
startActivity(intent);
//then the transaction part

Getting the bundle`:

    Bundle bundle = new Bundle();
    bundle = getIntent().getExtras();
   ArrayList<StatusData> dataReceived = (ArrayList<StatusData>)bundle.getSerializable("key"));

and then do whatever you like.Hope this helps.Cheers. You can also use Parcelable .

First Activity

public void shareCustomArrayListObject(ArrayList<CUSTOMOBJECT> arrayList) {
    if (arrayList != null && arrayList.size() > 0) {

         Intent intent = new Intent(getApplicationContext(), displayImage.class);            
         Bundle bundleObject = new Bundle();
         bundle.putSerializable("KEY", arrayList);

         intent.putExtras(bundleObject);
    }
}

Second activity where you want to retrieve the arraylist

 private ArrayList<CUSTOMOBJECT> arrayList;

    Bundle bundle=YOURACTIVITY.getBundle();

    if(bundle==null){
        bundle=getArguments();
    }

    if(bundle.getSerializable("KEY")!=null){
    arrayList=(ArrayList)bundle.getSerializable("KEY");
    }

and also if you have made a bean class for the arraylist you need to implement

public class CUSTOMOBJECT implements Serializable{

and you done :)

you should use Parcelable object to pass data between activities as below:

Passing data from activity A to activity B

Intent intent=new Intent(A.this,B.class);
intent.putParcelableArrayListExtra("key", array_list);
startActivity(intent);

Getting data in Activity B from activity A

Intent intent=getIntent();
array_list = intent.getParcelableArrayListExtra("key");

So simple.

I hope this will help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM