如何显示三个等级栏的总和?

时间:2018-08-23 10:21:26

标签: android

我正在研究一个小型评级应用程序,我有三个评级栏。我想要,当我选择每个等级栏时,它会出现在EditText中,然后计算所有结果并将总和显示在TextView中。每次更改值时,总和都会更新。不幸的是,我无法显示金额。

有人可以帮我吗?

这是我的代码:

 ratingBarHeures.setOnRatingBarChangeListener(new 
    RatingBar.OnRatingBarChangeListener() {
        @Override
        public void onRatingChanged(RatingBar ratingBar, float rating, 
     boolean fromUser) {
            myResult.setText("Resultat : " + rating);
     }
    });
    ratingBar.setOnRatingBarChangeListener(new 
    RatingBar.OnRatingBarChangeListener() {
        @Override
        public void onRatingChanged(RatingBar ratingBar, float rating1, 
    boolean fromUser) {
            myResult1.setText("Resultat : " + rating1);
     }
    });

    ratingBarSerice.setOnRatingBarChangeListener(new 
    RatingBar.OnRatingBarChangeListener() {
        @Override
        public void onRatingChanged(RatingBar ratingBar, float rating2, 
    boolean fromUser) {
            String.valueOf(rating2);
            myResult2.setText("Resultat : " + rating2);
        }
    });

}
public void rateMe(View view){
    final String nbr_heure,donne,service;
    nbr_heure=String.valueOf(ratingBarHeures.getRating());
    donne = String.valueOf(ratingBar.getRating());
    service = String.valueOf(ratingBarSerice.getRating());
    StringRequest stringRequest =new StringRequest(Request.Method.POST, 
    service_url, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
  Toast.makeText(getApplication(),response,Toast.LENGTH_LONG).show();
            Log.d("Response", response);
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
   Toast.makeText(AvisActivity.this,error+"",Toast.LENGTH_LONG).show();
      }}){
      @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String,String> params = new HashMap<>();
            params.put("qualité", String.valueOf(donne));
            params.put("nbr_heure", nbr_heure);
            params.put("service",String.valueOf(service));
            return params;
        }
    };
    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);
    }

1 个答案:

答案 0 :(得分:1)

在每个评分栏的OnRatingBarChangeListener中,设置自己的TextView,并选择评分,并获得已在总评分TextView中设置的总评分,并添加新的评分并再次设置。假设您有一个总额TextView初始化为RatingSum并使用默认文本Resultat: 0;

ratingBarHeures.setOnRatingBarChangeListener(new 
    RatingBar.OnRatingBarChangeListener() {
        @Override
        public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
            myResult.setText("Resultat : " + rating);
            float totalRating = Float.parseFloat(RatingSum.getText().toString().split(": ")[1]);
            float newTotalRating = rating + totalRating
            RatingSum.setText("Resultat: "+String.valueOf(newTotalRating));

        }
});

ratingBar.setOnRatingBarChangeListener(new 
    RatingBar.OnRatingBarChangeListener() {
        @Override
        public void onRatingChanged(RatingBar ratingBar, float rating1, boolean fromUser) {
            myResult1.setText("Resultat : " + rating1);
            float totalRating = Float.parseFloat(RatingSum.getText().toString().split(": ")[1]);
            float newTotalRating = rating + totalRating
            RatingSum.setText("Resultat: "+String.valueOf(newTotalRating));
        }
});

ratingBarSerice.setOnRatingBarChangeListener(new 
     RatingBar.OnRatingBarChangeListener() {
         @Override
         public void onRatingChanged(RatingBar ratingBar, float rating2, boolean fromUser) {
             myResult2.setText("Resultat : " + rating2);
             float totalRating = Float.parseFloat(RatingSum.getText().toString().split(": ")[1]);
             float newTotalRating = rating + totalRating
             RatingSum.setText("Resultat: "+String.valueOf(newTotalRating));
         }
});