前提・実現したいこと
初めて自主製作アプリを開発しているものです。
食事記録アプリ?のようなものが作りたくて
以下のような処理を実現させたいです。
floating Action Buttonを押す。 ↓ カレンダーのダイアログが表示される。 ↓ list_item.xmlの内容がlistViewに出力される。このときダイアログで選択した日付がdate_button.textに代入されている。
発生している問題
ネットのサンプルコードや本を頼りにコーディングを行ったのですが、
以下のような処理になってしまいます。
floating Action Buttonを押す。 ↓ add(ListItem(""))の数だけlist_item.xmlの内容が一度に出力される。 ↓ カレンダーのダイアログが表示される。 ↓ ダイアログで選択した日付が一番上のカードにだけ反映される。
該当のソースコード
kotlin
1//MainActivity.kt 2package com.example.promoto.proc 3 4import android.content.Context 5import android.support.v7.app.AppCompatActivity 6import android.os.Bundle 7import android.text.format.DateFormat 8import android.view.LayoutInflater 9import android.view.View 10import android.view.ViewGroup 11import android.widget.ArrayAdapter 12import android.widget.ImageButton 13import android.widget.ListView 14import android.widget.TextView 15import kotlinx.android.synthetic.main.activity_main.* 16import kotlinx.android.synthetic.main.list_item.* 17 18 19import java.util.* 20 21 22class MainActivity : AppCompatActivity() ,DatePickerFragment.OnDateSelectedListener { 23 24 override fun onSelected(year: Int, month: Int, date: Int) { 25 val c = Calendar.getInstance() 26 c.set(year, month, date) 27 date_button.text = DateFormat.format("MM/dd", c) 28 } 29 30 override fun onCreate(savedInstanceState: Bundle?) { 31 super.onCreate(savedInstanceState) 32 setContentView(R.layout.activity_main) 33 34 floatingActionButton.setOnClickListener { 35 36 //初期のリスト項目を設定 37 val arrayAdapter = MyArrayAdapter(this, 0).apply{ 38 add(ListItem("")) 39 } 40 //ListViewにリスト項目とArrayAdapterを設定 41 val listView: ListView = findViewById(R.id.listView) 42 listView.adapter = arrayAdapter 43 44 val dialog = DatePickerFragment() 45 dialog.show(supportFragmentManager, "date_dialog") 46 } 47 } 48} 49 50 51class ListItem(val date_data : String){} 52//リスト項目を再利用するためのホルダー 53data class ViewHolder(val date_dataView: TextView,val morningIcon:ImageButton,val lunchIcon:ImageButton,val dinnerIcon:ImageButton) 54 55//自作のリスト項目データを扱えるようにしたArrayAdapter 56class MyArrayAdapter : ArrayAdapter<ListItem> { 57 private var inflater : LayoutInflater? = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)as LayoutInflater? 58 59 constructor(context: Context,resource :Int):super(context,resource){} 60 override fun getView(position:Int, convertView: View?, parent: ViewGroup?):View{ 61 62 var viewHolder : ViewHolder? = null 63 var view = convertView 64 65 //再利用の設定 66 if(view == null){ 67 68 view = inflater!!.inflate(R.layout.list_item,parent,false) 69 70 viewHolder = ViewHolder( 71 view.findViewById(R.id.date_button), 72 view.findViewById(R.id.morning_button), 73 view.findViewById(R.id.lunch_button), 74 view.findViewById(R.id.dinner_button) 75 ) 76 view.tag = viewHolder 77 }else{ 78 viewHolder = view.tag as ViewHolder 79 } 80 81 //項目の情報設定 82 val listItem = getItem(position) 83 viewHolder.date_dataView.text =listItem!!.date_data 84 85 viewHolder.morningIcon.setOnClickListener{ 86 //削除ボタンを押したときの処理 87 this.remove(listItem) 88 this.notifyDataSetChanged() 89 } 90 return view!! 91 } 92} 93
kotlin
1//Dialogs.kt 2package com.example.promoto.proc 3 4import android.app.DatePickerDialog 5import android.app.Dialog 6import android.content.Context 7import android.os.Bundle 8import android.support.v4.app.DialogFragment 9import android.widget.DatePicker 10import java.util.* 11 12class DatePickerFragment : DialogFragment(), 13 DatePickerDialog.OnDateSetListener{ 14 15 interface OnDateSelectedListener{ 16 fun onSelected(year: Int,month: Int,date: Int) 17 } 18 19 private lateinit var listener: OnDateSelectedListener 20 21 override fun onAttach(context: Context?){ 22 super.onAttach(context) 23 if(context is OnDateSelectedListener){ 24 listener = context 25 } 26 } 27 28 override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { 29 val c = Calendar.getInstance() 30 val year = c.get(Calendar.YEAR) 31 val month = c.get(Calendar.MONTH) 32 val date =c.get(Calendar.DAY_OF_MONTH) 33 return DatePickerDialog(context,this,year,month,date) 34 35 } 36 37 override fun onDateSet(view: DatePicker,year:Int,month: Int,date: Int) { 38 listener.onSelected(year,month,date) //To change body of created functions use File | Settings | File Templates. 39 } 40}
xml
1//list_item.xml 2<?xml version="1.0" encoding="utf-8"?> 3<android.support.constraint.ConstraintLayout 4 xmlns:android="http://schemas.android.com/apk/res/android" 5 xmlns:tools="http://schemas.android.com/tools" 6 xmlns:app="http://schemas.android.com/apk/res-auto" 7 android:layout_width="match_parent" 8 android:layout_height="match_parent" 9 tools:context=".MainActivity"> 10 11 <android.support.v7.widget.CardView 12 xmlns:android="http://schemas.android.com/apk/res/android" 13 xmlns:card_view="http://schemas.android.com/apk/res-auto" 14 android:layout_width="match_parent" 15 android:layout_height="135dp" 16 card_view:cardCornerRadius="4dp" 17 android:id="@+id/cardView" 18 tools:layout_editor_absoluteX="31dp" 19 card_view:layout_constraintTop_toTopOf="parent" 20 card_view:layout_constraintBottom_toBottomOf="parent"> 21 22 <!-- カードに載せる情報 --> 23 24 <RelativeLayout 25 android:layout_width="match_parent" 26 android:layout_height="match_parent" 27 android:layout_gravity="center_horizontal" 28 android:id="@+id/cardRelative" 29 > 30 <Button 31 android:layout_width="65dp" 32 android:layout_height="match_parent" 33 android:textAllCaps="false" android:layout_alignParentStart="true" 34 android:layout_alignParentTop="true" android:layout_marginTop="0dp" 35 android:layout_marginStart="2dp" android:layout_alignParentBottom="true" 36 android:layout_marginBottom="0dp" android:textStyle="italic" 37 android:layout_toStartOf="@+id/morning_button" android:layout_marginRight="0dp" 38 android:layout_marginEnd="0dp" android:layout_toLeftOf="@+id/morning_button" 39 android:id="@+id/date_button"/> 40 <ImageButton 41 android:layout_width="100dp" 42 android:layout_height="match_parent" app:srcCompat="@drawable/morning_icon" 43 android:id="@+id/morning_button" 44 android:layout_alignParentTop="true" 45 android:layout_marginTop="0dp" android:layout_alignParentBottom="true" 46 android:layout_marginBottom="0dp" 47 android:layout_toStartOf="@+id/lunch_button" android:layout_marginRight="2dp" 48 android:layout_marginEnd="2dp" android:layout_toLeftOf="@+id/lunch_button"/> 49 <ImageButton 50 android:layout_width="100dp" 51 android:layout_height="wrap_content" app:srcCompat="@drawable/lunch_icon" 52 android:id="@+id/lunch_button" 53 android:layout_alignParentTop="true" android:layout_marginTop="0dp" 54 android:layout_alignParentBottom="true" android:layout_marginBottom="0dp" 55 android:layout_toStartOf="@+id/dinner_button" android:layout_marginRight="2dp" 56 android:layout_marginEnd="2dp" android:layout_toLeftOf="@+id/dinner_button"/> 57 <ImageButton 58 android:layout_width="100dp" 59 android:layout_height="wrap_content" app:srcCompat="@drawable/dinner_icon" 60 android:id="@+id/dinner_button" 61 android:layout_alignParentTop="true" android:layout_marginTop="0dp" 62 android:layout_alignParentBottom="true" 63 android:layout_marginBottom="0dp" 64 android:layout_alignParentEnd="true" android:layout_alignParentRight="true" 65 android:layout_marginRight="5dp" android:layout_marginEnd="5dp"/> 66 </RelativeLayout> 67 </android.support.v7.widget.CardView> 68</android.support.constraint.ConstraintLayout> 69 70
試したこと
似たような処理について書かれている記事が見つからない。Buildも問題なくできている事から原因がわからず、手詰まり状態です。
補足情報(FW/ツールのバージョンなど)
Gradle version 4.6
AndroidStudio 3.2.1
プログラミング初心者で、不慣れな点が多く、質問の説明もわかりにくいと思いますが
回答よろしくお願いします。
fabが宣言されていなかったりmainのxmlファイルがなかったりで、再現、検証しづらいです。簡潔な図など、
すみません、誤タッチで記入途中なのにしかも複数回送信してしまいました。続きを書くと、視覚的にイメージしやすくなるようなものなどを資料として追加した方が回答率が上がると思います。
ご指摘ありがとうございます。内容に変更があり新たな質問を作成しました。そちらのほうには図やmainのxmlが記述してあるのでよければご覧ください
回答1件
あなたの回答
tips
プレビュー