前提・実現したいこと
プログラミング初心者で、不慣れな点が多く、質問の説明もわかりにくいと思いますが
解決策を教えていただけると助かります。
実現したいことは
フローティングアクションボタンを押すとlistViewにadapterが追加された後、
カレンダーダイアログが表示され、date_butto.textに選択した日付が代入される。
という処理です。
発生している問題
下記のコードでアプリを実行すると
1つめのアダプターには日付が代入されるのですが
2つめ以降のアダプターを作成しようとすると1つ目の日付が上書きされてしまい、2つ目は空欄のままになってしまいます。
また、スクロールをしても1つ目の日付が消えてしまいます。
該当のソースコード
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 override fun onSelected(year: Int, month: Int, date: Int) { 24 val c = Calendar.getInstance() 25 c.set(year, month, date) 26 date_button.text= DateFormat.format("MM/dd", c) 27 } 28 29 override fun onCreate(savedInstanceState: Bundle?) { 30 super.onCreate(savedInstanceState) 31 setContentView(R.layout.activity_main) 32 33 val arrayAdapter = MyArrayAdapter(this, 0).apply{ 34 floatingActionButton.setOnClickListener { 35 val dialog = DatePickerFragment() 36 dialog.show(supportFragmentManager, "date_dialog") 37 add(ListItem("")) 38 } 39 } 40 val listView: ListView = findViewById(R.id.listView) 41 listView.adapter = arrayAdapter 42 43 } 44} 45 46 47class ListItem(val date_data:String){} 48 49data class ViewHolder(val date_dataView: TextView,val morningIcon:ImageButton,val lunchIcon:ImageButton,val dinnerIcon:ImageButton) 50 51class MyArrayAdapter : ArrayAdapter<ListItem> { 52 private var inflater : LayoutInflater? = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)as LayoutInflater? 53 54 constructor(context: Context,resource :Int):super(context,resource){} 55 override fun getView(position:Int, convertView: View?, parent: ViewGroup?):View{ 56 57 var viewHolder : ViewHolder? = null 58 var view = convertView 59 60 61 if(view == null){ 62 63 view = inflater!!.inflate(R.layout.list_item,parent,false) 64 65 viewHolder = ViewHolder( 66 view.findViewById(R.id.date_button), 67 view.findViewById(R.id.morning_button), 68 view.findViewById(R.id.lunch_button), 69 view.findViewById(R.id.dinner_button) 70 ) 71 view.tag = viewHolder 72 }else{ 73 viewHolder = view.tag as ViewHolder 74 } 75 76 val listItem = getItem(position) 77 viewHolder.date_dataView.text =listItem!!.date_data 78 79 viewHolder.morningIcon.setOnClickListener{ 80 81 this.remove(listItem) 82 this.notifyDataSetChanged() 83 } 84 return view!! 85 } 86} 87
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" android:text="@string/date_button_text"/> 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
xml
1//activity_main.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 <ListView 12 android:layout_width="match_parent" 13 android:layout_height="match_parent" 14 app:layout_constraintStart_toStartOf="parent" 15 app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" 16 app:layout_constraintTop_toTopOf="parent" android:id="@+id/listView" 17 app:layout_constraintHorizontal_bias="0.0" app:layout_constraintVertical_bias="1.0"/> 18 <android.support.design.widget.FloatingActionButton 19 android:layout_height="41dp" 20 android:clickable="true" app:srcCompat="@drawable/ic_add_black_24dp" 21 android:id="@+id/floatingActionButton" 22 android:layout_marginBottom="8dp" 23 app:layout_constraintBottom_toBottomOf="parent" android:layout_marginEnd="8dp" 24 app:layout_constraintEnd_toEndOf="parent" android:layout_marginRight="8dp" android:layout_marginTop="8dp" 25 app:layout_constraintTop_toTopOf="parent" android:layout_marginStart="8dp" 26 app:layout_constraintStart_toStartOf="@+id/listView" android:layout_marginLeft="8dp" 27 app:layout_constraintHorizontal_bias="0.911" app:layout_constraintVertical_bias="0.96" 28 android:layout_width="41dp"/> 29</android.support.constraint.ConstraintLayout>
試したこと
似たような処理について書かれている記事が見つからない。Buildも問題なくできている事から原因がわからず、手詰まり状態です。
補足情報(FW/ツールのバージョンなど)
Gradle version 4.6
AndroidStudio 3.2.1
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/11/05 17:08