如何将对话框中评级栏的评级从活动评级栏传递到评级栏

时间:2013-10-29 20:10:42

标签: android ratingbar

我想将评分 - 将由DIALOG评级栏上的用户设置 - 传递到活动的评级栏。 (我设置了2个xmls,有2个不同的评级栏)

你能帮忙(用代码)我能做到这一点吗? 提前谢谢。

public class RecipesDetails  extends Activity implements OnClickListener, OnRatingBarChangeListener{

    TextView rTitle, rType, rSubName, reviewAmount;
    Button aRating; 
    RatingBar getNewRating, setRatingBar;
    int count;
    float curRate;

    private static final int DIALOG_TEXT_ENTRY_RATE = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView (R.layout.recipes_detail);
        initialize();

        aRating.setOnClickListener(this);

        rTitle.setText(getIntent().getExtras().getString("rec_name"));
        rType.setText(getIntent().getExtras().getString("current_rating"));
        rSubName.setText(getIntent().getExtras().getString("sub_name"));

        setRatingBar.setRating(curRate);
        getNewRating.setOnRatingBarChangeListener(this);
    }

    private void initialize() {
        // TODO Auto-generated method stub
        rTitle = (TextView) findViewById(R.id.tvDetailRecipeTitle);
        rType = (TextView) findViewById(R.id.tv_detail_recipe_Rating);
        rSubName = (TextView) findViewById(R.id.tv_detail_recipe_SubmittedName);

        aRating = (Button) findViewById (R.id.bAddReview);

        getNewRating = (RatingBar) findViewById (R.id.detail_recipe_ratingBar);
        setRatingBar = (RatingBar) findViewById (R.id.dialogRatingBar);
        reviewAmount = (TextView) findViewById (R.id.tv_detail_recipe_Rating);

    }

    @SuppressWarnings("deprecation")
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch(v.getId()){
        case R.id.bAddReview:
        showDialog(DIALOG_TEXT_ENTRY_RATE);
            break;
        }

    }


    @Override
    public void onRatingChanged(RatingBar ratingBar, float rating,
            boolean fromUser) {
        // TODO Auto-generated method stub
        DecimalFormat decimalFormat = new DecimalFormat("#.#");
        curRate = Float.valueOf(decimalFormat.format((curRate * count + rating)
                / ++count));
        Toast.makeText(RecipesDetails.this,
                "New Rating: " + curRate, Toast.LENGTH_SHORT).show();
        setRatingBar.setRating(curRate);
        reviewAmount.setText(count + " Ratings");

    }

    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case DIALOG_TEXT_ENTRY_RATE:
            LayoutInflater acc = LayoutInflater.from(this);
            final View textEntryViewAcc = acc.inflate(R.layout.add_rating_dialog, null);
            final RatingBar rateME = (RatingBar) textEntryViewAcc.findViewById(R.id.detail_recipe_ratingBar);
            //final EditText amountofwater = (EditText) textEntryViewAcc.findViewById(R.id.etGlasesOfWater);
            return new AlertDialog.Builder(RecipesDetails.this)
                    //.setIconAttribute(android.R.attr.alertDialogIcon)
                    .setTitle(R.string.alert_dialog_add_review)
                    .setView(textEntryViewAcc)
                    .setPositiveButton(R.string.alert_dialog_submit,
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                        int whichButton) {

                                    /* User clicked OK so do some stuff */
                                }
                            })
                    .setNegativeButton(R.string.alert_dialog_cancel,
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                        int whichButton) {

                                    /* User clicked cancel so do some stuff */
                                }
                            })

                            .create();

        }
        return null;
        }

}

我需要移动onRatingChanged方法中的东西移动到正按钮部分的对话框,但不知道如何去做....

0 个答案:

没有答案