IndexOutOfBoundsException ListAdapter - android-recyclerview

I am migrating from usual RecyclerView to ListAdapter:
abstract class BaseAdapter<T>(
#LayoutRes
val normalLayoutId: Int,
#LayoutRes
val loadingLayoutId: Int = R.layout.progress_loading,
sm: (T, T) -> Boolean,
val itemsCells: ArrayList<T?> = ArrayList()
) : ListAdapter<T, RecyclerView.ViewHolder>(TaskDiffCallbac(sm)) {
val VIEW_TYPE_ITEM = 0
val VIEW_TYPE_LOADING = 1
class ItemViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)
class LoadingViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)
fun addData(dataViews: ArrayList<T>) {
itemsCells.addAll(dataViews)
this.submitList(dataViews)
notifyDataSetChanged()
}
override fun getItemViewType(position: Int): Int {
return if (getItem(position) == null) {
VIEW_TYPE_LOADING
} else {
VIEW_TYPE_ITEM
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
return if (viewType == Constant.VIEW_TYPE_ITEM) {
val view =
LayoutInflater.from(parent.context).inflate(normalLayoutId, parent, false)
ItemViewHolder(view)
} else {
val view =
LayoutInflater.from(parent.context).inflate(loadingLayoutId, parent, false)
LoadingViewHolder(view)
}
}
fun addLoadingView() {
itemsCells.add(null)
submitList(itemsCells)
}
fun removeLoadingView() {
if (itemsCells.size != 0) {
itemsCells.removeAt(itemsCells.size - 1)
submitList(itemsCells)
}
}
fun clearItems() {
itemsCells.clear()
submitList(itemsCells)
}
class TaskDiffCallbac<T>(val sm: (T, T) -> Boolean) : DiffUtil.ItemCallback<T>() {
override fun areItemsTheSame(oldItem: T, newItem: T): Boolean {
return sm(oldItem, newItem)
}
#SuppressLint("DiffUtilEquals")
override fun areContentsTheSame(oldItem: T, newItem: T): Boolean {
return oldItem == newItem
}
}
}
but while debugging I get this exception:
java.lang.IndexOutOfBoundsException: Index: 20, Size: 20
2021-02-19 11:38:11.770 5602-5668/? E/AndroidRuntime: FATAL EXCEPTION: pool-1-thread-1
Process: com.timelysoft.shelter, PID: 5602
java.lang.IndexOutOfBoundsException: Index: 20, Size: 20
at java.util.ArrayList.get(ArrayList.java:437)
at androidx.recyclerview.widget.AsyncListDiffer$1$1.areItemsTheSame(AsyncListDiffer.java:306)
at androidx.recyclerview.widget.DiffUtil.diffPartial(DiffUtil.java:268)
at androidx.recyclerview.widget.DiffUtil.calculateDiff(DiffUtil.java:145)
at androidx.recyclerview.widget.DiffUtil.calculateDiff(DiffUtil.java:105)
at androidx.recyclerview.widget.AsyncListDiffer$1.run(AsyncListDiffer.java:292)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
What is the problem?

abstract class BaseAdapter<T>(
#LayoutRes
val normalLayoutId: Int,
#LayoutRes
val loadingLayoutId: Int = R.layout.progress_loading,
sm: (T, T) -> Boolean,
val itemsCells: ArrayList<T?> = ArrayList()
) : ListAdapter<T, RecyclerView.ViewHolder>(CustomDiffCallbac(sm)) {
val VIEW_TYPE_ITEM = 0
val VIEW_TYPE_LOADING = 1
class ItemViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)
class LoadingViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)
fun addData(dataViews: ArrayList<T>) {
itemsCells.addAll(dataViews)
this.submitList(itemsCells)
}
override fun getItemViewType(position: Int): Int {
return if (itemsCells[position] == null) {
VIEW_TYPE_LOADING
} else {
VIEW_TYPE_ITEM
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
return if (viewType == Constant.VIEW_TYPE_ITEM) {
val view =
LayoutInflater.from(parent.context).inflate(normalLayoutId, parent, false)
ItemViewHolder(view)
} else {
val view =
LayoutInflater.from(parent.context).inflate(loadingLayoutId, parent, false)
LoadingViewHolder(view)
}
}
fun addLoadingView() {
itemsCells.add(null)
submitList(itemsCells)
notifyItemInserted(itemsCells.size-1)
}
fun removeLoadingView() {
if (itemsCells.size != 0) {
val index = itemsCells.size - 1
itemsCells.removeAt(index)
submitList(itemsCells)
notifyItemRemoved(index)
}
}
fun clearItems() {
itemsCells.clear()
submitList(itemsCells)
}
class CustomDiffCallbac<T>(val sm: (T, T) -> Boolean) : DiffUtil.ItemCallback<T>() {
override fun areItemsTheSame(oldItem: T, newItem: T): Boolean {
return sm(oldItem, newItem)
}
#SuppressLint("DiffUtilEquals")
override fun areContentsTheSame(oldItem: T, newItem: T): Boolean {
return oldItem == newItem
}
}
}

Related

How to remove fragment disappearance bug in viewPager on back in FragNav on kotlin

How to remove fragment disappearance bug in viewPager on back in FragNav on kotlin
`abstract class TabsFragment : BaseFragment() {
val fragment: ArrayList = arrayListOf()
inner class PagerAdapter(fragmentManager: FragmentManager) :
FragmentStatePagerAdapter(fragmentManager, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
override fun destroyItem(viewGroup: ViewGroup, i: Int, obj: Any) {
Intrinsics.areEqual(viewGroup, "container")
Intrinsics.areEqual(obj, "object")
}
override fun getCount(): Int {
for(index in 0..this#TabsFragment.count()) {
fragment.add(index, BaseFragment())
}
return this#TabsFragment.count()
}
override fun getPageTitle(i: Int): CharSequence? {
return this#TabsFragment.titleTab(i)
}
override fun getItem(position: Int): BaseFragment {
if(!fragment.contains(this#TabsFragment.tabItems(position))) {
fragment.removeAt(position)
fragment.add(position, this#TabsFragment.tabItems(position))
return this#TabsFragment.tabItems(position)
} else {
return fragment[position]
}
}
}
abstract fun tabItems(i: Int): BaseFragment
abstract fun titleTab(i: Int): String?
abstract fun count(): Int
override fun onActivityResult(i: Int, i2: Int, intent: Intent?) {
super.onActivityResult(i, i2, intent)
if (i == 100 && i2 == -1 && intent != null) {
val stringArrayListExtra =
intent.getStringArrayListExtra("android.speech.extra.RESULTS")
val str = stringArrayListExtra!![0]
//EventBusKt.m30817a(OnSearch(str))
}
}
override fun onCreateView(
layoutInflater: LayoutInflater,
viewGroup: ViewGroup?,
bundle: Bundle?
): View? {
val inflate: View =
layoutInflater.inflate(R.layout.fragment_home, viewGroup, false)
//EventBus.getDefault().register(this)
inflate.search_cardView.setOnClickListener {
mFragmentNavigation.pushFragment(SearchFragment.newInstance(mInt + 1))
}
inflate.settings.setOnClickListener {
mFragmentNavigation.pushFragment(MainFragment.newInstance(mInt + 1), right = true)
}
inflate.view_pager.currentItem = PreferenceManager.getDefaultSharedPreferences(requireContext()).getString("home_default_page", "1")?.toInt() ?: 1
val viewPager = inflate.findViewById<View>(R.id.view_pager) as ViewPager
viewPager.adapter = PagerAdapter(childFragmentManager)
//viewPager.offscreenPageLimit = count() - 1
//viewPager.offscreenPageLimit = 5
(inflate.findViewById<View>(R.id.tabs) as TabLayout).setupWithViewPager(viewPager)
return inflate
}
private var BadgeCount: Int? = null
override fun onSaveInstanceState(savedInstanceState: Bundle) {
super.onSaveInstanceState(savedInstanceState)
BadgeCount?.let { savedInstanceState.putInt("BadgeCount", it) }
}
}`

how do i update items that change in recyclerview use diffutil?

DiffUtil do not update items recyclerview with the same id and not same the content,
The model has [id : Int and title : String]. I want to the diffUtilCallBack check items with the same id and not same title and update to recyclerview
Please help me fix it
my DiffUtil CallBAck
class DiffCB(
val oldl : ArrayList<Test>,
val newl : ArrayList<Test>
) : DiffUtil.Callback() {
override fun getOldListSize(): Int {
return oldl.size
}
override fun getNewListSize(): Int {
return newl.size
}
override fun areItemsTheSame(oldItemPos: Int, newItemPos: Int): Boolean {
return oldl[oldItemPos].id == newl[newItemPos].id
}
override fun areContentsTheSame(oldItemPos: Int, newItemPos: Int): Boolean {
return oldl[oldItemPos].title.equals(newl[newItemPos].title)
}
}
my Adapter.kt
class TestAdapter(val click: TestClick) :
RecyclerView.Adapter<TestAdapter.TestVH>() {
var list: ArrayList<Test> = ArrayList()
inner class TestVH(val bin: ItemTestBinding) : RecyclerView.ViewHolder(bin.root) {
fun bind(test: Test) {
bin.tvTestId.text = "[id: ${test.id}]"
bin.tvTestTitle.text = test.title
bin.cbTest.isChecked = test.select
bin.cbTest.setOnClickListener {
click.click(test)
}
}
}
fun sumbitData(newl: ArrayList<Test>) {
val diff = DiffCB(this.list, newl)
val diffResult = DiffUtil.calculateDiff(diff)
list.clear()
this.list.addAll(newl)
diffResult.dispatchUpdatesTo(this)
}
override fun getItemCount(): Int {
return list.size
}
override fun onBindViewHolder(holder: TestVH, position: Int) {
holder.bind(list[position])
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TestVH {
return TestVH(ItemTestBinding.inflate(LayoutInflater.from(parent.context),
parent, false))
}
}
my Activity.kt
class TestActivity : AppCompatActivity(), TestClick {
private var list : ArrayList<Test> = ArrayList()
private lateinit var bin : ActivityTestBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
bin = ActivityTestBinding.inflate(layoutInflater)
setContentView(bin.root)
val viewModel = ViewModelProviders.of(this).get(TestViewModel::class.java)
val manager = LinearLayoutManager(this)
bin.rvTest.layoutManager = manager
val adapter = TestAdapter(this)
bin.rvTest.adapter = adapter
//init first data
viewModel.setTests(initData())
viewModel.getTests().observe(this, Observer {
adapter.sumbitData(it as ArrayList<Test>)
})
bin.btnTest.setOnClickListener{
viewModel.setTests(updateData())
}
}
fun updateData() : ArrayList<Test> {
for (i in 2..6) {
var test = Test()
test.id = i
test.title = "title test $i update"
list.add(test)
}
return list
}
fun initData() : ArrayList<Test> {
for (i in 1..3) {
var test = Test()
test.id = i
test.title = "title test $i"
list.add(test)
}
return list
}
override fun click(test: Test) {
Toast.makeText(this, "${test.id} ${test.title}", Toast.LENGTH_SHORT).show()
}
}
it is first data
after update data, items with the same id have not been updated (id 2, 3)

Show item info on selected item in RecyclerView using Kotlin

I am using kotlin language with android studio. I want to get the properties of the element I clicked in the RecyclerView.
Ben bu kod ile saderc id alabiliyorum
Ex: date
ListAdapter.kt
class ListAdapter(
private val context: Context
) : RecyclerView.Adapter<ListAdapter.ListViewHolder>() {
private var dataList = mutableListOf<Any>()
private lateinit var mListener: onItemClickListener
interface onItemClickListener {
fun onItemClick(position: Int)
}
fun setOnItemClickListener(listener: onItemClickListener) {
mListener = listener
}
fun setListData(data: MutableList<Any>) {
dataList = data
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ListViewHolder {
val view = LayoutInflater.from(context).inflate(R.layout.item_row, parent, false)
return ListViewHolder(view)
}
override fun onBindViewHolder(holder: ListViewHolder, position: Int) {
val question: Questionio = dataList[position] as Questionio
holder.bindView(question)
}
override fun getItemCount(): Int {
return if (dataList.size > 0) {
dataList.size
} else {
return 0
}
}
inner class ListViewHolder(itemView: View, listener: onItemClickListener) :
RecyclerView.ViewHolder(itemView) {
fun bindView(questionio: Questionio) {
itemView.findViewById<TextView>(R.id.txt_policlinic).text = questionio.policlinic
itemView.findViewById<TextView>(R.id.txt_title).text = questionio.title
itemView.findViewById<TextView>(R.id.txt_description).text = questionio.description
itemView.findViewById<TextView>(R.id.txt_date).text = questionio.date
itemView.findViewById<TextView>(R.id.txt_time).text = questionio.time
}
init {
itemView.setOnClickListener {
listener.onItemClick(adapterPosition)
}
}
}
}
My code in onCreateView inside list fragment.Edit
ListFragment
recyclerView.layoutManager = LinearLayoutManager(requireContext())
recyclerView.adapter = adapter
observeData()
adapter.setOnItemClickListener(object : ListAdapter.onItemClickListener {
override fun onItemClick(position: Int) {
showShortToast(position.toString())
}
})
this function is also my observationData(),
I made new edits
private fun observeData() {
binding.shimmerViewContainer.startShimmer()
listViewModel.fetchQuestinData("questions",
requireContext())
.observe(viewLifecycleOwner, {
binding.shimmerViewContainer.startShimmer()
binding.shimmerViewContainer.hideShimmer()
binding.shimmerViewContainer.hide()
adapter.setListData(it)
adapter.notifyDataSetChanged()
})
}
You can pass highOrderFuction into the adapter then setonclickListener for any view you want. Like this:
class ListAdapter(
private val context: Context,
private val onItemClick:(questionio: Questionio)->Unit
) : RecyclerView.Adapter<ListAdapter.ListViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ListViewHolder {
val view = LayoutInflater.from(context).inflate(R.layout.item_row, parent, false)
return ListViewHolder(view,onItemClick)
}
...
inner class ListViewHolder(itemView: View,private val onItemClick:(questionio: Questionio)->Unit) : RecyclerView.ViewHolder(itemView) {
fun bindView(questionio: Questionio) {
//set on any view you want
itemView.findViewById<TextView>(R.id.root_view_id).
setOnClickListener{onItemClick(questionio)}
itemView.findViewById<TextView>(R.id.txt_policlinic).text =
questionio.policlinic
itemView.findViewById<TextView>(R.id.txt_title).text = questionio.title
itemView.findViewById<TextView>(R.id.txt_description).text =
questionio.description
itemView.findViewById<TextView>(R.id.txt_date).text = questionio.date
itemView.findViewById<TextView>(R.id.txt_time).text = questionio.time
}
}
}

view not created for fragment of viewpager inside recyclerview

I want to make Home screen like Instagram where video will play/pause on horizontal and vertical scrolling.
For that I am using fragment inside viewpager and viewpager inside recyclerview for achieving horizontal and vertical scrolling.
Issue is that for second item of recyclerview view of fragment is not created.
can anyone help me to achieve this functionality.
Thanks in advance.
Below is the adapter class of recyclerview.
class DashboardFeedsAdapterNew(
private val mContext: Context, var mList: ArrayList<PostListModel.PostListData>,
private val viewHolderClicks: ViewHolderClicks,
) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
var mInflater: LayoutInflater =
mContext.getSystemService(Activity.LAYOUT_INFLATER_SERVICE) as LayoutInflater
var mActivePage = 0
private var isLoadingAdded = false
val LOADING_TYPE = 0
val POST_TYPE = 1
val STORY_TYPE = 2
fun getList(): ArrayList<PostListModel.PostListData> {
return mList
}
override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int
): RecyclerView.ViewHolder {
when (viewType) {
POST_TYPE -> {
val v = LayoutInflater.from(parent.context)
.inflate(R.layout.raw_feed, parent, false)
return PostViewHolder(v)
}
}
val v =
mInflater.inflate(R.layout.raw_feed, parent, false)
return PostViewHolder(
v
)
}
override fun getItemViewType(position: Int): Int {
if(position == mList.size -1 && isLoadingAdded) {
return LOADING_TYPE
} else {
/*if ((position + 1) % 5 == 0) {
return STORY_TYPE
}*/
return POST_TYPE
}
}
override fun onBindViewHolder(
holder: RecyclerView.ViewHolder,
position: Int
) {
val model = mList[holder.adapterPosition]
if (holder is PostViewHolder) {
//post type
try {
if (model.files.isNotEmpty()) {
setupViewpager(holder, model)
}
} catch (e: Exception) {
e.printStackTrace()
}
}
}
private fun setupViewpager(binding: PostViewHolder, model: PostListModel.PostListData) {
binding.vpAttachment.adapter = ViewPagerAdapter((activityContext() as AppCompatActivity).supportFragmentManager,model)
binding.vpAttachment.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
override fun onPageScrolled(
position: Int,
positionOffset: Float,
positionOffsetPixels: Int
) {
mActivePage = binding.vpAttachment.currentItem
}
override fun onPageSelected(position: Int) {
}
override fun onPageScrollStateChanged(state: Int) {}
})
}
inner class ViewPagerAdapter(fm: FragmentManager, val mData: PostListModel.PostListData) :
FragmentStatePagerAdapter(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT ) { //FragmentPagerAdapter
private val mFragmentList = java.util.ArrayList<Fragment>()
private val mFragmentTitleList = java.util.ArrayList<String>()
fun addFragment(fragment: Fragment, title: String) {
mFragmentList.add(fragment)
mFragmentTitleList.add(title)
}
//added this metod to update fragment after edit post
override fun getItemPosition(`object`: Any): Int {
return POSITION_NONE
}
override fun getItem(position: Int): Fragment {
mData.files[position].position = position
return PostMediaPreviewPageFragment.newInstance(position,mData.id, mData.files[position])
}
override fun getCount(): Int {
return mData.files.size
}
override fun getPageTitle(position: Int): CharSequence? {
return mFragmentTitleList[position]
}
}
private fun activityContext(): Context {
val context = mContext
return context
}
override fun getItemCount(): Int {
return mList.size
}
inner class ViewMoreViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val progressBar: ProgressBar = itemView.findViewById(R.id.progressBar)
}
inner class PostViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val imgAvatar: ImageView = itemView.findViewById(R.id.imgAvatar)
val tvUserName: TextView = itemView.findViewById(R.id.tvUserName)
val vpAttachment: ViewPager = itemView.findViewById(R.id.vpAttachment)
// val indicator: IndicatorView = itemView.findViewById(R.id.indicator)
val imgFeed: ImageView = itemView.findViewById(R.id.imgFeed)
val imgPlay: ImageView = itemView.findViewById(R.id.imgPlay)
val tvAttachmentCount: TextView = itemView.findViewById(R.id.tvAttachmentCount)
val tvFeedTitle: TextView = itemView.findViewById(R.id.tvFeedTitle)
val tvLocation: TextView = itemView.findViewById(R.id.tvLocation)
val tvViewCount: TextView = itemView.findViewById(R.id.tvViewCount)
val imgLike: ImageView = itemView.findViewById(R.id.imgLike)
val tvLikeCount: TextView = itemView.findViewById(R.id.tvLikeCount)
val lnComment: LinearLayout = itemView.findViewById(R.id.lnComment)
val tvCommentCount: TextView = itemView.findViewById(R.id.tvCommentCount)
val btnTakeAction: Button = itemView.findViewById(R.id.btnTakeAction)
}
inner class StoryViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val rvStory: RecyclerView = itemView.findViewById(R.id.rvStory)
val viewPager: ViewPager2 = itemView.findViewById(R.id.vpStory)
val indicator: CircleIndicator2 = itemView.findViewById(R.id.indicator)
}
interface ViewHolderClicks {
fun onUserClick(model: PostListModel.PostListData, position: Int)
fun onImageClick(model: PostListModel.PostListData, position: Int)
fun onLikeClick(model: PostListModel.PostListData, position: Int)
fun onLikeCountClick(model: PostListModel.PostListData, position: Int)
fun onCommentClick(model: PostListModel.PostListData, position: Int)
fun onTakeActionClick(model: PostListModel.PostListData, position: Int)
}
}

ExpandableListView Multilevel in Kotlin groupsPosition of firstChild always 0

i'm new in Kotlin and learning this Coding-Language right now.
At the moment i get stuck on the Problem that in my Multi-Level expandablelistview in secondlevel the groupPosition is always "0".
The view shows always 3 times the same objects, but it must be 3 different objects.
The Data comes from a local running SQL-Lite Database and the Databasequery works fine.
Here is my code, I hope you can help me!
class pflegeRabGru :AppCompatActivity(){
val ctx:Context = this
internal var expandableListView: ExpandableListView?=null
internal var listAdapter: ExpandableListAdapter?=null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_pflege_rabgru)
expandableListView= findViewById(R.id.expRabListView)
if (expandableListView !=null){
listAdapter=adapter_RabGruKat(this)
expandableListView!!.setAdapter(listAdapter)
}
}
The First Adapter
adapter_RabGruKat
class adapter_RabGruKat internal constructor(private val context: Context):BaseExpandableListAdapter() {
val lclDb=lclDBHelper(context)
val listRabgru = ArrayList<ArtRabGru>()
val listRabFields = ArrayList<RabGruFields>()
val listCats = ArrayList<Categorys>()
val listReadedRabgru= getRabForGroups()
val listReadedRabKats = getKatsForGroups()
override fun getChild(groupPosition: Int, childPosition: Int):Int{
return childPosition
}
override fun getChildId(groupPosition: Int, childPosition: Int): Long{
return childPosition.toLong()
}
override fun getChildView(groupPosition: Int, childPosition: Int, isLastChild: Boolean, convertView: View?, parent:ViewGroup):View{
var convertView = convertView
var subObjects = ExpandableListView(context)
subObjects!!.setAdapter(adapter_RabGruKatChilds(context,childPosition.toLong(),getGroup(groupPosition).get(groupPosition).WAHID))
subObjects!!.setGroupIndicator(null)
return subObjects
}
override fun getChildrenCount(groupPosition: Int): Int {
return listCats.size
}
override fun getGroup(groupPosition: Int):ArrayList<ArtRabGru>{
return listRabgru
}
override fun getGroupCount(): Int {
return listRabgru.size
}
override fun getGroupId(groupPosition: Int): Long {
return groupPosition.toLong()
}
override fun getGroupView(groupPosition: Int,isExpanded: Boolean, convertView: View?, parent: ViewGroup): View {
var convertView = convertView
val groupTitle = getGroup(groupPosition).get(groupPosition).WAH_RABGRU + " " + getGroup(groupPosition).get(groupPosition).WAH_RABGRUTEXT
val groupId = getGroup(groupPosition).get(groupPosition).WAHID
if (convertView==null){
val layoutInflater = this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
convertView = layoutInflater.inflate(R.layout.parent_pflege_rabgru, null)
}
val listRabGruTitle = convertView!!.findViewById<TextView>(R.id.txtRabgruName)
val listRabId = convertView!!.findViewById<TextView>(R.id.txtRabGruId)
listRabGruTitle.text=groupTitle
listRabId.text=groupId.toString()
return convertView
}
override fun hasStableIds(): Boolean {
return true
}
override fun isChildSelectable(groupPosition: Int, childPosition: Int):Boolean {
return true
}
fun getRabForGroups():ArrayList<ArtRabGru>{
lclDb.readAllArtRabGru().forEach() {
listRabgru.add(ArtRabGru(it.WAHID,it.WAH_RABGRU,it.WAH_RABGRUTEXT))
}
return listRabgru
}
fun getKatsForGroups():ArrayList<Categorys>{
lclDb.readAllCategory().forEach {
listCats.add(Categorys(it.KATID,it.KATNAME))
}
return listCats
}
}
and the SecondLevelAdapter
class adapter_RabGruKatChilds internal constructor(private val context:Context, private val childPositionOtherAdapter:Long,private val selectedRabgru:Long) : BaseExpandableListAdapter() {
val lclDb = lclDBHelper(context)
var listCats = ArrayList<Categorys>()
val readCats = getKatsForGroups()
val rabFields = ArrayList<RabGruFields>()
val readRabFields = getFieldRabs()
override fun getChild(childPosition: Int, groupPosition: Int):RabGruFields {
return rabFields.get(childPosition)
}
override fun getChildId(groupPosition:Int, childPosition:Int): Long {
return childPosition.toLong()
}
override fun getChildView(groupPosition: Int, childPosition: Int, isLastChild: Boolean, convertView: View?, parent:ViewGroup): View {
var convertView = convertView
val fieldId= getChild(childPosition,groupPosition).RABFIELDID
val fieldName= getChild(childPosition,groupPosition).RABFIELDTEXT
if (convertView==null){
val layoutInflater = this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
convertView=layoutInflater.inflate(R.layout.pflegefieldsitems, null)
}
val txtfieldId = convertView!!.findViewById<TextView>(R.id.txtFieldId)
val txtfieldName = convertView!!.findViewById<TextView>(R.id.txtFieldName)
txtfieldId.text=fieldId.toString()
txtfieldName.text=fieldName
return convertView
}
override fun getChildrenCount(groupPosition: Int): Int {
return rabFields.size
}
override fun getGroup(groupPosition: Int):Categorys{
Log.i("INFO", "GroupPosition in getGroupt="+ groupPosition.toString())
return listCats.get(groupPosition)
}
override fun getGroupCount(): Int {
return listCats.size
}
override fun getGroupId(groupPosition: Int):Long {
return groupPosition.toLong()
}
override fun getGroupView(groupPosition: Int,isExpanded:Boolean, convertView:View?, parent:ViewGroup): View? {
var convertView = convertView
val groupCatId = getGroup(groupPosition).KATID
val groupTitle = getGroup(groupPosition).KATNAME
if(convertView==null){
val layoutInflater = this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
convertView = layoutInflater.inflate(R.layout.parent_pflege_rabgru, parent,false)
}
val txtCatId = convertView!!.findViewById<TextView>(R.id.txtRabGruId)
val txtCatName = convertView!!.findViewById<TextView>(R.id.txtRabgruName)
txtCatId.text= groupCatId.toString()
txtCatName.text = groupTitle
Log.i("INFO", "GroupPosition in getGroupView=" + groupPosition.toString())
return convertView
}
override fun hasStableIds(): Boolean {
return true
}
override fun isChildSelectable(groupPosition: Int, childPosition: Int): Boolean {
return true
}
fun getKatsForGroups():ArrayList<Categorys>{
listCats.clear()
lclDb.readAllCategory().forEach {
listCats.add(Categorys(it.KATID,it.KATNAME))
}
return listCats
}
fun getFieldRabs(): ArrayList<RabGruFields>{
rabFields.clear()
lclDb.readAllRabFields(selectedRabgru).forEach{
rabFields.add(RabGruFields(it.RABID, it.RABGRUID,it.RABFIELDID,it.RABFIELDMANDATORY,it.RABFIELDSHOW,it.RABFIELDTEXT,it.RABFIELDRANDOMIZE))
}
return rabFields
}
}