将数据从 activity 传递到 android 中的 dialogFragment

Pass data from activity to dialogFragment in android

这是我将数据从activity发送到对话片段

的方法
 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 color set size: 0
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean > > java.util.ArrayList.add(java.lang.Object)' on a null object reference

因为 product_data_two.getColor() returns 一个 ArrayList<ArrayList<String>> ,你可以把它添加到 bundle 像这样:

bundle.putSerializable("SOME_KEY",product_data_two.getColor());

在你的 Fragment 中,像这样获取它:

getArgument = (ArrayList<ArrayList<String>>) bundle.getSerializable("SOME_KEY");

阅读更多:Bundle

首先将颜色添加到单独的 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");

然后在您的 Fragment 中按键获取整个 ArrayList 如下所示

ArrayList<String> colorArray =getArguments().getStringArrayList("colorArray");