so I'm trying to make a PagerView via Fragments containing a multiple BaseAdapters in them, but I ran into a little problem with BaseAdapter, I have multiple Fragment, but I'll post 1 as an example
class SpellManagement : Fragment(){
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
listViewSpells?.adapter = AllSpells()
return inflater.inflate(R.layout.activity_spell, container, false)
}
}
class AllSpells: BaseAdapter() {
override fun getCount(): Int {
return player.learnedSpells.size/5+1
}
override fun getItemId(position: Int): Long {
return position.toLong()
}
override fun getItem(position: Int): Any {
return "TEST STRING"
}
override fun getView(position: Int, convertView: View?, viewGroup: ViewGroup?): View {
val rowMain: View
val index:Int = if(position == 0) 0 else{
position*5
}
if (convertView == null) {
val layoutInflater = LayoutInflater.from(viewGroup!!.context)
rowMain = layoutInflater.inflate(R.layout.row_spells_managment, viewGroup, false)
val viewHolder = ViewHolder(rowMain.buttonSpellsManagment1,rowMain.buttonSpellsManagment2,rowMain.buttonSpellsManagment3,rowMain.buttonSpellsManagment4)
rowMain.tag = viewHolder
} else rowMain = convertView
val viewHolder = rowMain.tag as ViewHolder
viewHolder.buttonSpellsManagement1.setBackgroundResource(player.learnedSpells[index]!!.drawable)
viewHolder.buttonSpellsManagement2.setBackgroundResource(player.learnedSpells[index+1]!!.drawable)
viewHolder.buttonSpellsManagement3.setBackgroundResource(player.learnedSpells[index+2]!!.drawable)
viewHolder.buttonSpellsManagement4.setBackgroundResource(player.learnedSpells[index+3]!!.drawable)
viewHolder.buttonSpellsManagement1.setOnClickListener {
}
viewHolder.buttonSpellsManagement2.setOnClickListener {
}
viewHolder.buttonSpellsManagement3.setOnClickListener {
}
viewHolder.buttonSpellsManagement4.setOnClickListener {
}
return rowMain
}
private class ViewHolder(val buttonSpellsManagement1: Button, val buttonSpellsManagement2: Button, val buttonSpellsManagement3: Button, val buttonSpellsManagement4: Button)
}
I was originally using activities, but in the case of PagerView I've changed it to Fragment. What I know is, that I can't call this
etc. what I found in java codes was in some cases getActivity() instead of this
, which I couldn't find in Kotlin. So by saying that it could be in the viewGroup!!.context
in the BaseAdapter.
Any help will be appreciated, thanks.
XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:screenOrientation="landscape">
<android.support.constraint.Guideline
android:id="@+id/guideline9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.8" />
<android.support.constraint.Guideline
android:id="@+id/guideline30"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="15dp"
android:orientation="horizontal"
app:layout_constraintEnd_toStartOf="@+id/listViewSpells"
app:layout_constraintGuide_percent="0.4"
app:layout_constraintStart_toStartOf="parent" />
<android.support.constraint.Guideline
android:id="@+id/guideline10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="87dp"
android:orientation="vertical"
app:layout_constraintEnd_toStartOf="@+id/guideline7"
app:layout_constraintGuide_percent="0.1925"
app:layout_constraintStart_toEndOf="@+id/guideline11" />
<android.support.constraint.Guideline
android:id="@+id/guideline11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="30dp"
android:layout_marginEnd="83dp"
android:orientation="vertical"
app:layout_constraintEnd_toStartOf="@+id/guideline10"
app:layout_constraintGuide_percent="0.05"
app:layout_constraintStart_toStartOf="parent" />
<android.support.constraint.Guideline
android:id="@+id/guideline13"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="86dp"
android:orientation="vertical"
app:layout_constraintEnd_toStartOf="@+id/guideline14"
app:layout_constraintGuide_percent="0.4825"
app:layout_constraintStart_toEndOf="@+id/guideline7" />
<android.support.constraint.Guideline
android:id="@+id/guideline14"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="7dp"
android:orientation="vertical"
app:layout_constraintEnd_toStartOf="@+id/imageView3"
app:layout_constraintGuide_percent="0.63"
app:layout_constraintStart_toEndOf="@+id/guideline13" />
<android.support.constraint.Guideline
android:id="@+id/guideline15"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.774" />
<android.support.constraint.Guideline
android:id="@+id/guideline16"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.92" />
<android.support.constraint.Guideline
android:id="@+id/guideline7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="83dp"
android:orientation="vertical"
app:layout_constraintEnd_toStartOf="@+id/guideline13"
app:layout_constraintGuide_percent="0.34"
app:layout_constraintStart_toEndOf="@+id/guideline10" />
<ListView
android:id="@+id/listViewSpells"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toTopOf="@+id/guideline9"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintEnd_toStartOf="@+id/guideline14"
app:layout_constraintStart_toEndOf="@+id/guideline30"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textViewInfoSpell"
android:layout_width="0dp"
android:layout_height="141dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
app:layout_constraintBottom_toTopOf="@+id/guideline9"
app:layout_constraintDimensionRatio="H,1:1.25"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@+id/guideline14"
app:layout_constraintTop_toTopOf="@+id/guideline30" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="0dp"
android:layout_height="75dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
app:layout_constraintBottom_toTopOf="@+id/guideline30"
app:layout_constraintDimensionRatio="H,1:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@+id/guideline14"
app:layout_constraintTop_toBottomOf="@+id/imageView3" />
<ImageView
android:id="@+id/imageView3"
android:layout_width="0dp"
android:layout_height="47dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="7dp"
app:layout_constraintDimensionRatio="H,"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toEndOf="@+id/guideline14"
app:layout_constraintStart_toStartOf="@+id/guideline14"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
So... this was REALLY strange to me, the answer was to create a new ListView with the old nowadays not very used "view.FindViewById(...)" thingie, which I've never used before because of the plugins, that allows you to straight up write the name of the component such as "listView.adapter = ..." instead of external defining it by "view.findViewById(...)" thingie. Also with the combination of this answer by MinnKhant.The final code looks as follows:
class SpellManagement : Fragment(){
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = inflater!!.inflate(R.layout.activity_spell_managment, container, false)
val listView:ListView = view.findViewById(R.id.listViewSpells)
listView.adapter = AllSpells()
return view
}
}
Cheers
Edit: or just write view.listViewSpells.adapter = AllSpells(), instead of findViewById(...)