-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainActivity.java
More file actions
112 lines (89 loc) · 3.82 KB
/
Copy pathMainActivity.java
File metadata and controls
112 lines (89 loc) · 3.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package com.example.recyclerview;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import com.example.recyclerview.Data.Data;
import com.example.recyclerview.Data.Person;
import com.example.recyclerview.RecyclerViewClasses.RecyclerAdapter;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import java.io.ObjectStreamException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView recyclerView = findViewById(R.id.recyclerView);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
recyclerView.setLayoutManager(layoutManager);
SharedPreferences sharedPreferences = getSharedPreferences("shared", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
List<Person> list = setPersonData();
String json = new Gson().toJson(list);
editor.putString("contacts", json);
editor.commit();
String strContact = sharedPreferences.getString("contacts", "");
Type listType = new TypeToken<ArrayList<Person>>() {}.getType();
ArrayList<Person> datas = new Gson().fromJson(strContact, listType);
// Map<Integer, Map<String, Object>> map = setPersonData();
// for(Map.Entry<Integer, Map<String, Object>> entry : map.entrySet()){
// System.out.println(entry.getKey());
// System.out.println(entry.getValue());
// editor.putInt("id", entry.getKey());
// editor.putInt("img", (Integer) entry.getValue().get("img"));
// editor.putString("name", (String) entry.getValue().get("name"));
//
// System.out.println(editor.toString());
// //editor.commit();
// }
// String json = new Gson().toJson(map);
// editor.putString("shared", json);
// editor.commit();
//
// Type type = new TypeToken<ArrayList<Person>>() {}.getType();
//
// ArrayList<Person> personalList = new Gson().fromJson(json, type);
// for(Person p : personalList){
// System.out.println(p.getpId());
// System.out.println(p.getName());
// System.out.println(p.getImageId());
// }
recyclerView.setAdapter(new RecyclerAdapter(datas));
}
public List<Person> setPersonData(){
List<String> nameList = new ArrayList<>();
nameList.add("김병구");
nameList.add("김형진");
nameList.add("박경현");
nameList.add("이지율");
nameList.add("최영재");
List<Person> personList = new ArrayList<>();
for(int i=0; i < 5; i++){
Person person = new Person();
person.setImageId(R.drawable.ic_launcher_foreground);
person.setpId(i+1);
person.setName(nameList.get(i));
personList.add(person);
}
// Map<Integer, Map<String, Object>> map = new HashMap<>();
//
// for(Person p : personList){
// Map<String, Object> newMap = new HashMap<>();
// newMap.put("img", p.getImageId());
// newMap.put("name", p.getName());
//
// map.put(p.getpId(), newMap);
// }
return personList;
}
}