| | |
| | | package com.example.agvcontroller; |
| | | |
| | | import android.content.SharedPreferences; |
| | | import android.os.Bundle; |
| | | import android.util.Log; |
| | | |
| | | import androidx.appcompat.app.AppCompatActivity; |
| | | import androidx.core.graphics.Insets; |
| | | import androidx.core.view.ViewCompat; |
| | | import androidx.core.view.WindowInsetsCompat; |
| | | import androidx.recyclerview.widget.LinearLayoutManager; |
| | | import androidx.recyclerview.widget.RecyclerView; |
| | | |
| | | import com.google.gson.Gson; |
| | | import com.google.gson.reflect.TypeToken; |
| | | |
| | | import java.lang.reflect.Type; |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | |
| | | public class EditeActivity extends AppCompatActivity { |
| | | |
| | | private RecyclerView recyclerView; |
| | | private List<Item> items; |
| | | private EditeAdapter adapter; |
| | | private SharedPreferences sharedPreferences; |
| | | |
| | | @Override |
| | | protected void onCreate(Bundle savedInstanceState) { |
| | | super.onCreate(savedInstanceState); |
| | | setContentView(R.layout.activity_edite); |
| | | |
| | | sharedPreferences = getSharedPreferences("AGVControllerPrefs", MODE_PRIVATE); |
| | | |
| | | // Load items from SharedPreferences |
| | | items = loadItemsFromSharedPreferences(); |
| | | |
| | | recyclerView = findViewById(R.id.edite_recyclerView); |
| | | recyclerView.setLayoutManager(new LinearLayoutManager(this)); |
| | | |
| | | Log.d("EditeActivity", "onCreate: " + items.size()); |
| | | adapter = new EditeAdapter(items); |
| | | recyclerView.setAdapter(adapter); |
| | | } |
| | | |
| | | @Override |
| | | protected void onDestroy() { |
| | | super.onDestroy(); |
| | | // Save items to SharedPreferences |
| | | saveItemsToSharedPreferences(); |
| | | } |
| | | |
| | | private void saveItemsToSharedPreferences() { |
| | | SharedPreferences.Editor editor = sharedPreferences.edit(); |
| | | Gson gson = new Gson(); |
| | | String json = gson.toJson(items); |
| | | editor.putString("items", json); |
| | | editor.apply(); |
| | | } |
| | | |
| | | private List<Item> loadItemsFromSharedPreferences() { |
| | | Gson gson = new Gson(); |
| | | String json = sharedPreferences.getString("items", null); |
| | | if (json != null) { |
| | | Type type = new TypeToken<List<Item>>(){}.getType(); |
| | | return gson.fromJson(json, type); |
| | | } |
| | | return new ArrayList<>(); |
| | | } |
| | | } |