前提・実現したいこと
listviewに追加されたアダプターのボタンから画面遷移が行いたいです。
発生している問題
画面遷移を実装するにあたって
MyArrayAdapterクラス内に
kotlin
1 fun onViewActivitymove(view: View){ 2 startActivity(Intent(this, ViewActivity::class.java)) 3 } 4 viewHolder.morningIcon.setOnClickListener { 5 onViewActivitymove(it) 6 }
を追加したのですが。build時に下記のエラーが発生してしまいます。
public constructor Intent() defined in android.content.Intent
public constructor Intent(p0: Context!, p1: Class<>!) defined in android.content.Intent
public constructor Intent(p0: Intent!) defined in android.content.Intent
public constructor Intent(p0: String!) defined in android.content.Intent
public constructor Intent(p0: String!, p1: Uri!) defined in android.content.Intent
public constructor Intent(p0: String!, p1: Uri!, p2: Context!, p3: Class<>!) defined in android.content.Intent
恐らくIntenの引数が不足しているというエラーだと思うのですが
MainActivity内にコードを移すと、問題なくbuildができるという点が理解できません。
list内のボタンから画面遷移を行うにはどのようにすればいいのでしょうか。
該当のソースコード
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 //初期のリスト項目を設定 34 val arrayAdapter = MyArrayAdapter(this, 0).apply{ 35 floatingActionButton.setOnClickListener { 36 val dialog = DatePickerFragment() 37 dialog.show(supportFragmentManager, "date_dialog") 38 add(ListItem("")) 39 } 40 } 41 //ListViewにリスト項目とArrayAdapterを設定 42 val listView: ListView = findViewById(R.id.listView) 43 listView.adapter = arrayAdapter 44 45 } 46} 47 48 49class ListItem(val date_data:String){} 50//リスト項目を再利用するためのホルダー 51data class ViewHolder(val date_dataView: TextView,val morningIcon:ImageButton,val lunchIcon:ImageButton,val dinnerIcon:ImageButton) 52 53//自作のリスト項目データを扱えるようにしたArrayAdapter 54class MyArrayAdapter : ArrayAdapter<ListItem> { 55 private var inflater : LayoutInflater? = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)as LayoutInflater? 56 57 constructor(context: Context,resource :Int):super(context,resource){} 58 override fun getView(position:Int, convertView: View?, parent: ViewGroup?):View{ 59 60 var viewHolder : ViewHolder? = null 61 var view = convertView 62 63 //再利用の設定 64 if(view == null){ 65 66 view = inflater!!.inflate(R.layout.list_item,parent,false) 67 68 viewHolder = ViewHolder( 69 view.findViewById(R.id.date_button), 70 view.findViewById(R.id.morning_button), 71 view.findViewById(R.id.lunch_button), 72 view.findViewById(R.id.dinner_button) 73 ) 74 view.tag = viewHolder 75 }else{ 76 viewHolder = view.tag as ViewHolder 77 } 78 79 //項目の情報設定 80 val listItem = getItem(position) 81 viewHolder.date_dataView.text =listItem!!.date_data 82 83 viewHolder.morningIcon.setOnClickListener{ 84 //削除ボタンを押したときの処理 85 this.remove(listItem) 86 this.notifyDataSetChanged() 87 } 88 return view!! 89 } 90} 91
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>
補足情報(FW/ツールのバージョンなど)
Gradle version 4.6
AndroidStudio 3.2.1
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/11/08 11:55