无法从对话框片段调用父片段方法

时间:2014-03-19 16:27:21

标签: android android-fragments

如果这个问题得到了回答,请道歉,但过去两天我一直在寻找不同的事情,并且无法修复我的代码。

我正在创建一个AlertDialog单选(单选按钮)。我的对话框标题和文本是根据用户从父FragmentActivity点击的按钮动态创建的。然后,如果用户单击肯定按钮(在我的情况下,保存),我希望将与其选择对应的字符串传递回父FragmentActivity,以显示在相应的{ {1}}。

这是我的TextView代码:

DialogFragment

我已经表明了我认为不符合评论的package com.myDummyProject.FoodSelector; import java.util.Arrays; import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; import android.content.DialogInterface; import android.os.Bundle; import android.support.v4.app.DialogFragment; import android.widget.Toast; public class DummyDialogFragment extends DialogFragment { public interface selectChoiceDialogListener { public void onDialogPositiveClick(DialogFragment dialog,String dialogSubject,CharSequence selectedChoice); } selectChoiceDialogListener clickListener; public static CharSequence[] radioOptions; public static CharSequence selectedChoice = new String(); public static String dialogSubject = new String(); public static String dialogTitle = new String(); public static CharSequence defaultChoice = new String(); public DummyDialogFragment(){ } @Override public Dialog onCreateDialog(Bundle savedInstanceState) { // Retrieve values from bundle radioOptions = getArguments().getCharSequenceArray("DIALOG_radioOptions"); dialogSubject = getArguments().getString("CHOICE_TYPE"); dialogTitle = getArguments().getString("DIALOG_TITLE"); defaultChoice = getArguments().getString("DEFAULT_CHOICE"); int defaultValue = Arrays.asList(radioOptions).indexOf(defaultChoice); selectedChoice = defaultChoice; // Build the dialog AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setTitle(dialogTitle) .setSingleChoiceItems(radioOptions, defaultValue, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { selectedChoice = radioOptions[which]; } }) .setPositiveButton(R.string.action_save_allcaps, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { // Save selectedChoice; will be retrieved and showed in TextView of previous activity // ==> THIS DOESN'T SEEM TO BE WORKING <== clickListener.onDialogPositiveClick(DummyDialogFragment.this,dialogSubject,selectedChoice); CustomDialogListener myParentFragment = (CustomDialogListener) getFragmentManager().findFragmentByTag("myParentFragmentTag"); // ERROR: "CustomDialogListener cannot be resolved to a type" myParentFragment.refreshView(); // Toast informing user that choice was saved CharSequence toastMessage = dialogSubject + " saved as " + selectedChoice; Toast.makeText(getActivity(),toastMessage, Toast.LENGTH_SHORT).show(); // TODO: Return to previous activity -> make sure new values appear in the activity } }) .setNegativeButton(R.string.action_cancel_allcaps, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { // Toast informing user that choice was cancelled CharSequence toastMessage = dialogSubject + " change was cancelled"; Toast.makeText(getActivity(),toastMessage, Toast.LENGTH_SHORT).show(); } }); // Create the dialog return builder.create(); } @Override public void onAttach(Activity activity) { super.onAttach(activity); clickListener = (selectChoiceDialogListener) activity; } } 。 这是我的父FragmentActivity代码:

//==> THIS DOESN'T SEEM TO BE WORKING <==

当我按下package com.myDummyProject.FoodSelector; import android.os.Bundle; import android.support.v4.app.DialogFragment; import android.support.v4.app.Fragment; import android.support.v4.app.NavUtils; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.TextView; import android.annotation.TargetApi; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.content.Intent; import android.os.Build; import android.support.v4.app.FragmentActivity; public class DummyParentFragment extends FragmentActivity implements DummyDialogFragment.selectChoiceDialogListener { (...) public void pickFruits (View view){ DialogFragment pickFoodsDialog = new DummyDialogFragment(); String dialogTitle = "Select a fruit:"; String dialogSubject = "Fruit"; CharSequence[] radioOptions = new CharSequence[]{"Apple","Banana","Strawberry"}; TextView tv_fruit = (TextView) findViewById(R.id.textView_tv_fruit); CharSequence defaultFruit = tv_fruit.getText(); Bundle extras = new Bundle(); extras.putCharSequenceArray("DIALOG_radioOptions", radioOptions); extras.putString("DIALOG_TITLE", dialogTitle); extras.putString("DIALOG_SUBJECT", dialogSubject); extras.putCharSequence("DEFAULT_CHOICE", defaultFruit); pickFoodsDialog.setArguments(extras); pickFoodsDialog.show(getSupportFragmentManager(),"pickFoodsDialog"); } public void selectDessert(View view){ DialogFragment pickFoodsDialog = new DummyDialogFragment(); String dialogTitle = "Select a dessert:"; String dialogSubject = "Dessert"; CharSequence[] radioOptions = new CharSequence[]{"Cheescake","Chocolate Cake","Ice Cream","Creme Brulee"}; TextView tv_dessert = (TextView) findViewById(R.id.textView_day); CharSequence defaultDayOfWeek = dayOfWeek.getText(); Bundle extras = new Bundle(); extras.putCharSequenceArray("DIALOG_radioOptions", radioOptions); extras.putString("DIALOG_TITLE", dialogTitle); extras.putString("DIALOG_SUBJECT", dialogSubject); extras.putCharSequence("DEFAULT_CHOICE", defaultDayOfWeek); pickFoodsDialog.setArguments(extras); pickFoodsDialog.show(getSupportFragmentManager(),"pickFoodsDialog"); } public void selectStarter (View view){ DialogFragment radioButtonsDialog = new DummyDialogFragment(); String dialogTitle = "Select a starter:"; String dialogSubject = "Starter"; CharSequence[] radioOptions = new CharSequence[]{"Soup of the day","Garlic Bread","Prawns with garlic sauce","Mussels in white wine"}; TextView reminder = (TextView) findViewById(R.id.textView_reminder); CharSequence defaultReminder = reminder.getText(); Bundle extras = new Bundle(); extras.putCharSequenceArray("DIALOG_radioOptions", radioOptions); extras.putString("DIALOG_TITLE", dialogTitle); extras.putString("DIALOG_SUBJECT", dialogSubject); extras.putCharSequence("DEFAULT_CHOICE", defaultReminder); radioButtonsDialog.setArguments(extras); radioButtonsDialog.show(getSupportFragmentManager(),"pickFoodsDialog"); } public void onDialogPositiveClick(DialogFragment dialog, String dialogSubject, CharSequence selectedChoice) { // User touched the dialog's positive button TextView tv = new TextView(this); if(dialogSubject.equals("Fruit")){ tv = (TextView)findViewById(R.id.textView_tv_fruit); }else if (dialogSubject.equals("Dessert")){ tv = (TextView)findViewById(R.id.textView_dessert); }else if (dialogSubject.equals("Starter")){ tv = (TextView)findViewById(R.id.textView_starter); } tv.setText(selectedChoice); } public void refreshView(){ FragmentManager mFragmentManager = new getFragmentManager(); // ERROR: "getFragmentManager cannot be resolved to a type" Fragment currentFragment = getActivity().getFragmentManager().findFragmentByTag("myParentFragmentTag"); // ERROR: "The method getActivity() is undefined for the type AddModule" FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction(); fragmentTransaction.detach(currentFragment); fragmentTransaction.attach(currentFragment); fragmentTransaction.commit(); } } 按钮时,toast消息显示正常,然后对话框关闭,但我认为方法SAVE未在父onDialogPositiveClick中调用,因为FragmentActivity永远不会改变。我认为这将由TextViews管理。

1 个答案:

答案 0 :(得分:0)

道歉...使用dialogSubject.equals("fruit")来比较字符串。

您的新内容未显示在父片段中的原因是该片段需要刷新。

只需将其添加到对话框的onClick方法(setPositiveButton)中

即可
CustomDialogListener myParentFragment = (CustomDialogListener) getFragmentManager().findFragmentByTag("myParentFragmentTag");
myParentFragment.refreshView();

并在父片段中添加以下方法: (这将刷新你的父片段。它将删除并将片段添加到视图中 - 这就是全部。标签(myParentFragmentTag)是识别相应片段所必需的。

public void refreshView() {
    Fragment currentFragment = getActivity().getFragmentManager().findFragmentByTag("myParentFragmentTag");
    FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
    fragmentTransaction.detach(currentFragment);
    fragmentTransaction.attach(currentFragment);
    fragmentTransaction.commit();
}

我猜你的MainActivity拥有你的父片段。 你会发现类似......的代码:

ParentFragment myParentFragment = new ParentFragment();

FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.drawer_main_content_frame, myParentFragment, "myParentFragmentTag");
fragmentTransaction.commit();

关于这一点的重要部分是命名片段。这发生在replace() - &gt; “” myParentFragmentTag”。

我希望我的答案现在更容易理解:) 如果你还在努力,请告诉我。