android:用Sharedpreferences保存对象实例
一,安装第三方库:
地址:
https://mvnrepository.com/artifact/com.google.code.gson/gson
build.gradle中dependencies添加:
implementation 'com.google.code.gson:gson:2.13.0'
然后点击 Sync Now
二,代码:
xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activity.PreferenceActivity">
<Button
android:id="@+id/buttonsave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="168dp"
android:layout_marginTop="30dp"
android:layout_marginEnd="155dp"
android:layout_marginBottom="653dp"
android:text="保存"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/buttonread"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="174dp"
android:layout_marginTop="65dp"
android:layout_marginEnd="149dp"
android:layout_marginBottom="539dp"
android:text="读取"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/buttonsave" />
</androidx.constraintlayout.widget.ConstraintLayout>
java
Goodsinfo.java
package com.example.okdemo1.model;
public class GoodsInfo {
//商品id
private int id;
//商品图片
private String pic;
public GoodsInfo(int id, String pic) {
this.id = id;
this.pic = pic;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getPic() {
return pic;
}
public void setPic(String pic) {
this.pic = pic;
}
}
PreferenceActivity.java
package com.example.okdemo1.activity;
import android.app.ActivityOptions;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import com.example.imagelib.ImageListActivity;
import com.example.okdemo1.MainActivity;
import com.example.okdemo1.R;
import com.example.okdemo1.model.GoodsInfo;
import com.google.gson.Gson;
public class PreferenceActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_preference);
//给按钮增加点击事件
Button myButton = findViewById(R.id.buttonsave);
myButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
GoodsInfo gi = new GoodsInfo(123,"桂林山水图片");
// 处理点击事件
//获取一个文件名为userInfo、权限为private的xml文件的SharedPreferences对象
SharedPreferences sharedPreferences = getSharedPreferences("userInfo", MODE_PRIVATE);
//得到SharedPreferences.Editor对象,并保存数据到该对象中
SharedPreferences.Editor editor = sharedPreferences.edit();
//保存key-value对到文件中
Gson gson = new Gson();
String goodsStr = gson.toJson(gi);
editor.putString("goodsOne", goodsStr);
editor.commit();
}
});
//给按钮增加点击事件
Button readButton = findViewById(R.id.buttonread);
readButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 处理点击事件
SharedPreferences sharedPreferences = getSharedPreferences("userInfo", MODE_PRIVATE);
//读取数据
String goodsOne = sharedPreferences.getString("goodsOne", "");
if (goodsOne != null) {
Gson gson = new Gson();
GoodsInfo obj = gson.fromJson(goodsOne, GoodsInfo.class);
//打印已读取数据
Log.d("id",String.valueOf(obj.getId()));
Log.d("pic",obj.getPic());
}
}
});
}
}
三,测试效果:


四,查看文件内容:
<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
<string name="name">总统秘书王富贵</string>
<int name="id" value="12345" />
<string name="goodsOne">{"id":123,"pic":"桂林山水图片"}</string>
</map>
浙公网安备 33010602011771号