質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Realm

RealmとはSQLiteやCore Dataに代わるモバイルデータベースです。iOSとAndroidの両方でサポートされています。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

Kotlin

Kotlinは、ジェットブレインズ社のアンドリー・ブレスラフ、ドミトリー・ジェメロフが開発した、 静的型付けのオブジェクト指向プログラミング言語です。

Q&A

解決済

2回答

2319閲覧

DatePickerDialogの年月を使ってソートしたい

asdfhorse

総合スコア17

Realm

RealmとはSQLiteやCore Dataに代わるモバイルデータベースです。iOSとAndroidの両方でサポートされています。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

Kotlin

Kotlinは、ジェットブレインズ社のアンドリー・ブレスラフ、ドミトリー・ジェメロフが開発した、 静的型付けのオブジェクト指向プログラミング言語です。

0グッド

0クリップ

投稿2018/12/04 02:05

編集2018/12/07 01:00

家計簿アプリを作成しています。家計簿アプリでは月ごとの収支を一覧表示させたいと考えています。家計簿の収支データはもちろん2018年の11月のデータもあれば2018年の12月のデータも格納されていると仮定します。DatePickerDialogで年月を選択し(2018/12と選択したとします)、OKと押したときに2018/12で曖昧検索をし、2018年の12月のデータのみ一覧表示させたいと考えています。DatePickerDialogを使って検索値となる2018/12のデータは取ってこれるのですが、DatePickerDialogの処理を書く場所と書き方がよくわからず困っています。イメージ説明
どなたかご教授お願いします。

kotlin

1package com.example.username.kakeibo 2 3 4import android.content.Intent 5import android.os.Bundle 6import android.support.design.internal.BottomNavigationItemView 7import android.support.v4.app.Fragment 8import android.support.v4.app.FragmentActivity 9import android.view.View 10import android.widget.Button 11import android.widget.ImageButton 12import android.widget.ListView 13import android.widget.TextView 14import io.realm.Realm 15import io.realm.RealmConfiguration 16import io.realm.RealmResults 17import io.realm.kotlin.where 18import kotlinx.android.synthetic.main.activity_kakeibo.* 19import kotlinx.android.synthetic.main.kakeibolist.* 20import java.util.* 21 22 23class kakeiboActivity : FragmentActivity(), DatePick.Callback { 24 private lateinit var realm: Realm 25 private fun initRealm() { 26 val realmConfiguration = RealmConfiguration.Builder() 27 .deleteRealmIfMigrationNeeded() 28 .schemaVersion(0) 29 .build() 30 realm = Realm.getInstance(realmConfiguration) 31 32 } 33 34 override fun onCreate(savedInstanceState: Bundle?) { 35 36 super.onCreate(savedInstanceState) 37 setContentView(R.layout.activity_kakeibo) 38 39 val config = RealmConfiguration.Builder().deleteRealmIfMigrationNeeded().build() 40 val test_navi = findViewById<Button>(R.id.insert_button) 41 val reload = findViewById<ImageButton>(R.id.reload_button) 42 43 Realm.setDefaultConfiguration(config) 44 45 realm = Realm.getDefaultInstance() 46 47 48 var listView: ListView 49 50 51 insert_button.setOnClickListener { 52 val intent = Intent(application, Kakeibo_AddActivity::class.java) 53 startActivity(intent) 54 } 55 val kakeibo_navi: BottomNavigationItemView = findViewById(R.id.navigation_kakeibo) 56 val resito_navi: BottomNavigationItemView = findViewById(R.id.navigation_resito) 57 val buylist_navi: BottomNavigationItemView = findViewById(R.id.navigation_buylist) 58 val recipe_navi: BottomNavigationItemView = findViewById(R.id.navigation_recipe) 59 kakeibo_navi.setOnClickListener { 60 val intent = Intent(this, kakeiboActivity::class.java) 61 startActivity(intent) 62 } 63 buylist_navi.setOnClickListener { 64 val intent = Intent(this, buylistActivity::class.java) 65 startActivity(intent) 66 } 67 resito_navi.setOnClickListener { 68 val intent = Intent(this, resitoActivity::class.java) 69 70 startActivity(intent) 71 } 72 recipe_navi.setOnClickListener { 73 val intent = Intent(this, MainActivity::class.java) 74 startActivity(intent) 75 } 76 77 listView = findViewById(R.id.kakeibolist) 78 79 val Kakeibo = realm.where<Kakeibo>().findAll() 80 listView?.adapter = KakeiboAdapter(Kakeibo) 81 82 83 84 } 85 override fun onDestroy() { 86 super.onDestroy() 87 realm.close() 88 } 89 override fun onDatePicked(year: Int,monthOfYear: Int, dayOfMonth: Int) { 90 val str = String.format(Locale.US, "%d/%d", year, monthOfYear+1 , dayOfMonth) 91 calendar.text = str 92 } 93 override fun onAttachFragment(fragment : Fragment) { 94 if(fragment is DatePick) { 95 fragment.callback = this 96 } 97 } 98 fun showDatePickerDialog(view: View) { 99 val newFragment = DatePick() 100 newFragment.show(supportFragmentManager, "datePicker") 101 } 102 103}

kotlin

1package com.example.username.kakeibo 2 3import android.app.DatePickerDialog 4import android.app.Dialog 5import android.os.Bundle 6import android.support.v4.app.DialogFragment 7import android.widget.DatePicker 8import java.util.* 9 10class DatePick : DialogFragment(),DatePickerDialog.OnDateSetListener { 11 var callback : Callback? = null 12 13 override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { 14 val c = Calendar.getInstance() 15 val year = c.get(Calendar.YEAR) 16 val month = c.get(Calendar.MONTH) 17 val day = c.get(Calendar.DAY_OF_MONTH) 18 19 return DatePickerDialog(activity,this, year,month,day) 20 } 21 override fun onDateSet(view: DatePicker, year: Int,month: Int, dayOfMonth: Int) { 22 callback?.onDatePicked(year , month , dayOfMonth) 23 } 24 interface Callback { 25 fun onDatePicked(year : Int , monthOfYear: Int , dayOfMonth: Int) 26 } 27}

xml

1<?xml version="1.0" encoding="utf-8"?> 2<android.support.constraint.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:id="@+id/container" 7 android:layout_width="match_parent" 8 android:layout_height="match_parent" 9 tools:context=".MainActivity"> 10 11 <TextView 12 android:id="@+id/calendar" 13 android:layout_width="wrap_content" 14 android:layout_height="wrap_content" 15 android:layout_marginLeft="16dp" 16 android:text="@string/title_nengetu" 17 android:textSize="25dp" 18 android:onClick="showDatePickerDialog1" 19 app:layout_constraintLeft_toLeftOf="parent" 20 app:layout_constraintTop_toTopOf="parent" 21 android:layout_marginTop="12dp" 22 /> 23 24 <View 25 android:layout_width="match_parent" 26 android:layout_height="2.5dp" 27 android:background="@color/blackColor" 28 android:layout_marginLeft="10dp" 29 android:layout_marginRight="10dp" 30 app:layout_constraintRight_toRightOf="parent" 31 app:layout_constraintLeft_toLeftOf="parent" 32 app:layout_constraintBottom_toBottomOf="parent" 33 app:layout_constraintTop_toTopOf="parent" 34 app:layout_constraintHorizontal_bias="1.0" 35 app:layout_constraintVertical_bias="0.092"/> 36 37 <android.support.design.widget.BottomNavigationView 38 android:id="@+id/navigation" 39 android:layout_width="0dp" 40 android:layout_height="wrap_content" 41 android:layout_marginEnd="0dp" 42 android:layout_marginStart="0dp" 43 android:background="?android:attr/windowBackground" 44 app:layout_constraintBottom_toBottomOf="parent" 45 app:layout_constraintLeft_toLeftOf="parent" 46 app:layout_constraintRight_toRightOf="parent" 47 app:menu="@menu/navigation"/> 48 <ListView 49 android:id="@+id/kakeibolist" 50 android:layout_width="382dp" 51 android:layout_height="405dp" 52 app:layout_constraintLeft_toLeftOf="parent" 53 app:layout_constraintRight_toRightOf="parent" 54 app:layout_constraintTop_toTopOf="parent" 55 app:layout_constraintBottom_toBottomOf="parent" 56 app:layout_constraintHorizontal_bias="0.0" 57 app:layout_constraintVertical_bias="0.433"/> 58 <Button 59 android:id="@+id/insert_button" 60 android:layout_width="wrap_content" 61 android:layout_height="wrap_content" 62 android:text="@string/insert_gamen" 63 android:layout_marginBottom="300dp" 64 android:layout_marginLeft="260dp" 65 app:layout_constraintLeft_toLeftOf="parent" 66 app:layout_constraintRight_toRightOf="parent" 67 app:layout_constraintTop_toTopOf="parent" 68 app:layout_constraintBottom_toBottomOf="parent" 69 app:layout_constraintHorizontal_bias="0.0" app:layout_constraintVertical_bias="0.0"/> 70</android.support.constraint.ConstraintLayout>

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

kakajika

2018/12/06 16:09

目的の値はDate型ですか? それとも年と月を別々の値として保存していますか?
asdfhorse

2018/12/07 00:44

String型で保存しています。変更点があるため本文に追記しました。
kakajika

2018/12/07 01:40 編集

なるほど、Stringで保存しているのですね。後々のことを考えるとDate型で保存したほうが柔軟な範囲指定ができたり(例えば、過去3ヶ月とか)と何かと便利かと思いますが、とりあえず文字列ということで回答しますね。
asdfhorse

2018/12/11 06:51

すみません。サンプルデータを入れてテストしてみた結果12/10の下に12/3が表示されてしまい、昇順に正しく並べ替えできていないとわかりました。そこでDate型で値を保存し、シリアル値で正しく昇順に並べ替えしたいと考えています。現在StringからDate型への変換とDate型からString型への変換の仕方がよくわからず困っています。よければご教授お願い致します。
kakajika

2018/12/12 11:12

本件とは異なる問題のようですから、別に質問を立ててください。
asdfhorse

2018/12/13 02:00

承知しました。
guest

回答2

0

ベストアンサー

おそらく求めている機能はソートではなく絞り込み(フィルター)ではないでしょうか?

Realmは使ったことがないので 公式ドキュメント を見ながらの回答になりますが、文字列の場合は beginsWith() で先頭一致による検索ができるようです。
これを利用して、 onDatePickedの中で"年/月/"で始まるデータを検索し、adapterをセットする処理を実装してみてください。

kotlin

1override fun onDatePicked(year: Int, monthOfYear: Int, dayOfMonth: Int) { 2 ... 3 4 val kakeiboOfMonth = realm.where<Kakeibo>() 5 .beginsWith("createdAt", "${year}/${monthOfYear+1}/") // フィールド名は特に記述がなかったので適当です 6 .findAll() 7 kakeibolist.adapter = KakeiboAdapter(kakeiboOfMonth) 8}

投稿2018/12/07 01:35

編集2018/12/07 01:37
kakajika

総合スコア3131

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

asdfhorse

2018/12/07 01:49

おかげさまで求めていた機能を実装することが出来ました。kakajikaさんの言う通りソートではなく、フィルター機能でした...  質問内容などに至らない点があったのにも関わらず、自分の実装したい機能を理解して回答していただきありがとうございました!
guest

0

質問の意図を勘違いして、ActivityとFragmentのやりとりの仕方について長文で回答をしてしまったので、内容を削除しました。

投稿2018/12/04 18:00

編集2018/12/05 17:16
kafumi

総合スコア87

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

asdfhorse

2018/12/05 04:52 編集

別の画面(収支追加)で年月日のデータを取得してくるという処理があるのですが、自分はDatePickとDatePickkの二つに分けて処理を書いていたので楽になりました。ですが元々実現したいことに記述しました取得してきたデータを使ってのソートというところがまだよくわかりません。自分の質問の説明に足りないところが多く申し訳ないです。ソート機能についてご教授していただけると幸いです。
kafumi

2018/12/05 17:14

申し訳ありません、質問の意図を勘違いしていました。DatePickerの使い方でなく、ソートの方法ということですね。私はRealmには詳しくなく、アドバイスが難しいのですが、「ソート対象となるデータの定義」と「何にもとづいてソートしたいのか(日付なのか、支出の科目なのか、など)」が質問に含まれていれば、他の方もアドバイスしやすくなると思います。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問