hashmap和SharedPreferences存储
把hashmap转换成string,然后存到共享参数里面,然后从共享参数取数据
step1: HelloActivity
package com.example.myapplication;
import static com.example.myapplication.ExtrasKt.FIRST_GUID_NET;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import com.google.gson.Gson;
import java.util.HashMap;
public class HelloActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hello);
Button btn_hello = findViewById(R.id.btn_hello);
btn_hello.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
hashmapTest();
}
});
}
private void hashmapTest() {
HashMap<String, String> testHashMap = new HashMap<String, String>();
testHashMap.put("A4:C1:07:7F:74:2B", "通断器");
testHashMap.put("A7:C1:07:7F:74:2B", "随意贴");
Gson gson = new Gson();
String hashMapString = gson.toJson(testHashMap);
SpUtils.putString(HelloActivity.this, FIRST_GUID_NET, hashMapString);
Intent intent = new Intent(HelloActivity.this,FirstActivity.class);
startActivity(intent);
}
}
step2: FirstActivity
package com.example.myapplication;
import static com.example.myapplication.