Parceable Arraylist of objects

前端 未结 3 1206
眼角桃花
眼角桃花 2021-01-24 21:50

I\'m trying to pass an ArrayList of Person Objects from the MainActivity to SecondActivity to print the details of the person in a custom listView adapter.

The applicati

3条回答
  •  旧时难觅i
    2021-01-24 22:13

    1.Create FirstActivity with one button

    package com.appkart.sdcardconnection;
    
    import java.util.ArrayList;
    
    import android.content.Intent;
    import android.os.Bundle;
    import android.support.v7.app.ActionBarActivity;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    
    public class FirstActivity extends ActionBarActivity {
    
        private Button btn;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.first_activity);
    
            btn = (Button) findViewById(R.id.btn);
            btn.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
                    ArrayList personList = new ArrayList();
    
                    personList.add(new Person("10", "Arun"));
                    personList.add(new Person("20", "Ankit"));          
    
                    intent.putExtra("person_list", personList);
                    startActivity(intent);              
                }
            });
        }
    }
    

    2.xml file

    
    
        

    3.Second Activity

    package com.appkart.sdcardconnection;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.util.Log;
    
    public class SecondActivity extends Activity {
    
        private static final String TAG = SecondActivity.class.getSimpleName();
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.second_activity);
    
            List personList = (ArrayList) getIntent()
                    .getSerializableExtra("person_list");
    
            for (Person person : personList) {
                Log.d(TAG, "id  : " + person.getId());
                Log.d(TAG, "name  : " + person.getName());
    
            }
        }
    }
    

    4.Person POJO

    package com.appkart.sdcardconnection;
    
    import java.io.Serializable;
    
    public class Person implements Serializable {
    
        private String id;
        private String name;
    
        public Person(String id, String name) {
            this.id = id;
            this.name = name;
        }
    
        public String getId() {
            return id;
        }
    
        public void setId(String id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
    }
    

    Use Serializable instead Parcelable. According to ArrayList java doc it implement Serializable. Array list java doc

自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题