简体   繁体   English

如何将自定义对象的ArrayList传递给新活动?

[英]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. 我已经创建了textViewTable类,在该类中,我正在保存与要传递给Next Activity的TextViews相关的数据。

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. 从我的活动,我想发送TextViewTable类的对象的ArrayList。 I have use the below function to send the ArrayList. 我使用下面的函数发送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: 当前使用Bundle.putSerializableTextViewTable类对象ArrayList发送到下一个Activity,但未在TextViewTable类中实现Serializable接口:

public class TextViewTable implement Serializable{

 ....
}

you can follow ρяσѕρєя K's answer OR also you can do like below code: 您可以按照ρяσsρєяK的答案进行操作,也可以按照以下代码操作:

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: 然后您可以在第一个活动中将数据存储在上述arraylist中,如下所示:

Collections.copy(GeneralClass.data,textViewsData);

and now you can use GeneralClass.data arraylist in your second activity; 现在,您可以在第二个活动中使用GeneralClass.data arraylist;

Sending the POJO from one activity to another acitivity: POJO从一项活动发送到另一项活动:

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 . 您也可以使用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 并且如果您已经为arraylist创建了一个bean类,则需要实现

public class CUSTOMOBJECT implements Serializable{

and you done :) 你完成了:)

you should use Parcelable object to pass data between activities as below: 您应该使用Parcelable对象在活动之间传递数据,如下所示:

Passing data from activity A to activity B 将数据从活动A传递到活动B

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

Getting data in Activity B from activity A 从活动A获取活动B中的数据

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

So simple. 很简单。

I hope this will help you. 我希望这能帮到您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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