Write a program in C language to find a 1! +2! +3! +4! … (two methods)

2023-01-11   ES  

PARCLABLE is a unique interface in the Android system. It is not like the Java language as the Serializable interface. Intent also uses the ParceLABLE mode to pass the object’s data, but it is different from the Serializable method to serialize the object. Instead, it is decomposed by a complete object, and each part of the decomposition is the transmission data type supported by Intent.

Note that the custom class must be static, otherwise it will report an error if you change it. At present, I don’t know why, so it ’s right to use it ~

public static class Person implements Parcelable{
    

        private String name;
        private int age;

        public void Person(){
    }

        protected Person(Parcel in) {
    
            name = in.readString();
            age  = in.readInt();
        }

        public static final Creator<Person> CREATOR = new Creator<Person>() {
    
            @Override
            public Person createFromParcel(Parcel in) {
    
                return new Person(in);
            }

            @Override
            public Person[] newArray(int size) {
    
                return new Person[size];
            }
        };

        public Person() {
    

        }

        @Override
        public int describeContents() {
    
            return 0;
        }

        @Override
        public void writeToParcel(Parcel dest, int flags) {
    
            dest.writeString(name);
            dest.writeInt(age);
        }
        
        /*
        	Setter and Getter
        	*/
    }

Intent intent = new Intent(this, SecondActivity.class);
Bundle bundle = new Bundle();

Person person = new Person();
person.setAge(144);
person.setName("lzh");

bundle.putParcelable("data", person);
intent.putExtras(bundle);

startActivity(intent);

// Get data
Person person = getIntent().getParcelableExtra("data");

Taking the dishes as an example, it is different from the situation that there is no List type data above.

If the class in the list type is customized, the custom class needs to implement the PareLABLE interface, otherwise the Parcel: Unable to Marshal Value error will appear.

At the same time, you need to rewrite the CreateFromParcel method.

DishData.java

public class DishData implements Parcelable {
    
    public class DishData implements Parcelable {
    
    private int id;
    private String name;
    private String peoplenum;
    private String preparetime;
    private String cookingtime;
    private String content;
    private String pic;
    private String tag;
    private List<Material> material = new ArrayList<>();
    private List<Process> process = new ArrayList<>();

    public DishData(){
    }


    protected DishData(Parcel in) {
    
        id = in.readInt();
        name = in.readString();
        peoplenum = in.readString();
        preparetime = in.readString();
        cookingtime = in.readString();
        content = in.readString();
        pic = in.readString();
        tag = in.readString();
        material = in.readArrayList(getClass().getClassLoader());
        process = in.readArrayList(getClass().getClassLoader());
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
    
        dest.writeInt(id);
        dest.writeString(name);
        dest.writeString(peoplenum);
        dest.writeString(preparetime);
        dest.writeString(cookingtime);
        dest.writeString(content);
        dest.writeString(pic);
        dest.writeString(tag);
        dest.writeList(material);
        dest.writeList(process);
    }

    @Override
    public int describeContents() {
    
        return 0;
    }

    public static final Creator<DishData> CREATOR = new Creator<DishData>() {
    
        @Override
        public DishData createFromParcel(Parcel in) {
    
            DishData data = new DishData();
            data.id = in.readInt();
            data.name = in.readString();
            data.peoplenum = in.readString();
            data.preparetime = in.readString();
            data.cookingtime = in.readString();;
            data.content = in.readString();
            data.pic = in.readString();
            data.tag = in.readString();
            
            data.material = new ArrayList<>();
            in.readList(data.material, getClass().getClassLoader());

            data.process = new ArrayList<>();
            in.readList(data.process, getClass().getClassLoader());
            
            return data;
        }

        @Override
        public DishData[] newArray(int size) {
    
            return new DishData[size];
        }
    };

    /*
    	Setter and Getter
    	*/
}

Material.java

public class Material implements Parcelable {
    
    private String mname;   
    private String amount;  

    protected Material(Parcel in) {
    
        mname = in.readString();
        amount = in.readString();
    }

    public static final Creator<Material> CREATOR = new Creator<Material>() {
    
        @Override
        public Material createFromParcel(Parcel in) {
    
            return new Material(in);
        }

        @Override
        public Material[] newArray(int size) {
    
            return new Material[size];
        }
    };

    @Override
    public int describeContents() {
    
        return 0;
    }

    @Override
    public void writeToParcel(Parcel parcel, int i) {
    
        parcel.writeString(mname);
        parcel.writeString(amount);
    }
    
    /*
    	Setter and getter
   		*/
}

MainActivity transmission data

Intent intent = new Intent(getActivity(), DishActivity.class);
Bundle bundle = new Bundle();

DishData data = new DishData();

data.setId(id);
data.setName(name);
// ...

bundle.putParcelable("dishData", data);
intent.putExtras(bundle);

// Jump to Dishactivity
startActivity(intent);

Dishactivity get data

DishData dishData = getIntent().getParcelableExtra("dishData");
Log.i(TAG, "onCreate: material.size = "+dishData.getMaterial().size());

source

Related Posts

Use EXE4J to pack executable jar into exe files

numpy shaped operation changes the shape of the array

linux configuration web agent Luo Jun

maze problem [BFS] [Three -dimensional space]

Write a program in C language to find a 1! +2! +3! +4! … (two methods)

Random Posts

MATLAB (Fmincon): Warning: The matrix is close to the strange value, or the scaling error. The result may be inaccurate. RCond = 2.156571E-16.

jsp: usebean, jsp: setproperty Detailed explanation

[C ++ Thinking Maps and Code Example] 11 an exception processing mechanism

ESP32’s serial port communication (performed by interruption and looking at the door)

BTC Transaction rawtx analysis