Passing an arraylist of arraylists

后端 未结 3 1333
[愿得一人]
[愿得一人] 2021-01-28 22:37

I know it\'s a commonly asked question and I looked at the solutions online, but I am having a difficulty implementing this on my own.

I have a class which contains thr

3条回答
  •  暖寄归人
    2021-01-28 23:04

    Ok.

    Your Model

    public class ImageHolder implements Serializable {
    
        // Use String here, to avoid serialization problems
        private String uri;
        private Date date;
        private Location loc;
    
        public ImageHolder() {
        }
    
        public ImageHolder(String uri, Date date, Location loc) {
            this.uri = uri;
            this.date = date;
            this.loc = loc;
        }
    
        public String getUri() {
            return uri;
        }
    
        public void setUri(String uri) {
            this.uri = uri;
        }
    
        public Date getDate() {
            return date;
        }
    
        public void setDate(Date date) {
            this.date = date;
        }
    
        public Location getLoc() {
            return loc;
        }
    
        public void setLoc(Location loc) {
            this.loc = loc;
        }
    
        @Override
        public String toString() {
            return "ImageHolder{" +
                    "uri=" + uri +
                    ", date=" + date +
                    ", loc=" + loc +
                    '}';
        }
    }
    

    Create a Container class

    public class Container implements Serializable {
    
        private ArrayList list;
    
        public Container() {
        }
    
        public Container(ArrayList list) {
            this.list = list;
        }
    
        public ArrayList getList() {
            return list;
        }
    
        public void setList(ArrayList list) {
            this.list = list;
        }
    
        @Override
        public String toString() {
            return "Container{" +
                    "list=" + list +
                    '}';
        }
    }
    

    Your First Activity

    public class MainActivity extends AppCompatActivity {
    
        public static final String TAG = MainActivity.class.getSimpleName();
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main_activity);
        }
    
        @Override
        protected void onPostCreate(@Nullable Bundle savedInstanceState) {
            super.onPostCreate(savedInstanceState);
    
            ImageHolder imageHolder0 = new ImageHolder("http://www.stackoverflow.com", new Date(), new Location("test0"));
            ImageHolder imageHolder1 = new ImageHolder("http://www.google.com", new Date(), new Location("test1"));
    
            ArrayList list = new ArrayList<>();
            list.add(imageHolder0);
            list.add(imageHolder1);
    
            Container container = new Container(list);
    
            Gson gson = new Gson();
            String json = gson.toJson(container);
    
            Intent intent = new Intent(MainActivity.this, TestActivity.class);
            intent.putExtra("yourKey", json);
    
            startActivity(intent);
        }
    }
    

    Your second activity

    public class TestActivity extends AppCompatActivity {
        public static final String TAG = TestActivity.class.getSimpleName();
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
        }
    
        @Override
        protected void onPostCreate(@Nullable Bundle savedInstanceState) {
            super.onPostCreate(savedInstanceState);
    
            Gson gson = new Gson();
            Container container = gson.fromJson(getIntent().getStringExtra("yourKey"), Container.class);
    
            Log.d(TAG, "OK?" + container.getList().toString());
    
            // Here convert String uri Fields to Uri Objects
            // Example:
            Uri uri = Uri.parse(container.getList().get(0).getUri());
        }
    }
    


    Logcat result: 08-01 11:10:22.097 6671-6671/it.darksurfer.english.template D/TestActivity: OK?[ImageHolder{uri=http://www.stackoverflow.com, date=Wed Aug 01 11:10:21 GMT+02:00 2018, loc=Location[test0 0,000000,0,000000 acc=??? t=?!? et=?!?]}, ImageHolder{uri=http://www.google.com, date=Wed Aug 01 11:10:21 GMT+02:00 2018, loc=Location[test1 0,000000,0,000000 acc=??? t=?!? et=?!?]}]



    Obviously you can put an undefined number of others ArrayList in the list of Container class!

提交回复
热议问题