From 458ee409159fae8930af673778aa8a965877dcfa Mon Sep 17 00:00:00 2001 From: whycq <913841844@qq.com> Date: 星期一, 20 一月 2025 17:04:31 +0800 Subject: [PATCH] # --- app/src/main/java/com/example/agvcontroller/EditeActivity.java | 36 ++++++++++++++++++++++++++++++++++-- app/src/main/res/layout/activity_edite.xml | 11 +++++++++-- app/build.gradle | 1 + 3 files changed, 44 insertions(+), 4 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 7dcae09..3e3614e 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -38,6 +38,7 @@ androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1' implementation 'org.greenrobot:eventbus:3.2.0' implementation 'redis.clients:jedis:3.0.1' + implementation 'com.google.code.gson:gson:2.8.9' implementation 'androidx.recyclerview:recyclerview:1.3.0' implementation 'io.netty:netty-all:4.1.68.Final' diff --git a/app/src/main/java/com/example/agvcontroller/EditeActivity.java b/app/src/main/java/com/example/agvcontroller/EditeActivity.java index d4ee322..b1b0f7a 100644 --- a/app/src/main/java/com/example/agvcontroller/EditeActivity.java +++ b/app/src/main/java/com/example/agvcontroller/EditeActivity.java @@ -1,5 +1,6 @@ package com.example.agvcontroller; +import android.content.SharedPreferences; import android.os.Bundle; import android.util.Log; @@ -7,6 +8,10 @@ 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; @@ -15,15 +20,17 @@ 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); - items = new ArrayList<>(); + sharedPreferences = getSharedPreferences("AGVControllerPrefs", MODE_PRIVATE); - items.add(new Item("Item 1", "Description 1", 10, "1", 0)); + // Load items from SharedPreferences + items = loadItemsFromSharedPreferences(); recyclerView = findViewById(R.id.edite_recyclerView); recyclerView.setLayoutManager(new LinearLayoutManager(this)); @@ -32,4 +39,29 @@ 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<>(); + } } \ No newline at end of file diff --git a/app/src/main/res/layout/activity_edite.xml b/app/src/main/res/layout/activity_edite.xml index 637f41a..0d1f37b 100644 --- a/app/src/main/res/layout/activity_edite.xml +++ b/app/src/main/res/layout/activity_edite.xml @@ -52,14 +52,21 @@ android:id="@+id/edite_recyclerView" android:layout_width="0dp" android:layout_height="0dp" - app:layout_constraintBottom_toBottomOf="parent" + app:layout_constraintBottom_toTopOf="@+id/addButton" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="1.0" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/header" app:layout_constraintVertical_bias="1.0" /> - + <ImageButton + android:id="@+id/addButton" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:src="@android:drawable/ic_input_add" + android:background="?attr/selectableItemBackgroundBorderless" + android:padding="10dp" + tools:ignore="MissingConstraints" /> -- Gitblit v1.9.1