2012-06-03 21 views
7

Ich habe eine Aktivität mit zwei Fragmenten: eine für das Anzeigen von Produkten in einer Gridview und die andere, um die Produkte anzuzeigen, die der Benutzer der Bestellung hinzufügt (ListFragment). Wenn der Benutzer auf ein Produkt in der Rasteransicht klickt, muss ich einen Dialog (DialogFragment) anzeigen, in dem ich die Menge des gewünschten Produkts erfrage. Wenn der Benutzer dann im Dialogfeld auf "Akzeptieren" klickt, möchte ich, dass das Produkt in ListFragment angezeigt wird.Kommunikation zwischen Fragmenten/Dialogen in Android

Zum einen muss ich das Objektprodukt an den Dialog übergeben, um seinen Namen als Titel des Dialogs anzuzeigen (zum Beispiel). Also, was ich tat, war es auf diese Weise weitergeben müssen:

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

Ich denke, dass es okey ist, korrigieren Sie mich, wenn ich falsch liege. Aber dann muss ich im OnClick-Ereignis der positiven Schaltfläche die im Dialog eingegebene Menge abrufen und sie dann an das andere Fragment (das ListFragment) übergeben, und dann sollte es sofort in der Liste angezeigt werden.

Wie könnte ich das tun? eine Schnittstelle, und dann von der Aktivität auf das Fragment

Vielen Dank im Voraus

Antwort

10

Die recommended approach ist vom DialogFragment zur Aktivität zu kommunizieren.

In Ihrer Tätigkeit:

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

Dann wird die DialogFragment innere Klasse

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

Die XML für R.layout.alert_dialog_text_entry vom API Demos ist. Es passt nicht zu Ihrem Anwendungsfall, eine Menge vom Benutzer zu erhalten, aber es illustriert die Verwendung eines benutzerdefinierten Layouts, um einen Wert vom Benutzer zu erhalten.

<?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> 
+1

Verursacht zyklische Vererbung? –

+0

Ich glaube stark, dass diese Antwort mehr über die Kommunikation zwischen einem Fragment '(DialogFragment)' und einer Aktivität '(Main)' erklärt. Bitte überprüfen Sie diesen Thread mit einer ähnlichen Frage: https://stackoverflow.com/q/18561119/3987745 –

Verwandte Themen