Вот код я возможно в нем где то ошибся , Сделай так чтобы наша оценка не сбрасывалась…
- User
Вот код я возможно в нем где то ошибся , Сделай так чтобы наша оценка не сбрасывалась после перезапуска приложения) для этого исправь код или укажи что нужна испаравить мне : @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.fragment_first, container, false);
recyclerView = v.findViewById(R.id.recycler_view_products); recyclerView.setLayoutManager(new LinearLayoutManager(getContext())); List<Product> products = getProducts(); productAdapter = new ProductAdapter(getContext(), products); recyclerView.setAdapter(productAdapter); // Получаем оценки из SharedPreferences SharedPreferences prefs = getContext().getSharedPreferences("MyPrefs", Context.MODE_PRIVATE); float rating1 = prefs.getFloat("rating1", 0.0f); float rating2 = prefs.getFloat("rating2", 0.0f); float rating3 = prefs.getFloat("rating3", 0.0f); // Устанавливаем оценки в соответствующие товары products.get(0).setRating(rating1); products.get(1).setRating(rating2); products.get(2).setRating(rating3); viewPager = v.findViewById(R.id.view_pager); SliderAdapter adapter = new SliderAdapter(getActivity(), imageUrls); viewPager.setAdapter(adapter); final Handler handler = new Handler(); final Runnable runnable = new Runnable() { @Override public void run() { int currentItem = viewPager.getCurrentItem(); int totalItems = viewPager.getAdapter().getCount(); int nextItem = currentItem == totalItems - 1 ? 0 : currentItem + 1; viewPager.setCurrentItem(nextItem); handler.postDelayed(this, 3000); } }; handler.postDelayed(runnable, 3000); return v; } - Assistant
Для того чтобы сохранить оценки в SharedPreferences, необходимо использовать метод SharedPreferences.Editor, например:
SharedPreferences prefs = getContext().getSharedPreferences("MyPrefs", Context.MODE_PRIVATE); SharedPreferences.Editor editor = prefs.edit(); editor.putFloat("rating1", products.get(0).getRating()); editor.putFloat("rating2", products.get(1).getRating()); editor.putFloat("rating3", products.get(2).getRating()); editor.apply();
Также необходимо убедиться, что методы getRating() и setRating() правильно работают с оценкой товара и сохраняют ее состояние.
- User
Вот код я возможно в нем где то ошибся , Сделай так чтобы наша оценка не сбрасывалась после перезапуска приложения) для этого исправь код или укажи что нужна исправить мне перепиши код ,чтобы все заработало : @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.fragment_first, container, false);
recyclerView = v.findViewById(R.id.recycler_view_products); recyclerView.setLayoutManager(new LinearLayoutManager(getContext())); List products = getProducts(); productAdapter = new ProductAdapter(getContext(), products); recyclerView.setAdapter(productAdapter);
// Получаем оценки из SharedPreferences SharedPreferences prefs = getContext().getSharedPreferences(“MyPrefs”, Context.MODE_PRIVATE); float rating1 = prefs.getFloat(“rating1”, 0.0f); float rating2 = prefs.getFloat(“rating2”, 0.0f); float rating3 = prefs.getFloat(“rating3”, 0.0f);
// Устанавливаем оценки в соответствующие товары products.get(0).setRating(rating1); products.get(1).setRating(rating2); products.get(2).setRating(rating3);
- Assistant
- User
- Assistant