2014-08-29 71 views
0

我的目標是在單擊評分欄後,將此評分欄的值傳遞給顯示新評分欄的DialogFragment。Android:將評分欄值傳遞給對話框

這是我的活動代碼(的onCreate)

ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() { 

     @Override 
     public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) { 
      if (fromUser){ 
      RateItemDialogFragment newFragment = new RateItemDialogFragment(); 

      Bundle bundle = new Bundle(); 
      bundle.putFloat("ratingValue", rating); 
      newFragment.setArguments(bundle); 
      newFragment.show(getSupportFragmentManager(), "rate menu item"); 

      } 
     } 
    }); 

這裏是我的DialogFragment

@Override 
public Dialog onCreateDialog(Bundle savedInstanceState) { 
    // Use the Builder class for convenient dialog construction 
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 

    // Get the layout inflater 
    LayoutInflater inflater = getActivity().getLayoutInflater(); 


    // Inflate and set the layout for the dialog 
    // Pass null as the parent view because its going in the dialog layout 
    builder.setView(inflater.inflate(R.layout.dialog_rate_item, null)) 

      .setMessage(R.string.title_rating_dialog) 
      .setPositiveButton(R.string.send, new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 
        // Send the positive button event back to the host activity 
        mListener.onDialogPositiveClick(RateItemDialogFragment.this); 
       } 
      }) 
      .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 
        // Send the negative button event back to the host activity 
        mListener.onDialogNegativeClick(RateItemDialogFragment.this); 
        // User cancelled the dialog 
        RateItemDialogFragment.this.getDialog().cancel(); 
       } 
      }) 
    ; 

    Dialog dialog = builder.create(); 

    // set the ratingBar to the right value 
    Bundle bundle = getArguments(); 
    ((RatingBar) dialog.findViewById(R.id.rating_bar_dialog)).setRating(bundle.getFloat("ratingValue")); 

    // Create the AlertDialog object and return it 
    return dialog; 
} 

這使我對

 ((RatingBar) dialog.findViewById(R.id.rating_bar_dialog)).setRating(bundle.getFloat("ratingValue")); 

我必須做一些錯誤的JavaNullPointerException片段生命週期,但我不知道在哪裏......謝謝!

+0

我想,你要檢索的參數如下 '捆綁ARGS = getArguments();' 然後你就可以做 ' args.getFloat(「ratingValue」);' – Guillaume 2014-08-29 14:45:52

+1

他已經這麼做了。 – r2DoesInc 2014-08-29 15:02:36

回答

0

您的看法可能不是對話框的孩子。

試試這個,在膨脹的對話框視圖中尋找你的視圖。 查看root = inflater.inflate(R.layout.dialog_rate_item,null); builder.setView(根)

  .setMessage(R.string.title_rating_dialog) 
      .setPositiveButton(R.string.send, new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 
        // Send the positive button event back to the host activity 
        mListener.onDialogPositiveClick(RateItemDialogFragment.this); 
       } 
      }) 
      .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 
        // Send the negative button event back to the host activity 
        mListener.onDialogNegativeClick(RateItemDialogFragment.this); 
        // User cancelled the dialog 
        RateItemDialogFragment.this.getDialog().cancel(); 
       } 
      }); 

然後

((RatingBar) root.findViewById(R.id.rating_bar_dialog)).setRating(bundle.getFloat("ratingValue")); 
+0

非常感謝!我在這裏學到了有價值的東西! – Stephane 2014-08-29 15:01:00