質問編集履歴
1
ソースコードの追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -3,6 +3,10 @@
|
|
3
3
|
ソースコードのような2つのクラスのフィールドを別々のデータベースに保存します。
|
4
4
|
|
5
5
|
その後呼び出すときには2つのクラスは同じItemという括りでまとめて、図のようにidの昇順(同じ場合はAが先)にソートして、名前だけをリスト表示し、それぞれのクリックイベントを持たせたいです。
|
6
|
+
|
7
|
+
|
8
|
+
|
9
|
+
// 9/4 17:15 下部に詳細を追記しました。
|
6
10
|
|
7
11
|
|
8
12
|
|
@@ -97,3 +101,95 @@
|
|
97
101
|
...
|
98
102
|
|
99
103
|
```
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
###9/4 17:15 追記
|
108
|
+
|
109
|
+
```Java
|
110
|
+
|
111
|
+
//MainActivity
|
112
|
+
|
113
|
+
ListView listView;
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
@Override
|
118
|
+
|
119
|
+
protected void onCreate(Bundle savedInstanceState) {
|
120
|
+
|
121
|
+
super.onCreate(savedInstanceState);
|
122
|
+
|
123
|
+
setContentView(R.layout.activity_main);
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
listView = (ListView) findViewById(R.id.listview);
|
128
|
+
|
129
|
+
ItemAdapter adapter=new ItemAdapter(this);
|
130
|
+
|
131
|
+
listView.setAdapter(adapter);
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
String[] itemNames=... //ItemAとItemBのdatabaseからname要素のみを取り出し、ソートした配列を用意
|
136
|
+
|
137
|
+
|
138
|
+
|
139
|
+
for(int i=0;i<itemNames.length;i++){
|
140
|
+
|
141
|
+
adapter.add(itemNames[i]); //Itemインスタンスではなく、先ほどのitemNamesの配列をAdapterにセット
|
142
|
+
|
143
|
+
/*nameのみの配列をAdapterに渡してしまうと、後々ItemAかItemBで別々のクリックイベントを持たせたいときに識別する方法がなくなる。
|
144
|
+
|
145
|
+
*また、同名のものにも対応ができない。
|
146
|
+
|
147
|
+
*前もってItemをインスタンス化してその配列をセットしてもいいが、このviewには15個あるフィールドのうちnameしか使わない。
|
148
|
+
|
149
|
+
なのでnameとは別にdatabaseからもってきたidの配列をカスタムAdapterにインスタンスとして持たせ、getViewの時にidとしてsetできないか考えた。*/
|
150
|
+
|
151
|
+
}
|
152
|
+
|
153
|
+
|
154
|
+
|
155
|
+
}
|
156
|
+
|
157
|
+
public class ItemAdapter extends ArrayAdapter<String> {
|
158
|
+
|
159
|
+
public ItemAdapter(Context context){
|
160
|
+
|
161
|
+
super(context,R.layout.list_row);
|
162
|
+
|
163
|
+
}
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
@Override
|
168
|
+
|
169
|
+
public View getView(int position, View convertView, ViewGroup parent){
|
170
|
+
|
171
|
+
if(convertView==null){
|
172
|
+
|
173
|
+
convertView=getLayoutInflater().inflate(R.layout.list_row,null);
|
174
|
+
|
175
|
+
}
|
176
|
+
|
177
|
+
//ItemAdapterに持たせているItemのnameリストから呼び出し
|
178
|
+
|
179
|
+
String str =getItem(position);
|
180
|
+
|
181
|
+
|
182
|
+
|
183
|
+
//textViewにitemのnameのみをセット、このtextViewにクリックイベントを持たせようとしても識別できない
|
184
|
+
|
185
|
+
TextView textTitle=(TextView)convertView.findViewById(R.id.text_title);
|
186
|
+
|
187
|
+
textTitle.setText(str);
|
188
|
+
|
189
|
+
return convertView;
|
190
|
+
|
191
|
+
}
|
192
|
+
|
193
|
+
}
|
194
|
+
|
195
|
+
```
|