回答編集履歴
4
コード修正
test
CHANGED
@@ -66,7 +66,7 @@
|
|
66
66
|
|
67
67
|
String value = pref.getString(prefKey, null);
|
68
68
|
if(value != null) {
|
69
|
-
for(String name : value.split(",")) add(name);
|
69
|
+
for(String name : value.split(",")) list.add(name);
|
70
70
|
}
|
71
71
|
}
|
72
72
|
|
3
コード修正
test
CHANGED
@@ -47,7 +47,7 @@
|
|
47
47
|
|
48
48
|
private static class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder> {
|
49
49
|
private class ViewHolder extends RecyclerView.ViewHolder {
|
50
|
-
final
|
50
|
+
final CheckBox nameView;
|
51
51
|
public ViewHolder(@NonNull View itemView) {
|
52
52
|
super(itemView);
|
53
53
|
nameView = itemView.findViewById(R.id.name);
|
2
コード修正
test
CHANGED
@@ -97,7 +97,7 @@
|
|
97
97
|
@NonNull
|
98
98
|
@Override
|
99
99
|
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
100
|
-
return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.card,
|
100
|
+
return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.card, null, false));
|
101
101
|
}
|
102
102
|
|
103
103
|
@Override
|
1
コード追加
test
CHANGED
@@ -4,3 +4,180 @@
|
|
4
4
|
add により view はビューの階層に組み込まれますが、それは root を根とするツリー状ですので、 1 つの view が複数回 1 つのツリー内に現れることは出来ません。(1つの view を複数のツリーに add することも出来ません。)
|
5
5
|
|
6
6
|
やろうとされていることを考えますと、自分で生成・追加等をするよりも、 普通に RecyclerView 等を使ったほうが良いのではないでしょうか。
|
7
|
+
|
8
|
+
---
|
9
|
+
RecyclerView 版です。
|
10
|
+
|
11
|
+
MainActivity.java
|
12
|
+
```java
|
13
|
+
import android.content.SharedPreferences;
|
14
|
+
import android.os.Bundle;
|
15
|
+
import android.view.*;
|
16
|
+
import android.widget.*;
|
17
|
+
|
18
|
+
import androidx.annotation.NonNull;
|
19
|
+
import androidx.appcompat.app.*;
|
20
|
+
import androidx.recyclerview.widget.RecyclerView;
|
21
|
+
|
22
|
+
import java.util.*;
|
23
|
+
|
24
|
+
public class MainActivity extends AppCompatActivity {
|
25
|
+
@Override
|
26
|
+
protected void onCreate(Bundle savedInstanceState) {
|
27
|
+
super.onCreate(savedInstanceState);
|
28
|
+
setContentView(R.layout.activity_main);
|
29
|
+
|
30
|
+
RecyclerView list = findViewById(R.id.list);
|
31
|
+
Adapter adapter = new Adapter(getPreferences(MODE_PRIVATE), "current");
|
32
|
+
list.setAdapter(adapter);
|
33
|
+
|
34
|
+
Button add = findViewById(R.id.add);
|
35
|
+
add.setOnClickListener(v -> {
|
36
|
+
View view = getLayoutInflater().inflate(R.layout.dialog, null, false);
|
37
|
+
EditText name = view.findViewById(R.id.nameEdit);
|
38
|
+
new AlertDialog.Builder(MainActivity.this)
|
39
|
+
.setView(view)
|
40
|
+
.setTitle("もちものついか")
|
41
|
+
.setPositiveButton("ついか", (d,i) -> adapter.add(name.getText().toString()))
|
42
|
+
.setNegativeButton("やめる", null)
|
43
|
+
.create()
|
44
|
+
.show();
|
45
|
+
});
|
46
|
+
}
|
47
|
+
|
48
|
+
private static class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder> {
|
49
|
+
private class ViewHolder extends RecyclerView.ViewHolder {
|
50
|
+
final TextView nameView;
|
51
|
+
public ViewHolder(@NonNull View itemView) {
|
52
|
+
super(itemView);
|
53
|
+
nameView = itemView.findViewById(R.id.name);
|
54
|
+
Button delete = itemView.findViewById(R.id.delete);
|
55
|
+
delete.setOnClickListener(v -> remove(getAdapterPosition()));
|
56
|
+
}
|
57
|
+
}
|
58
|
+
|
59
|
+
private final SharedPreferences pref;
|
60
|
+
private final String prefKey;
|
61
|
+
private final List<String> list = new ArrayList<>();
|
62
|
+
|
63
|
+
Adapter(SharedPreferences pref, String prefKey) {
|
64
|
+
this.pref = pref;
|
65
|
+
this.prefKey = prefKey;
|
66
|
+
|
67
|
+
String value = pref.getString(prefKey, null);
|
68
|
+
if(value != null) {
|
69
|
+
for(String name : value.split(",")) add(name);
|
70
|
+
}
|
71
|
+
}
|
72
|
+
|
73
|
+
void add(String name) {
|
74
|
+
list.add(name);
|
75
|
+
store();
|
76
|
+
notifyItemInserted(list.size() - 1);
|
77
|
+
}
|
78
|
+
|
79
|
+
void remove(int position) {
|
80
|
+
if(position < 0) return;
|
81
|
+
list.remove(position);
|
82
|
+
store();
|
83
|
+
notifyItemRemoved(position);
|
84
|
+
}
|
85
|
+
|
86
|
+
private void store() {
|
87
|
+
pref.edit().putString(prefKey, getValue()).commit();
|
88
|
+
}
|
89
|
+
|
90
|
+
private String getValue() {
|
91
|
+
if(list.isEmpty()) return null;
|
92
|
+
StringJoiner sj = new StringJoiner(",");
|
93
|
+
for(String value : list) sj.add(value);
|
94
|
+
return sj.toString();
|
95
|
+
}
|
96
|
+
|
97
|
+
@NonNull
|
98
|
+
@Override
|
99
|
+
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
100
|
+
return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.card, parent, false));
|
101
|
+
}
|
102
|
+
|
103
|
+
@Override
|
104
|
+
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
|
105
|
+
holder.nameView.setText(list.get(position));
|
106
|
+
}
|
107
|
+
|
108
|
+
@Override
|
109
|
+
public int getItemCount() {
|
110
|
+
return list.size();
|
111
|
+
}
|
112
|
+
}
|
113
|
+
}
|
114
|
+
```
|
115
|
+
res/layout/activity_main.xml
|
116
|
+
```xml
|
117
|
+
<?xml version="1.0" encoding="utf-8"?>
|
118
|
+
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
119
|
+
xmlns:app="http://schemas.android.com/apk/res-auto"
|
120
|
+
xmlns:tools="http://schemas.android.com/tools"
|
121
|
+
android:layout_width="match_parent"
|
122
|
+
android:layout_height="match_parent"
|
123
|
+
tools:context=".MainActivity">
|
124
|
+
|
125
|
+
<androidx.recyclerview.widget.RecyclerView
|
126
|
+
android:id="@+id/list"
|
127
|
+
android:layout_width="match_parent"
|
128
|
+
android:layout_height="match_parent"
|
129
|
+
android:layout_marginBottom="70dp"
|
130
|
+
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
|
131
|
+
app:layout_constraintBottom_toTopOf="@id/add"
|
132
|
+
app:layout_constraintEnd_toEndOf="parent"
|
133
|
+
app:layout_constraintStart_toStartOf="parent"
|
134
|
+
app:layout_constraintTop_toTopOf="parent" />
|
135
|
+
|
136
|
+
<Button
|
137
|
+
android:id="@+id/add"
|
138
|
+
android:layout_width="wrap_content"
|
139
|
+
android:layout_height="wrap_content"
|
140
|
+
android:layout_marginBottom="10dp"
|
141
|
+
android:text="✙ ついか"
|
142
|
+
app:layout_constraintBottom_toBottomOf="parent"
|
143
|
+
app:layout_constraintEnd_toEndOf="parent"
|
144
|
+
app:layout_constraintStart_toStartOf="parent" />
|
145
|
+
|
146
|
+
</androidx.constraintlayout.widget.ConstraintLayout>
|
147
|
+
```
|
148
|
+
res/layout/card.xml
|
149
|
+
```xml
|
150
|
+
<?xml version="1.0" encoding="utf-8"?>
|
151
|
+
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
|
152
|
+
xmlns:app="http://schemas.android.com/apk/res-auto"
|
153
|
+
android:layout_width="match_parent"
|
154
|
+
android:layout_height="wrap_content"
|
155
|
+
android:layout_margin="10dp"
|
156
|
+
app:cardCornerRadius="10dp"
|
157
|
+
app:cardElevation="10dp">
|
158
|
+
|
159
|
+
<RelativeLayout
|
160
|
+
android:layout_width="match_parent"
|
161
|
+
android:layout_height="wrap_content"
|
162
|
+
android:padding="10dp">
|
163
|
+
|
164
|
+
<CheckBox
|
165
|
+
android:id="@+id/name"
|
166
|
+
android:layout_width="wrap_content"
|
167
|
+
android:layout_height="wrap_content"
|
168
|
+
android:textColor="@android:color/holo_purple"
|
169
|
+
android:textFontWeight="1000"
|
170
|
+
android:textSize="20dp" />
|
171
|
+
|
172
|
+
<Button
|
173
|
+
android:id="@+id/delete"
|
174
|
+
android:layout_width="50dp"
|
175
|
+
android:layout_height="50dp"
|
176
|
+
android:layout_alignParentEnd="true"
|
177
|
+
android:layout_alignParentRight="true"
|
178
|
+
android:text="✖" />
|
179
|
+
|
180
|
+
</RelativeLayout>
|
181
|
+
|
182
|
+
</androidx.cardview.widget.CardView>
|
183
|
+
```
|