这是我将数据从活动发送到对话框片段
的方法 MultipleColorFragment multipleColorFragment = new MultipleColorFragment();//Get Fragment Instance
Bundle data = new Bundle();//Use bundle to pass data
for(int i = 0;i<product_data_two.getColor().size();i++) {
data.putStringArrayList(i + "", product_data_two.getColor().get(i));
Log.d(TAG + " send fragment data",data.toString());
}
data.putInt("size", product_data_two.getColor().size());
multipleColorFragment.setArguments(data);//Finally set argument bundle to fragment
final FragmentManager fm=getFragmentManager();
multipleColorFragment.show(fm,"Color Set");
这是我收回数据的方法。
RecyclerView rv;
ArrayList<ArrayList<String>> getArgument;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.multiple_color_fragment_layout, container);
//RECYCER
rv = (RecyclerView) rootView.findViewById(R.id.multiple_color_recyclerview);
rv.setLayoutManager(new LinearLayoutManager(this.getActivity()));
rv.setLayoutManager(new GridLayoutManager(getActivity(), 6));
rv.setAdapter(new MultipleColorAdapter());
int size = getArguments().getInt("size");
Log.d( " size : " + size ,"");
for (int i = 0; i < size; i++)
getArgument.add(getArguments().getStringArrayList(i + ""));
this.getDialog().setTitle("Color Set");
return rootView;
}
我发现我传递的数据是正确的。但是,我无法取回片段中的数据。任何人都能帮我解决问题吗?非常感谢你。
更新
ProductTypeTwo.java
public class ProductTypeTwo {
private String productName;
private String brandID;
private String description;
private String productImage;
private Long colorNo;
private String category;
private String uid;
private ArrayList<ArrayList<String>> color;
public ProductTypeTwo(String productName, String brandID, String description, String productImage, Long colorNo, String category, String uid, ArrayList<ArrayList<String>> color) {
this.productName = productName;
this.brandID = brandID;
this.description = description;
this.productImage = productImage;
this.colorNo = colorNo;
this.category = category;
this.uid = uid;
this.color = color;
}
public ProductTypeTwo()
{
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public String getBrandID() {
return brandID;
}
public void setBrandID(String brandID) {
this.brandID = brandID;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getProductImage() {
return productImage;
}
public void setProductImage(String productImage) {
this.productImage = productImage;
}
public Long getColorNo() {
return colorNo;
}
public void setColorNo(Long colorNo) {
this.colorNo = colorNo;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
public ArrayList<ArrayList<String>> getColor() {
return color;
}
public void setColor(ArrayList<ArrayList<String>> color) {
this.color = color;
}
}
来自我的logcat的消息:
D / MultipleColorFragment颜色集大小:0
java.lang.NullPointerException:尝试调用虚方法&#39; boolean&gt; &GT; java.util.ArrayList.add(java.lang.Object中)&#39;在空对象引用上
答案 0 :(得分:3)
由于product_data_two.getColor()
返回ArrayList<ArrayList<String>>
,您可以将其添加到bundle
,如下所示:
bundle.putSerializable("SOME_KEY",product_data_two.getColor());
在Fragment
中,请按照以下方式进行操作:
getArgument = (ArrayList<ArrayList<String>>) bundle.getSerializable("SOME_KEY");
了解详情:Bundle
答案 1 :(得分:0)
首先在单独的ArrayList中添加颜色,然后将其全部添加到要传递给Fragment的包中
MultipleColorFragment multipleColorFragment = new
MultipleColorFragment();//Get Fragment Instance
Bundle data = new Bundle();//Use bundle to pass data
ArrayList<String> colorArray = new ArrayList<>()
for(int i = 0; i<product_data_two.getColor().size(); i++) {
colorArray.add(product_data_two.getColor().get(i));
}
data.putStringArrayList("colorArray", colorArray);
data.putInt("size", product_data_two.getColor().size());
multipleColorFragment.setArguments(data);//Finally set argument bundle to fragment
final FragmentManager fm=getFragmentManager();
multipleColorFragment.show(fm,"Color Set");
在你的片段中,按键获取整个ArrayList,如下所示
ArrayList<String> colorArray =getArguments().getStringArrayList("colorArray");