ng2b30
ng2b30

Reputation: 351

Pass data from activity to dialogFragment in android

This is the method that I send the data from activity to dialog 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");

This is the method that I get back the data.

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

I find that the data that I pass is right. However, I cannot get back the data in the fragment. Can anyoun help me to figure out the problem? Thank you very much.

Update:

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

}

message from my 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

Upvotes: 2

Views: 5618

Answers (1)

Atef Hares
Atef Hares

Reputation: 4881

Since product_data_two.getColor() returns an ArrayList<ArrayList<String>> , you can add it to bundle like this:

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

And in your Fragment, get it like this:

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

read more: Bundle

Upvotes: 3

Related Questions