Passing data through intent using Serializable

前端 未结 11 1508
借酒劲吻你
借酒劲吻你 2020-11-22 13:35

I\'ve implemented my class with serializable, but it still didn\'t work.

This is my class:

package com.ursabyte.thumbnail;

import java.io.Serializab         


        
相关标签:
11条回答
  • 2020-11-22 13:42

    I use the following method when sending a List<MySerializableObject> via intent:

    List<Thumbnail> thumbList = new ArrayList<>();
    //Populate ...
    
    Intent intent = new Intent(context, OtherClass.class);
    intent.putExtra("ThumbArray", thumbList.toArray(new Thumbnail[0]));
    //Send intent...
    

    And retrieving it like so:

    Thumbnail[] thumbArr = (Thumbnail[]) getIntent().getSerializableExtra("ThumbArray");
    if (thumbArr != null) {
        List<Thumbnail> thumbList = Arrays.asList(thumbArr);
    }
    
    0 讨论(0)
  • 2020-11-22 13:43
    Intent intent = new Intent(getApplicationContext(),SomeClass.class);
    intent.putExtra("value",all_thumbs);
    startActivity(intent);
    

    In SomeClass.java

    Bundle b = getIntent().getExtras();
    if(b != null)
    thumbs = (List<Thumbnail>) b.getSerializable("value");
    
    0 讨论(0)
  • 2020-11-22 13:45

    Don't forget to implement Serializable in every class your object will use like a list of objects. Else your app will crash.

    Example:

    public class City implements Serializable {
    
    private List<House> house;
    
    public List<House> getHouse() {
        return house;
    }
    
    public void setHouse(List<House> house) {
        this.house = house;
    }}
    

    Then House needs to implements Serializable as so :

    public class House implements Serializable {
    
    private String name;
    
    public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name = name;
    }}
    

    Then you can use:

    Bundle bundle = new Bundle();
    bundle.putSerializable("city", city);
    intent.putExtras(bundle);
    

    And retreive it with:

    Intent intent = this.getIntent();
    Bundle bundle = intent.getExtras();
    City city =  (City)bundle.getSerializable("city");
    
    0 讨论(0)
  • 2020-11-22 13:48

    Try to pass the serializable list using Bundle.Serializable:

    Bundle bundle = new Bundle();
    bundle.putSerializable("value", all_thumbs);
    intent.putExtras(bundle);
    

    And in SomeClass Activity get it as:

    Intent intent = this.getIntent();
    Bundle bundle = intent.getExtras();
    
    List<Thumbnail> thumbs=
                   (List<Thumbnail>)bundle.getSerializable("value");
    
    0 讨论(0)
  • 2020-11-22 13:50

    1- You are using android.graphics.Bitmap which doesn't implements Serializable class so you have to remove that class then it will work.

    2- for brief you can visit how to pass data between intents.

    0 讨论(0)
  • 2020-11-22 13:56

    This code may help you:

    public class EN implements Serializable {
    //... you don't need implement any methods when you implements Serializable
    }
    

    Putting data. Create new Activity with extra:

    EN enumb = new EN();
    Intent intent = new Intent(getActivity(), NewActivity.class);
    intent.putExtra("en", enumb); //second param is Serializable
    startActivity(intent);
    

    Obtaining data from new activity:

    public class NewActivity extends Activity {
    
        private EN en;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            try {
                super.onCreate(savedInstanceState);
    
                Bundle extras = getIntent().getExtras();
                if (extras != null) {
                    en = (EN)getIntent().getSerializableExtra("en"); //Obtaining data 
                }
    //...
    
    0 讨论(0)
提交回复
热议问题