package com.example.agvcontroller;
|
|
import android.annotation.SuppressLint;
|
import android.content.SharedPreferences;
|
import android.os.Bundle;
|
import android.util.Log;
|
import android.view.View;
|
import android.widget.Button;
|
|
import androidx.appcompat.app.AppCompatActivity;
|
import androidx.appcompat.widget.AppCompatImageButton;
|
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<AGVCar> items;
|
private EditeAdapter adapter;
|
private SharedPreferences sharedPreferences;
|
private AppCompatImageButton addItem;
|
private Button confirm;
|
|
@SuppressLint("WrongViewCast")
|
@Override
|
protected void onCreate(Bundle savedInstanceState) {
|
super.onCreate(savedInstanceState);
|
setContentView(R.layout.activity_edite);
|
addItem = findViewById(R.id.add_button);
|
confirm = findViewById(R.id.confirm_button);
|
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);
|
addItem.setOnClickListener(new View.OnClickListener() {
|
@Override
|
public void onClick(View v) {
|
|
items.add(new AGVCar("", "", 0, "", 0));
|
adapter.notifyDataSetChanged();
|
Log.d("items", items.toString());
|
}
|
});
|
confirm.setOnClickListener(new View.OnClickListener() {
|
@Override
|
public void onClick(View v) {
|
saveItemsToSharedPreferences();
|
}
|
});
|
}
|
|
@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);
|
Log.d("save",json);
|
editor.apply();
|
}
|
|
private List<AGVCar> loadItemsFromSharedPreferences() {
|
Gson gson = new Gson();
|
String json = sharedPreferences.getString("items", null);
|
if (json != null) {
|
Type type = new TypeToken<List<AGVCar>>(){}.getType();
|
return gson.fromJson(json, type);
|
}
|
return new ArrayList<>();
|
}
|
}
|