#
whycq
2025-01-20 458ee409159fae8930af673778aa8a965877dcfa
#
3个文件已修改
48 ■■■■■ 已修改文件
app/build.gradle 1 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
app/src/main/java/com/example/agvcontroller/EditeActivity.java 36 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
app/src/main/res/layout/activity_edite.xml 11 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
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'
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<>();
    }
}
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" />