Question

Can I pass a Vector to a DialogFragment as a serializable, in the follows way?


class Foo {}

class MyDialogFragment extends DialogFragment {

    static MyDialogFragment newInstance(Vectorvec) {
        MyDialogFragment f = new MyDialogFragment();

        Bundle args = new Bundle();
        args.putSerializable("vec", vec);
        f.setArguments(args);

        return f;
    }
}

If no, how i can do it?

Was it helpful?

Solution

Yes, You can do it using Serializable.

I would suggest to check Android way: it provides faster and more convenient Parcelable interface. Here's explanation on why it's better to use Parcelable.

So, it might look the following way:

class MyVectorItem implements Parcelable {

    private final String mName;

    public MyVectorItem(final String name) {
        mName = name;
    }

    public MyVectorItem(final Parcel in) {
        mName = in.readString();
    }

    @Override
    public int describeContents() {
        return hashCode();
    }

    @Override
    public String toString() {
        return "Item[" + mName + "]";
    }

    @Override
    public void writeToParcel(final Parcel dest, final int flags) {
        dest.writeString(mName);
    }

    public static final Parcelable.Creator<MyVectorItem> CREATOR
            = new Parcelable.Creator<MyVectorItem>() {
        public MyVectorItem createFromParcel(Parcel in) {
            return new MyVectorItem(in);
        }

        public MyVectorItem[] newArray(int size) {
            return new MyVectorItem[size];
        }
    };



}

class MyDialogFragment extends DialogFragment {

    static MyDialogFragment newInstance(Vector<MyVectorItem> vec) {
        MyDialogFragment f = new MyDialogFragment();

        Bundle args = new Bundle();
        args.putParcelableArray("vec", vec.toArray(new MyVectorItem[1]));
        f.setArguments(args);

        return f;
    }

    @Override
    public Dialog onCreateDialog(final Bundle savedInstanceState) {
        MyVectorItem[] items = (MyVectorItem[]) getArguments().getParcelableArray("vec");

        return new AlertDialog.Builder(getActivity())
                .setTitle(items.length + " items")
                .setMessage("Items are " + Arrays.toString(items))
                .setPositiveButton("OK",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int whichButton) {
                                // nothing to do
                            }
                        }
                )
                .setNegativeButton("Cancel",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int whichButton) {
                                // nothing to do
                            }
                        }
                )
                .create();
    }
}

And dialog showing is:

final Vector<MyVectorItem> vec = new Vector<MyVectorItem>();

vec.add(new MyVectorItem("name 1"));
vec.add(new MyVectorItem("name 2"));
vec.add(new MyVectorItem("name 3"));
vec.add(new MyVectorItem("name 4"));

MyDialogFragment.newInstance(vec).show(getFragmentManager(), "mydialog");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top