質問編集履歴
1
ソースを追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -15,3 +15,243 @@
|
|
15
15
|
|
16
16
|
|
17
17
|
参考になりそうな文献が少ないため、方法をご教授していただければ幸いです。
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
追記
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
●DataBaseActivity.java
|
26
|
+
|
27
|
+
```
|
28
|
+
|
29
|
+
public class DataBaseActivity extends ActionBarActivity {
|
30
|
+
|
31
|
+
static final String DB = "sqlite_sample.db";
|
32
|
+
|
33
|
+
static final int DB_VERSION = 1;
|
34
|
+
|
35
|
+
static final String CREATE_TABLE = "create table mytable ( _id integer primary key autoincrement, data integer not null );";
|
36
|
+
|
37
|
+
static final String DROP_TABLE = "drop table mytable;";
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
static SQLiteDatabase mydb;
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
private SimpleCursorAdapter myadapter;
|
46
|
+
|
47
|
+
private static final String TAG = MainActivity.class.getSimpleName();
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
private ListView listview;
|
52
|
+
|
53
|
+
private Button addbtn, delbtn, addTxtbtn;
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
/** Called when the activity is first created. */
|
58
|
+
|
59
|
+
@Override
|
60
|
+
|
61
|
+
public void onCreate(Bundle savedInstanceState) {
|
62
|
+
|
63
|
+
super.onCreate(savedInstanceState);
|
64
|
+
|
65
|
+
setContentView(R.layout.activity_data_base);
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
MySQLiteOpenHelper hlpr = new MySQLiteOpenHelper(getApplicationContext());
|
70
|
+
|
71
|
+
mydb = hlpr.getWritableDatabase();
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
Cursor cursor = mydb.query("mytable", new String[] {"_id", "data"}, null, null, null, null, "_id DESC");
|
76
|
+
|
77
|
+
String[] from = new String[] {"_id", "data"};
|
78
|
+
|
79
|
+
int[] to = new int[] {R.id.db_id, R.id.db_data};
|
80
|
+
|
81
|
+
startManagingCursor(cursor);
|
82
|
+
|
83
|
+
myadapter = new SimpleCursorBtnAdapter(this,R.layout.db_text, cursor, from, to,R.id.btn_update);
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
listview = (ListView)findViewById(R.id.db_listView);
|
88
|
+
|
89
|
+
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
|
90
|
+
|
91
|
+
@Override
|
92
|
+
|
93
|
+
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
|
94
|
+
|
95
|
+
update(parent,view,position,id);
|
96
|
+
|
97
|
+
}
|
98
|
+
|
99
|
+
});
|
100
|
+
|
101
|
+
listview.setAdapter(myadapter);
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
}
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
private static class MySQLiteOpenHelper extends SQLiteOpenHelper {
|
110
|
+
|
111
|
+
public MySQLiteOpenHelper(Context c) {
|
112
|
+
|
113
|
+
super(c, DB, null, DB_VERSION);
|
114
|
+
|
115
|
+
}
|
116
|
+
|
117
|
+
public void onCreate(SQLiteDatabase db) {
|
118
|
+
|
119
|
+
db.execSQL(CREATE_TABLE);
|
120
|
+
|
121
|
+
}
|
122
|
+
|
123
|
+
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
|
124
|
+
|
125
|
+
db.execSQL(DROP_TABLE);
|
126
|
+
|
127
|
+
onCreate(db);
|
128
|
+
|
129
|
+
}
|
130
|
+
|
131
|
+
}
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
|
136
|
+
|
137
|
+
/**
|
138
|
+
|
139
|
+
* 更新を行う(未完成)
|
140
|
+
|
141
|
+
* @param parent
|
142
|
+
|
143
|
+
* @param view
|
144
|
+
|
145
|
+
* @param position
|
146
|
+
|
147
|
+
* @param id
|
148
|
+
|
149
|
+
*/
|
150
|
+
|
151
|
+
private void update(AdapterView<?> parent, View view, int position, long id) {
|
152
|
+
|
153
|
+
ListView listView = (ListView) parent;
|
154
|
+
|
155
|
+
Cursor item = (Cursor)listView.getItemAtPosition(position);
|
156
|
+
|
157
|
+
int lgin_id = item.getInt(item.getColumnIndex("_id"));
|
158
|
+
|
159
|
+
String lgin_name = item.getString(item.getColumnIndex("data"));
|
160
|
+
|
161
|
+
Log.d("ItemClick", "id="+lgin_id+", data=" + lgin_name);
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+
}
|
166
|
+
|
167
|
+
|
168
|
+
|
169
|
+
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
}
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
|
178
|
+
|
179
|
+
```
|
180
|
+
|
181
|
+
|
182
|
+
|
183
|
+
●DataBaseActivity.java
|
184
|
+
|
185
|
+
```
|
186
|
+
|
187
|
+
/**
|
188
|
+
|
189
|
+
* ListViewにおけるボタン要素のポジション取得アダプター
|
190
|
+
|
191
|
+
* Created by r-aga on 2015/07/13.
|
192
|
+
|
193
|
+
*/
|
194
|
+
|
195
|
+
public class SimpleCursorBtnAdapter extends SimpleCursorAdapter {
|
196
|
+
|
197
|
+
private int mButton;
|
198
|
+
|
199
|
+
|
200
|
+
|
201
|
+
public SimpleCursorBtnAdapter(Context context, int layout, Cursor c, String[] from, int[] to, int button) {
|
202
|
+
|
203
|
+
super(context, layout, c, from, to);
|
204
|
+
|
205
|
+
mButton = button;
|
206
|
+
|
207
|
+
}
|
208
|
+
|
209
|
+
|
210
|
+
|
211
|
+
public View getView(final int position, View convertView, final ViewGroup parent) {
|
212
|
+
|
213
|
+
View view = super.getView(position, convertView, parent);
|
214
|
+
|
215
|
+
Button btn = (Button) view.findViewById(mButton);
|
216
|
+
|
217
|
+
btn.setTag(position);
|
218
|
+
|
219
|
+
|
220
|
+
|
221
|
+
btn.setOnClickListener(new View.OnClickListener() {
|
222
|
+
|
223
|
+
@Override
|
224
|
+
|
225
|
+
public void onClick(View arg) {
|
226
|
+
|
227
|
+
AdapterView.OnItemClickListener listener = list.getOnItemClickListener();
|
228
|
+
|
229
|
+
long id = getItemId(position);
|
230
|
+
|
231
|
+
listener.onItemClick((AdapterView<?>) parent, arg, position, id);
|
232
|
+
|
233
|
+
}
|
234
|
+
|
235
|
+
});
|
236
|
+
|
237
|
+
return view;
|
238
|
+
|
239
|
+
}
|
240
|
+
|
241
|
+
|
242
|
+
|
243
|
+
}
|
244
|
+
|
245
|
+
```
|
246
|
+
|
247
|
+
|
248
|
+
|
249
|
+
|
250
|
+
|
251
|
+
不要な部分は削除したので、おかしなところがあるかもしれません。
|
252
|
+
|
253
|
+
ListViewにおいて、更新ボタンを押した時のpositionを作成するために、アダプターを自前で用意しています。
|
254
|
+
|
255
|
+
参考サイト
|
256
|
+
|
257
|
+
http://blogs.gine.jp/taka/archives/2966
|