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

前端 未结 3 1927
慢半拍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:04

    1st Activity

            Arraylist<String> values;
    
            Intent it=new Intent(MainActivity.this,Next.class);
            it.putExtra("value", values);
            startActivity(it);
    

    2nd Activity

      Arraylist<String> list;
    
    list=(ArrayList<String>) getIntent().getSerializableExtra("value");
    
    0 讨论(0)
  • 2021-01-29 01:08

    Try this, put you arraylist using putStringArrayListExtra

    Intent i = new Intent(MainActivity.this,Nextactivity.class);
    i.putStringArrayListExtra("arraylist", arraylist);
    

    And get arraylist.

    Intent i = getIntent();
    list = i.getIntegerArrayListExtra("arraylist");
    
    0 讨论(0)
  • 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<Qabir> list = new ArrayList<Qabir>();
    
        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<Qabir> 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<Qabir> list = new ArrayList<Qabir>();
    
        list = getObjectList(getIntent().getStringExtra("obj"));
    
        Log.e("Activity 2", "" +list.size());
    }
    
    private ArrayList<Qabir> getObjectList(String st) {
    
        ArrayList<Qabir> list = new ArrayList<Qabir>();
        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....

    0 讨论(0)
提交回复
热议问题