Моя «оценка приложения» находится в панели навигации. У меня есть общие настройки для сохранения значения шкалы оценок, но когда я открываю ее снова, в ней отображается пустое поле. Где я ошибаюсь? Вот мой код:
case R.id.nav_rate:
try {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
View layout= null;
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layout = inflater.inflate(R.layout.rating, null);
final RatingBar ratingBar = (RatingBar)layout.findViewById(R.id.ratingBar);
builder.setTitle("Rate Us");
builder.setMessage("Thank you for rating us , it will help us to provide you the best service .");
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Float value = ratingBar.getRating();
Toast.makeText(MainActivity.this,"Rating is : "+value,Toast.LENGTH_LONG).show();
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0); // 0 - for private mode
SharedPreferences.Editor editor = pref.edit();
editor.putFloat("numStars", value);//put value
editor.commit();
float rating = pref.getFloat("numStars", 0f);
ratingBar.setRating(rating);//save
}
});
builder.setNegativeButton("No,Thanks", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builder.setCancelable(false);
builder.setView(layout);
builder.show();
} catch (Exception e) {
e.printStackTrace();
}}