我的GridView适配器中有frames.add(new TextureRegion(atlas.findRegion("PriestOverworld3"), i * 16, 0, 16, 16));
,我需要将此列表发送到另一个片段,我该怎么做?因为我不能通过捆绑发送它。
答案 0 :(得分:1)
ArrayList<arrayList > arrayList = new ArrayList<>();
将arrayList传递给bundle
Bundle bundle = new Bundle();
bundle.putSerializable("key", arrayList);
和接收片段
ArrayList<arrayList > arrayList = (ArrayList<arrayList>)getArguments().getSerializable("key");
答案 1 :(得分:1)
假设您的arraylist属于模型类Sample.Then模型类应该像这样实现Parcelable类
public class Sample implements Parcelable {
private String name;
private int value;
protected Sample(Parcel in) {
name = in.readString();
value = in.readInt();
}
public static final Creator<Sample> CREATOR = new Creator<Sample>() {
@Override
public Sample createFromParcel(Parcel in) {
return new Sample(in);
}
@Override
public Sample[] newArray(int size) {
return new Sample[size];
}
};
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeString(name);
parcel.writeInt(value);
}}
然后将数据从一个类发送到另一个类
ArrayList<Sample>arrayList=new ArrayList<>();
Bundle bundle = new Bundle();
bundle.putParcelableArrayList("key", arrayList);
然后在另一节课中获取数据
ArrayList<arrayList>arrayList=(ArrayList<Sample>)getArguments().putParcelableArrayList("key");
答案 2 :(得分:1)
您可以使用Instance类在整个应用程序中传递数据
public class RuntimeData {
private Context context;
private List<PoJo> questionHolders = new ArrayList<>();
private RuntimeData(Context context) {
this.context = context;
}
private static RuntimeData instance;
public static void init(Context context) {
if (instance == null) {
instance = new RuntimeData(context);
}
}
public static RuntimeData getInstance() {
return instance;
}
public void setQuestionHolders(List<PoJo> questionHolders) {
this.questionHolders = questionHolders;
}
public List<PoJo> getQuestionHolders() {
return questionHolders;
}
}
Pojo.class
public class PoJo {
private String name;
private setName(String name) {
this.context = name;
}
public String getName()
{
return name;
}
}
对于设定值:
RuntimeData.getInstance().setQuestionHolders(<here your arraylist>);
获取价值:
RuntimeData.getInstance().getQuestionHolders();