Spinnerで選択した項目でSQLite(Room)の抽出項目を選択したいのですが、
調べても分からず質問させていただきました。
初心者の質問で申し訳ありませんが、ご教授よろしくお願いします。
Activity
1class NaviSettingActivity : AppCompatActivity() { 2 3 private var dateSet: Int? = null 4 private var courseSet: Int? = null 5 6 override fun onCreate(savedInstanceState: Bundle?) { 7 super.onCreate(savedInstanceState) 8 setContentView(R.layout.activity_navi_setting) 9 10 // スピナー:曜日セット 11 val dateSelect: Spinner = findViewById(R.id.date_in) 12 val dateItem = resources.getStringArray(R.array.date_list) 13 val dateAdapter = ArrayAdapter(this, R.layout.spinner_dropdown_item, dateItem) 14 dateSelect.adapter = dateAdapter 15 16 dateSelect.onItemSelectedListener = object : AdapterView.OnItemSelectedListener { 17 override fun onNothingSelected(parent: AdapterView<*>?) { 18 } 19 20 override fun onItemSelected( 21 parent: AdapterView<*>?, 22 view: View?, 23 position: Int, 24 id: Long 25 ) { 26 val spinner = parent as? Spinner 27 val item1 = spinner?.selectedItem as? Int 28 dateSet = item1 29 } 30 } 31 32 33 // スピナー:コースセット 34 val courseSelect: Spinner = findViewById(R.id.course_in) 35 val courseItem = resources.getStringArray(R.array.course_list) 36 val courseAdapter = ArrayAdapter(this, R.layout.spinner_dropdown_item, courseItem) 37 courseSelect.adapter = courseAdapter 38 39 courseSelect.onItemSelectedListener = object : AdapterView.OnItemSelectedListener { 40 override fun onNothingSelected(parent: AdapterView<*>?) { 41 } 42 43 override fun onItemSelected( 44 parent: AdapterView<*>?, 45 view: View?, 46 position: Int, 47 id: Long 48 ) { 49 val spinner = parent as? Spinner 50 val item2 = spinner?.selectedItem as? Int 51 courseSet = item2 52 } 53 } 54 55 56 // 仮マップ表示 57 val mapClick = findViewById<Button>(R.id.btMap) 58 mapClick.setOnClickListener { 59 val intent1 = Intent(this, MapsActivity::class.java) 60 startActivity(intent1) 61 } 62 63 // コース修正画面表示 64 val fixClick = findViewById<Button>(R.id.btCourseFix) 65 fixClick.setOnClickListener { 66 val intent2 = Intent(this, BaseActivity::class.java) 67 startActivity(intent2) 68 } 69 } 70} 71
Repository
1class UserRepository(private val userDao: UserDao) { 2 3 val dateSet = 2 4 val courseSet = 2 5 val readAllData: LiveData<List<User>> = userDao.readAllData(dateSet = dateSet,courseSet = courseSet) 6 7 suspend fun addUser(user: User) { 8 userDao.addUser(user) 9 } 10 11 suspend fun updateUser(user: User) { 12 userDao.updateUser(user) 13 } 14 15 suspend fun deleteUser(user: User) { 16 userDao.deleteUser(user) 17 } 18 19 suspend fun deleteAllUser() { 20 userDao.deleteAllUsers() 21 } 22}
Dao
1@Dao 2interface UserDao { 3 4 @Insert(onConflict = OnConflictStrategy.REPLACE) 5 suspend fun addUser(user: User) 6 7 @Update 8 suspend fun updateUser(user: User) 9 10 @Delete 11 fun deleteUser(user: User) 12 13 @Query("DELETE FROM user_table") 14 suspend fun deleteAllUsers() 15 16// @Query("SELECT * FROM user_table ORDER BY id ASC") 17 @Query("SELECT * FROM user_table WHERE date = :dateSet + course = :courseSet") 18 fun readAllData(dateSet: Int,courseSet:Int): LiveData<List<User>> 19}
Repositoryに暫定としてdateSetとcourseSetに"2"を入れてますが、
これをActivityのSpinnerで選択した項目を入れて、SQLite(Room)の
抽出項目にしたいと考えてます。
あなたの回答
tips
プレビュー