【问题标题】:Pass data from activity to dialogFragment in android将数据从活动传递到android中的dialogFragment
【发布时间】:2017-08-25 13:20:13
【问题描述】:

这是我将数据从活动发送到对话框片段的方法

 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:尝试在空对象引用上调用虚拟方法 'boolean >> java.util.ArrayList.add(java.lang.Object)'

【问题讨论】:

  • product_data_two.getColor()返回的List是什么类型的?
  • 发布您的 product_data_two 类及其子类(如果有)。你得到了什么 -> getArguments().getStringArrayList(i + "");空??
  • 请阅读以下内容:How to Ask & minimal reproducible example,以增加获得问题答案的机会!
  • D/MultipleColorFragment 颜色集大小:0 来自我的 logcat。
  • @Aishwarya Tiwari 是的。 java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“boolean java.util.ArrayList.add(java.lang.Object)”

标签: android arguments parameter-passing android-dialogfragment


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    由于 product_data_two.getColor() 返回 ArrayList&lt;ArrayList&lt;String&gt;&gt; ,您可以将其添加到 bundle ,如下所示:

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

    在你的Fragment 中,这样获取:

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

    阅读更多:Bundle

    【讨论】:

    • 是的,我也会推荐这个。
    • 我仍然收到此错误。尝试在空对象引用上调用虚拟方法“java.lang.String java.util.ArrayList.toString()”
    • 对不起。这次是我的错字。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多