前提
androidstudioについて質問します。
以下のプログラムにおいて、編集ボタンを押された場合、編集ボタンに対応する_idを取得したいと思っているのですが全ての行で最終行の_idが取得されて困っています。どのようにしたらうまくいきますか。
実現したいこと
編集ボタンを押したらidを取得し出力する
該当のソースコード
DatabaseHelper.java
1import android.content.Context; 2import android.database.sqlite.SQLiteDatabase; 3import android.database.sqlite.SQLiteOpenHelper; 4 5public class DatabaseHelper extends SQLiteOpenHelper { 6 private static final String DATABASE_NAME = "Sample.db"; 7 private static final int DATABASE_VERSION = 1; 8 9 /** 10 * コンストラクタ。 11 * 12 * @param context コンテキスト 13 */ 14 public DatabaseHelper(Context context){ 15 super(context, DATABASE_NAME, null, DATABASE_VERSION); 16 } 17 18 @Override 19 public void onCreate(SQLiteDatabase db){ 20 //ユーザー情報 21 StringBuffer sb = new StringBuffer(); 22 sb.append("CREATE TABLE sample ("); 23 sb.append("_id INTEGER PRIMARY KEY AUTOINCREMENT,"); 24 sb.append("name TEXT NOT NULL,"); 25 sb.append("age INTEGER NOT NULL"); 26 sb.append(");"); 27 String sql = sb.toString(); 28 29 db.execSQL(sql); 30 } 31 32 @Override 33 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){ 34 } 35}
SampleDAO.java
1import android.database.Cursor; 2import android.database.sqlite.SQLiteDatabase; 3import android.database.sqlite.SQLiteStatement; 4 5public class SampleDAO { 6 public static Cursor findAll(SQLiteDatabase db){ 7 String sql = "SELECT * FROM sample"; 8 Cursor cursor = db.rawQuery(sql, null); 9 return cursor; 10 } 11 12 public static long insert(SQLiteDatabase db, String name, int age){ 13 String sql = "INSERT INTO sample (name, age) VALUES (?, ?)"; 14 SQLiteStatement stmt = db.compileStatement(sql); 15 stmt.bindString(1, name); 16 stmt.bindLong(2, age); 17 long insertedId = stmt.executeInsert(); 18 return insertedId; 19 } 20}
MainActivity.java
1import androidx.appcompat.app.AppCompatActivity; 2 3import android.database.Cursor; 4import android.database.sqlite.SQLiteDatabase; 5import android.os.Bundle; 6import android.view.View; 7import android.widget.Button; 8import android.widget.ListView; 9import android.widget.SimpleCursorAdapter; 10 11import java.util.ArrayList; 12 13public class MainActivity extends AppCompatActivity { 14 /** 15 * データベースヘルパーオブジェクト。 16 */ 17 private DatabaseHelper _helper; 18 @Override 19 protected void onCreate(Bundle savedInstanceState) { 20 super.onCreate(savedInstanceState); 21 setContentView(R.layout.activity_main); 22 23 _helper = new DatabaseHelper(this); 24 SQLiteDatabase db = _helper.getWritableDatabase(); 25 26 ArrayList<ArrayList<String>> list = new ArrayList<>(); 27 28 ArrayList<String> record = new ArrayList<>(); 29 record.add("赤松"); 30 record.add("20"); 31 list.add(record); 32 record = new ArrayList<>(); 33 record.add("河田"); 34 record.add("30"); 35 list.add(record); 36 record = new ArrayList<>(); 37 record.add("鈴木"); 38 record.add("40"); 39 list.add(record); 40 record = new ArrayList<>(); 41 record.add("山田"); 42 record.add("20"); 43 list.add(record); 44 record = new ArrayList<>(); 45 record.add("石田"); 46 record.add("20"); 47 list.add(record); 48 49 for(int i=0; i<list.size(); i++){ 50 SampleDAO.insert(db, list.get(i).get(0), Integer.parseInt(list.get(i).get(1))); 51 } 52 53 Cursor cursor = SampleDAO.findAll(db); 54 ListView flashcardList = findViewById(R.id.listView); 55 //単語帳を表示 56 String[] from = {"name", "age", "_id"}; 57 int[] to = {R.id.textView1, R.id.textView2, R.id.edit}; 58 SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.list, cursor, from, to, 0); 59 adapter.setViewBinder(new ViewBinder()); 60 flashcardList.setAdapter(adapter); 61 } 62 63 private class ViewBinder implements SimpleCursorAdapter.ViewBinder { 64 @Override 65 public boolean setViewValue(View view, Cursor cursor, int columnIndex) { 66 switch(view.getId()) { 67 case R.id.edit: 68 Button button = view.findViewById(R.id.edit); 69 button.setOnClickListener((View v) -> { 70 System.out.println(Long.parseLong(cursor.getString(columnIndex))); 71 }); 72 return true; 73 } 74 return false; 75 } 76 } 77}
activity_main.xml
1<?xml version="1.0" encoding="utf-8"?> 2<androidx.constraintlayout.widget.ConstraintLayout 3 xmlns:android="http://schemas.android.com/apk/res/android" 4 xmlns:app="http://schemas.android.com/apk/res-auto" 5 xmlns:tools="http://schemas.android.com/tools" 6 android:layout_width="match_parent" 7 android:layout_height="match_parent" 8 tools:context=".MainActivity"> 9 10 <ListView 11 android:id="@+id/listView" 12 android:layout_width="0dp" 13 android:layout_height="0dp" 14 android:layout_marginStart="8dp" 15 android:layout_marginTop="8dp" 16 android:layout_marginEnd="8dp" 17 android:layout_marginBottom="8dp" 18 app:layout_constraintBottom_toBottomOf="parent" 19 app:layout_constraintEnd_toEndOf="parent" 20 app:layout_constraintStart_toStartOf="parent" 21 app:layout_constraintTop_toTopOf="parent" /> 22</androidx.constraintlayout.widget.ConstraintLayout>
list.xml
1<?xml version="1.0" encoding="utf-8"?> 2<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent"> 6 7 <Button 8 android:id="@+id/edit" 9 android:layout_width="wrap_content" 10 android:layout_height="wrap_content" 11 android:layout_marginTop="8dp" 12 android:layout_marginEnd="8dp" 13 android:text="編集" 14 app:layout_constraintEnd_toEndOf="parent" 15 app:layout_constraintTop_toTopOf="parent" /> 16 17 <TextView 18 android:id="@+id/textView2" 19 android:layout_width="0dp" 20 android:layout_height="wrap_content" 21 android:layout_marginStart="8dp" 22 android:layout_marginEnd="8dp" 23 app:layout_constraintBottom_toBottomOf="@+id/edit" 24 app:layout_constraintEnd_toStartOf="@+id/edit" 25 app:layout_constraintStart_toStartOf="@+id/guideline2" 26 app:layout_constraintTop_toTopOf="@+id/edit" /> 27 28 <androidx.constraintlayout.widget.Guideline 29 android:id="@+id/guideline2" 30 android:layout_width="wrap_content" 31 android:layout_height="wrap_content" 32 android:orientation="vertical" 33 app:layout_constraintGuide_percent="0.4" /> 34 35 <TextView 36 android:id="@+id/textView1" 37 android:layout_width="0dp" 38 android:layout_height="wrap_content" 39 android:layout_marginStart="8dp" 40 android:layout_marginEnd="8dp" 41 app:layout_constraintBottom_toBottomOf="@+id/edit" 42 app:layout_constraintEnd_toStartOf="@+id/guideline2" 43 app:layout_constraintStart_toStartOf="parent" 44 app:layout_constraintTop_toTopOf="@+id/edit" /> 45</androidx.constraintlayout.widget.ConstraintLayout>

回答1件
あなたの回答
tips
プレビュー
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。
また依頼した内容が修正された場合は、修正依頼を取り消すようにしましょう。
2023/01/23 14:02