0

ViewHolder seems to be very helpful to access every view on a listview. I have found some code examples where there was a viewholder in an adapter listview (used in the getview function).

But when I attempt to implement a viewHolder in that function, Android Studio wants to import a recyclerview class (viewHolder) so I think it is not normal.

What I have tried is putting val viewHolder = RecyclerView.ViewHolder(convertView) into getview function of my adapter. And then I have got import androidx.recyclerview.widget.RecyclerView ...

What I want to do is it possible nowadays?

Here is a sample code that I have found on the web and I am not able to do the same thing:

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.TextView

class MyCustomAdapter(context: Context, val items: List<String>) : BaseAdapter() {
    private val layoutInflater = LayoutInflater.from(context)

    override fun getCount(): Int {
        return items.size
    }

    override fun getView(position: Int, view: View?, viewGroup: ViewGroup?): View? {
        val viewHolder: ViewHolder
        val rowView: View?

        if (view == null) {
            rowView = layoutInflater.inflate(R.layout.list_item, viewGroup, false)

            viewHolder = ViewHolder(rowView)
            rowView.tag = viewHolder

        } else {
            rowView = view
            viewHolder = rowView.tag as ViewHolder
        }

        viewHolder.itemName.text = items[position]

        return rowView
    }

    override fun getItem(position: Int): Any {
        return 0
    }

    override fun getItemId(position: Int): Long {
        return 0
    }

    private class ViewHolder(view: View?) {
        val itemName = view?.findViewById(R.id.list_item_text_view) as TextView

    }
}
fdermishin
  • 3,519
  • 3
  • 24
  • 45
jujuf1
  • 175
  • 2
  • 10
  • In the very beginning, along with the title, you were talkin' 'bout list view. Then you you talk about RecyclerView. Is it on purpose or are you confused between both? – private static Dec 23 '20 at 10:12
  • I talk about listview. I don't want to use recyclerview. But android studio import a class from recycler view...that is the problem. – jujuf1 Dec 23 '20 at 10:14
  • 1
    I would suggest using a RecyclerView instead. However, this link https://stackoverflow.com/questions/8166497/custom-adapter-for-list-view is for using a custom adapter with a listview in **java**. I can't help any further coz I know nothing about kotlin. Hope the link might be helpfula bit. – private static Dec 23 '20 at 10:18
  • 1
    And if you dont get a solution, try googling "listview with custom adapter in kotlin". – private static Dec 23 '20 at 10:19
  • 1
    Thanks :) I can see it is necessary to create own viewholder class. – jujuf1 Dec 23 '20 at 10:21

1 Answers1

2

My first advice just from your reading your question, is that you should probably use a RecyclerView instead of a ListView. Is way more efficient and more "correct".

The sample code shows a private ViewHolder just for that list, which means it's using the same ViewHolder paradigm and not the Recycler.ViewHolder class.

Soo in order to mimic the sample code you need to create your own private ViewHolder class instead of instancing the one made for RecyclerView (Not listView)

  • Thanks for your reply. I try too to use recyclerview but i am unable to refresh recyclerview from dataset whithout anyproblem, because I use EditText. – jujuf1 Dec 23 '20 at 13:19