android - 在android中将数据从activity传递到dialogFragment

标签 android arguments parameter-passing android-dialogfragment

这是我将数据从 Activity 发送到对话框 fragment 的方法

 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;
}

我发现我传递的数据是正确的。但是,我无法取回 fragment 中的数据。任何人都可以帮我解决这个问题吗?非常感谢。

更新:

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;
}

}

来 self 的 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()返回 ArrayList<ArrayList<String>> ,您可以将其添加到 bundle像这样:

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

在你的 Fragment 中,像这样得到:

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

了解更多:Bundle

关于android - 在android中将数据从activity传递到dialogFragment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43137805/

相关文章:

android - 带有 fragment 导航组件的底部导航 View 设置不起作用

javascript - 在 Javascript 中的参数中分割数组

iphone - 我如何 "connect"到 View Controller 的 TableView

php - Python 脚本中关于 str(sys.argv[1]) 的错误

android - Android ListView 项的滑入按钮效果

android - UI 自动转储 "Killed"

android - 在 ConstraintLayout 中对齐 View ,无需裁剪

python - 是否可以使用未指定数量的参数的 python 函数?

jenkins - 如何在build.gradle文件中定义变量,其值由jenkins动态传递

c++ - Const 指针的问题