我的目標是在單擊評分欄後,將此評分欄的值傳遞給顯示新評分欄的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片段生命週期,但我不知道在哪裏......謝謝!
我想,你要檢索的參數如下 '捆綁ARGS = getArguments();' 然後你就可以做 ' args.getFloat(「ratingValue」);' – Guillaume 2014-08-29 14:45:52
他已經這麼做了。 – r2DoesInc 2014-08-29 15:02:36