Passing ArrayList of an object from one activity to another in android

前端 未结 3 1929
慢半拍i
慢半拍i 2021-01-29 00:23

So the case is i want to send data from one activity to another with the help of ArrayList of objects. Below is the class code which i want to use as object. So please tell me t

3条回答
  •  粉色の甜心
    2021-01-29 01:09

    First in your custom Class create two methods

    public class Qabir {
    
    public int age;
    public String name;
    
    Qabir(){
    }
    
    Qabir(int age,String name){
        this.age=age; this.name=name;
    }   
    
    // method for sending object
    public String toJSON(){
        return "{age:" + age + ",name:\"" +name +"\"}";
    }
    
    // method for get back original object
    public void initilizeWithJSONString(String jsonString){
    
        JSONObject json;        
        try {
            json =new JSONObject(jsonString );
            age=json.getInt("age");
            name=json.getString("name");
        } catch (JSONException e) {
            e.printStackTrace();
        } 
    }
    

    }

    then create a function to send object list

        @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        Qabir q1 = new Qabir(22, "KQ");
        Qabir q2 = new Qabir(23, "K Q");
        Qabir q3 = new Qabir(24, "K_Q");
    
        ArrayList list = new ArrayList();
    
        list.add(q1);
        list.add(q2);
        list.add(q3);
    
        Intent in = new Intent(this, SubActivity.class);
        in.putExtra("obj", arrayListToJSON(list));
        startActivity(in);
    }
    
    private String arrayListToJSON(ArrayList al) {
        JSONArray array = new JSONArray();
        try {
            for (int i = 0; i < al.size(); i++) {
                array.put(new JSONObject(al.get(i).toJSON()));
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return array.toString();
    }
    

    and create another function on receiver

        @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        ArrayList list = new ArrayList();
    
        list = getObjectList(getIntent().getStringExtra("obj"));
    
        Log.e("Activity 2", "" +list.size());
    }
    
    private ArrayList getObjectList(String st) {
    
        ArrayList list = new ArrayList();
        JSONArray array = null;
        try {
            array = new JSONArray(st);
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    
        for (int i = 0; i < array.length(); i++) {
            try {
                Qabir q= new Qabir();
                 q.initilizeWithJSONString(""+array.get(i));
                 list.add(q);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return list;
    }
    

    Enjoy....

提交回复
热议问题