问题描述
我正在尝试实例化一个片段,以发送"一个自定义对象数组列表,但是我找不到执行此操作的确切方法.我想我可能必须使用Serializable
或Parcelable
(从另一个答案中可以看出),但是即使在阅读了该答案之后,我也不确定如何实现它.
I'm trying to instantiate a fragment "sending" a custom object array list, but I can't find the exact way to do it. I think I may have to use Serializable
or Parcelable
(as seen in another answer), but I'm not sure how to implement it even after reading that answer.
有人可以帮助我吗?预先感谢.
Can someone help me? Thanks in advance.
要实例化的代码
Fragment f = new Fragment();
ArrayList<IconsCategory> category = Utils.getCategory();
ArrayList<IconItem> icons = category.getIconsArray();
f = IconsFragment.newInstance(icons);
return f;
片段中的代码以创建实例:我知道args.putArrayList()
方法不存在,我只是写了它来表明我希望能够对其进行编码.
Code in fragment to create the instance:I know args.putArrayList()
method doesn't exist, I just wrote it to show how I expect to be able to code it.
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类:
IconsCategory class:
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类:
IconItem class:
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;
}
}
推荐答案
您应该避免使用Serializable,实现Parcelable是Android上的首选路由( http://developer.android.com/reference/android/os/Parcelable.html ):
You should avoid using Serializable, implementing Parcelable is is the preferred route on Android (http://developer.android.com/reference/android/os/Parcelable.html):
IconItem.java
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
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
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;
}
这篇关于使用自定义对象数组列表实例化片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!