2014-03-19 94 views
-1

如果此問題已得到解答,但是我一直在四處尋找過去兩天的不同事情,並且無法修復我的代碼。無法從對話框片段中調用父級片段方法

我正在創建一個帶有單選按鈕(單選按鈕)的AlertDialog。我的對話標題和文本是根據用戶從父母FragmentActivity中點擊的按鈕動態創建的。然後,如果用戶點擊肯定按鈕(在我的情況下,保存),我希望將與他的選擇對應的字符串傳回給父項FragmentActivity,以顯示在相應的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; 
    } 

} 

我已經表明我認爲不與評論//==> THIS DOESN'T SEEM TO BE WORKING <==工作。 這是我父FragmentActivity代碼:

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

} 

當我按下SAVE按鈕,敬酒的消息顯示細膩,然後對話框關閉,但是,我認爲,該方法onDialogPositiveClick不被稱爲父FragmentActivity ,因爲TextViews從不改變。我認爲這將由clickListener管理。

+0

改變這個'TextView的電視=新的TextView。 (this);'to'TextView tv' – Raghunandan

+0

'dialogSubject ==「fruit」'不起作用,這不是你在Java中比較字符串的方式,2秒的調試會告訴你這樣的結果 – njzk2

+0

@Raghunandan:雖然我同意這個實例是無用的,刪除它不會解決任何問題。 – njzk2

回答

0

我的歉意...使用dialogSubject.equals("fruit")來比較字符串。

您的新內容未顯示在您的父片段中的問題是該片段需要刷新。

只是你的對話框

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

onClick方法(setPositiveButton)內添加這和你的父母片段添加下面的方法: (這將refesh父片段它將刪除和添加片段的觀點 - 這是所有標籤(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(); 

有關的重要組成部分,是來命名的片段。這種情況發生在替換() - >「」 myParentFragmentTag」

我希望我的回答是,現在更容易理解:) 讓我知道你是否仍然與奮鬥

+0

如果我創建一個自定義對話框監聽器,對我的'clickListener'不是多餘的?另外,不幸的是,因爲我是新手這樣做,我不怎麼實現:-(至於字符串比較,爲什麼'包含'而不是'等於'?(我試過了,無論如何,它不起作用) – PrincessLilly

+0

我不想濫用你的耐心與基本問題(你可能已經意識到我是一個初學者:-)),但這些是我的困難與您提出的解決方案:第1部分CustomDialogListener - 我想這是一個我不得不創建聽衆,因爲這不是一個類型的解決方案,我找不到任何文獻?第2部分)檢查我的更新代碼上面的錯誤;第3部分)我無法在我的主要活動中找到這樣的代碼,也無法在parentFragment中找到這樣的代碼...我需要在哪裏添加它?非常感謝! – PrincessLilly

+0

我發現我的原始實現[這裏](http://developer.android.com/guide/components/fragments.html#CommunicatingWithActivity)的源代碼,並沒有提到必須刷新視圖。但也許這是需要在我的情況下,因爲我試圖把數據放在TextView中? – PrincessLilly