Оценка после перезапуска сбрасывается укажи где я тшибся и исправь ошибки в коде :…
- User
Оценка после перезапуска сбрасывается укажи где я тшибся и исправь ошибки в коде : package com.example.myapp_2.UI.view.fragments;
import static android.content.Context.MODE_PRIVATE;
import android.app.NotificationChannel; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.os.Build; import android.os.Bundle;
import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.core.app.NotificationCompat; import androidx.fragment.app.Fragment; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; import androidx.viewpager.widget.ViewPager;
import android.os.Handler; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.EditText; import android.widget.Switch; import android.widget.TextView; import android.widget.Toast;
import com.example.myapp_2.List_1.Product; import com.example.myapp_2.List_1.ProductAdapter; import com.example.myapp_2.R; import com.example.myapp_2.UI.view.adapters.SliderAdapter; import com.example.myapp_2.UI.view.adapters.SliderAdapter_2;
import java.util.ArrayList; import java.util.List;
public class SecondFragment extends Fragment implements View.OnClickListener { private static final String MY_PREFS_NAME = "MyPrefs_2";
code - Assistant
Ошибка в строке:
SharedPreferences prefs = getContext().getSharedPreferences("MyPrefs2", Context.MODE_PRIVATE);
Неправильный ключ при получении SharedPreferences, должно быть "MyPrefs_2" вместо "MyPrefs2":
SharedPreferences prefs = getContext().getSharedPreferences("MyPrefs_2", Context.MODE_PRIVATE);
Также строки с сохранением и получением оценок повторяются дважды, необходимо их удалить:
products.get(0).setRating(rating1); products.get(1).setRating(rating2); products.get(2).setRating(rating3); products.get(3).setRating(rating4); products.get(4).setRating(rating5); products.get(5).setRating(rating6); products.get(6).setRating(rating7); products.get(7).setRating(rating8); products.get(8).setRating(rating9); products.get(9).setRating(rating10); products.get(0).setRating(rating1); products.get(1).setRating(rating2); products.get(2).setRating(rating3); products.get(3).setRating(rating4); products.get(4).setRating(rating5); products.get(5).setRating(rating6); products.get(6).setRating(rating7); products.get(7).setRating(rating8); products.get(8).setRating(rating9); products.get(9).setRating(rating10);