Link between fragments / dialogs in android

I have an activity with two fragments: one for displaying products in a gridview, and the other for displaying products that the user adds to the order (ListFragment). When the user clicks on the product in the form of a grid, I need to display a dialog box (DialogFragment), in which I request the quantity of the required product. Then, when the user clicked the accept button in the dialog box, I want the product to appear in the list.

On the one hand, I have to pass the object-object into the dialog in order to show its name as the title of the dialog (for example). So, I did it like this:

public static class ProductDialog extends DialogFragment { static ProductDialog newInstance(ProductVO product) { ProductDialog f = new ProductDialog(); Bundle args = new Bundle(); args.putSerializable("product", product); f.setArguments(args); return f; } @Override public Dialog onCreateDialog(Bundle savedInstanceState) { ProductVO product = (ProductVO) getArguments().getSerializable("product"); return new AlertDialog.Builder(getActivity()) .setIcon(R.drawable.ic_dialog_add) .setTitle(R.string.add_product) ... .setPositiveButton(R.string.accept, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { } } ) .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { } } ) .create(); } } 

I think this is okey, correct me if I am wrong. But then in the onClick event of the positive button, I have to get the amount entered in the dialog box, and then transfer it to another fragment (ListFragment), and then it should be displayed in the list instantly.

How can i do this?

Thanks in advance

+7
source share
1 answer

The recommended approach is to exchange data from the Dialog dialog box to Activity using the interface, and then from Activity to Slice.

In your activity:

 public class Main extends FragmentActivity implements OnQuantitySelectedListener { public interface OnQuantitySelectedListener { void onFinishEditDialog(String inputText); } @Override public void onFinishEditDialog(String inputText) { Toast.makeText(this, "Quantity: " + inputText, Toast.LENGTH_SHORT).show(); } } 

Then inner class DialogFragment

 public static class ProductDialog extends DialogFragment { static ProductDialog newInstance(ProductVO product) { ProductDialog f = new ProductDialog(); Bundle args = new Bundle(); args.putSerializable("product", product); f.setArguments(args); return f; } @Override public Dialog onCreateDialog(Bundle savedInstanceState) { ProductVO product = (ProductVO) getArguments().getSerializable("product"); LayoutInflater factory = LayoutInflater.from(getActivity()); final View textEntryView = factory.inflate(R.layout.alert_dialog_text_entry, null); mEditText = (EditText) textEntryView.findViewById(R.id.txt_your_name); return new AlertDialog.Builder(getActivity()) .setIcon(R.drawable.ic_dialog_add) .setTitle(R.string.add_product) .setView(textEntryView) .setPositiveButton(R.string.accept, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { OnQuantitySelectedListener listener = (OnQuantitySelectedListener) getActivity(); listener.onFinishEditDialog(mEditText.getText().toString()); } } ) .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { } } ) .create(); } } 

XML for R.layout.alert_dialog_text_entry from the Demos API . It is not suitable for your use to get the quantity from the user, but it illustrates the use of the user layout to get the value from the user.

 <?xml version="1.0" encoding="utf-8"?> <!-- Copyright (C) 2008 The Android Open Source Project Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/username_view" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_marginLeft="20dip" android:layout_marginRight="20dip" android:text="@string/alert_dialog_username" android:gravity="left" android:textAppearance="?android:attr/textAppearanceMedium" /> <EditText android:id="@+id/username_edit" android:layout_height="wrap_content" android:layout_width="match_parent" android:layout_marginLeft="20dip" android:layout_marginRight="20dip" android:scrollHorizontally="true" android:autoText="false" android:capitalize="none" android:gravity="fill_horizontal" android:textAppearance="?android:attr/textAppearanceMedium" /> <TextView android:id="@+id/password_view" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_marginLeft="20dip" android:layout_marginRight="20dip" android:text="@string/alert_dialog_password" android:gravity="left" android:textAppearance="?android:attr/textAppearanceMedium" /> <EditText android:id="@+id/password_edit" android:layout_height="wrap_content" android:layout_width="match_parent" android:layout_marginLeft="20dip" android:layout_marginRight="20dip" android:scrollHorizontally="true" android:autoText="false" android:capitalize="none" android:gravity="fill_horizontal" android:password="true" android:textAppearance="?android:attr/textAppearanceMedium" /> </LinearLayout> 
+10
source

All Articles