使用自定义对象数组列表实例化片段

时间:2016-02-06 23:11:03

标签: java android android-fragments arraylist instance

我正在尝试实例化一个“发送”自定义对象数组列表的片段,但我找不到确切的方法来执行它。我想我可能不得不使用SerializableParcelable(如另一个答案中所见),但即使在阅读完答案之后我也不确定如何实现它。

有人能帮助我吗?提前谢谢。

实例化的代码

Fragment f = new Fragment();

ArrayList<IconsCategory> category = Utils.getCategory();
ArrayList<IconItem> icons = category.getIconsArray();

f = IconsFragment.newInstance(icons);

return f;

用于创建实例的片段中的代码: 我知道args.putArrayList()方法不存在,我只是写它来表明我希望能够编码它。

public static IconsFragment newInstance(ArrayList<IconItem> list) {
    IconsFragment fragment = new IconsFragment();
    Bundle args = new Bundle();
    args.putArrayList(list);   // here's where the code is supposed to go 
    fragment.setArguments(args);
    return fragment;
}

IconsCategory类:

import java.util.ArrayList;

public class IconsCategory {

    private String name;
    private ArrayList<IconItem> iconsArray = new ArrayList<>();

    public IconsCategory(String name, ArrayList<IconItem> iconsArray) {
        this.name = name;
        this.iconsArray = iconsArray;
    }

    public String getCategoryName() {
        return this.name;
    }

    public ArrayList<IconItem> getIconsArray() {
        return iconsArray.size() > 0 ? this.iconsArray : null;
    }

}

IconItem类:

public class IconItem {

    private String name;
    private int resId;

    public IconItem(String name, int resId){
        this.name = name;
        this.resId = resId;
    }

    public String getName(){
        return this.name;
    }

    public int getResId(){
        return this.resId;
    }

}

4 个答案:

答案 0 :(得分:3)

你应该避免使用Serializable,实现Parcelable是Android上的首选路由(http://developer.android.com/reference/android/os/Parcelable.html):

IconItem.java

import android.os.Parcel;
import android.os.Parcelable;

public class IconItem implements Parcelable {

    private String name;
    private int resId;

    public IconItem(String name, int resId) {
        this.name = name;
        this.resId = resId;
    }

    public String getName() {
        return this.name;
    }

    public int getResId() {
        return this.resId;
    }

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

    @Override public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(name);
        dest.writeInt(resId);
    }

    public static final Creator<IconItem> CREATOR = new Creator<IconItem>() {
        @Override
        public IconItem createFromParcel(Parcel in) {
            String name = in.readString();
            int redId = in.readInt();
            return new IconItem(name, redId);
        }

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

IconsCategory.java

import android.os.Parcel;
import android.os.Parcelable;

import java.util.ArrayList;

public class IconsCategory implements Parcelable {

    private String name;
    private ArrayList<IconItem> iconsArray = new ArrayList<>();

    public IconsCategory(String name, ArrayList<IconItem> iconsArray) {
        this.name = name;
        this.iconsArray = iconsArray;
    }


    public static final Creator<IconsCategory> CREATOR = new Creator<IconsCategory>() {
        @Override
        public IconsCategory createFromParcel(Parcel in) {
            String name = in.readString();
            ArrayList<IconItem> icons = new ArrayList<>();
            in.readTypedList(icons, IconItem.CREATOR);
            return new IconsCategory(name, icons);
        }

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

    public String getCategoryName() {
        return this.name;
    }

    public ArrayList<IconItem> getIconsArray() {
        return iconsArray.size() > 0 ? this.iconsArray : null;
    }

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

    @Override public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(name);
        dest.writeTypedList(iconsArray);
    }
}

IconsFragment.java

public static IconsFragment newInstance(IconsCategory category) {
    IconsFragment fragment = new IconsFragment();
    Bundle args = new Bundle();
    args.putParcelable("category", category); 
    fragment.setArguments(args);
    return fragment;
}

答案 1 :(得分:1)

在IconItem类中实现Serializable:

import java.io.Serializable;

public class IconItem implements Serializable {

    private String name;
    private int resId;

    public IconItem(String name, int resId){
        this.name = name;
        this.resId = resId;
    }

    public String getName(){
        return this.name;
    }

    public int getResId(){
        return this.resId;
    }

}

然后

public class IconsFragment extends Fragment {
    public static final String KEY = "key";
    public static IconsFragment newInstance(ArrayList<IconItem> list) {
        IconsFragment fragment = new IconsFragment();
        Bundle args = new Bundle();
        args.putSerializable(KEY, list);
        fragment.setArguments(args);
        return fragment;
    }
}

ArrayList默认实现Serializable,因此您只需要在类中实现它。

答案 2 :(得分:0)

另一个选项是创建一个Listener接口,然后只需“监听”Fragment onCreateonResumeGetLogListener中的最新列表。

以下是我的实施示例,如果您需要帮助,请与我联系:https://goo.gl/gNJFTV

请注意MainActivityDailyViewFragment

中{{1}}的实施情况

答案 3 :(得分:-2)

这将创建新的Fragment并允许您在创建Fragment时传入ArrayList。有了它,你不必让所有东西都可以使用。如果你想能够来回传递你的POJO,那么使用Parcelable作为Serializable并不是一个有用的,因为你得到更复杂的POJO结构。 Parcelable也有更好的表现。

public IconsFragment extends Fragment {

    private ArrayList<IconItem> list;

    /** Blank constructor */
    public IconsFragment() {}

    public static IconsFragment createFragment(@Nullable ArrayList<IconItem> list) {
        IconsFragment frag = new IconsFragment();
        frag.init(list);
        return frag;
    }

    public void init(ArrayList<IconItem> list) {
        if (list==null) {
            this.list = new ArrayList<>(0);
        } else {
            // This creates a new list coping your values into the new array
            this.list = new ArrayList<>(list);
            // Or this just uses your current list.
            // JUST PICK ON OR THE OTHER.
            this.list = list;
        }
    }
}