teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

ソースを追記

2015/07/15 07:05

投稿

ryo_se
ryo_se

スコア68

title CHANGED
File without changes
body CHANGED
@@ -6,4 +6,124 @@
6
6
  現在更新ボタンをおしたときに、position情報を取得するところまではできたのですが、
7
7
  position情報からEditTextの情報を取得しても入力内容変更前のデータしか取得することができません。
8
8
 
9
- 参考になりそうな文献が少ないため、方法をご教授していただければ幸いです。
9
+ 参考になりそうな文献が少ないため、方法をご教授していただければ幸いです。
10
+
11
+ 追記
12
+
13
+ ●DataBaseActivity.java
14
+ ```
15
+ public class DataBaseActivity extends ActionBarActivity {
16
+ static final String DB = "sqlite_sample.db";
17
+ static final int DB_VERSION = 1;
18
+ static final String CREATE_TABLE = "create table mytable ( _id integer primary key autoincrement, data integer not null );";
19
+ static final String DROP_TABLE = "drop table mytable;";
20
+
21
+ static SQLiteDatabase mydb;
22
+
23
+ private SimpleCursorAdapter myadapter;
24
+ private static final String TAG = MainActivity.class.getSimpleName();
25
+
26
+ private ListView listview;
27
+ private Button addbtn, delbtn, addTxtbtn;
28
+
29
+ /** Called when the activity is first created. */
30
+ @Override
31
+ public void onCreate(Bundle savedInstanceState) {
32
+ super.onCreate(savedInstanceState);
33
+ setContentView(R.layout.activity_data_base);
34
+
35
+ MySQLiteOpenHelper hlpr = new MySQLiteOpenHelper(getApplicationContext());
36
+ mydb = hlpr.getWritableDatabase();
37
+
38
+ Cursor cursor = mydb.query("mytable", new String[] {"_id", "data"}, null, null, null, null, "_id DESC");
39
+ String[] from = new String[] {"_id", "data"};
40
+ int[] to = new int[] {R.id.db_id, R.id.db_data};
41
+ startManagingCursor(cursor);
42
+ myadapter = new SimpleCursorBtnAdapter(this,R.layout.db_text, cursor, from, to,R.id.btn_update);
43
+
44
+ listview = (ListView)findViewById(R.id.db_listView);
45
+ listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
46
+ @Override
47
+ public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
48
+ update(parent,view,position,id);
49
+ }
50
+ });
51
+ listview.setAdapter(myadapter);
52
+
53
+ }
54
+
55
+ private static class MySQLiteOpenHelper extends SQLiteOpenHelper {
56
+ public MySQLiteOpenHelper(Context c) {
57
+ super(c, DB, null, DB_VERSION);
58
+ }
59
+ public void onCreate(SQLiteDatabase db) {
60
+ db.execSQL(CREATE_TABLE);
61
+ }
62
+ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
63
+ db.execSQL(DROP_TABLE);
64
+ onCreate(db);
65
+ }
66
+ }
67
+
68
+
69
+ /**
70
+ * 更新を行う(未完成)
71
+ * @param parent
72
+ * @param view
73
+ * @param position
74
+ * @param id
75
+ */
76
+ private void update(AdapterView<?> parent, View view, int position, long id) {
77
+ ListView listView = (ListView) parent;
78
+ Cursor item = (Cursor)listView.getItemAtPosition(position);
79
+ int lgin_id = item.getInt(item.getColumnIndex("_id"));
80
+ String lgin_name = item.getString(item.getColumnIndex("data"));
81
+ Log.d("ItemClick", "id="+lgin_id+", data=" + lgin_name);
82
+
83
+ }
84
+
85
+
86
+
87
+ }
88
+
89
+
90
+ ```
91
+
92
+ ●DataBaseActivity.java
93
+ ```
94
+ /**
95
+ * ListViewにおけるボタン要素のポジション取得アダプター
96
+ * Created by r-aga on 2015/07/13.
97
+ */
98
+ public class SimpleCursorBtnAdapter extends SimpleCursorAdapter {
99
+ private int mButton;
100
+
101
+ public SimpleCursorBtnAdapter(Context context, int layout, Cursor c, String[] from, int[] to, int button) {
102
+ super(context, layout, c, from, to);
103
+ mButton = button;
104
+ }
105
+
106
+ public View getView(final int position, View convertView, final ViewGroup parent) {
107
+ View view = super.getView(position, convertView, parent);
108
+ Button btn = (Button) view.findViewById(mButton);
109
+ btn.setTag(position);
110
+
111
+ btn.setOnClickListener(new View.OnClickListener() {
112
+ @Override
113
+ public void onClick(View arg) {
114
+ AdapterView.OnItemClickListener listener = list.getOnItemClickListener();
115
+ long id = getItemId(position);
116
+ listener.onItemClick((AdapterView<?>) parent, arg, position, id);
117
+ }
118
+ });
119
+ return view;
120
+ }
121
+
122
+ }
123
+ ```
124
+
125
+
126
+ 不要な部分は削除したので、おかしなところがあるかもしれません。
127
+ ListViewにおいて、更新ボタンを押した時のpositionを作成するために、アダプターを自前で用意しています。
128
+ 参考サイト
129
+ http://blogs.gine.jp/taka/archives/2966