I'm trying to refactor my app to use ViewBinding. I've gone through all the fragments and activities; however, I have an ArrayAdapter that I'm unsure of the proper convention to use view binding to prevent memory leaks.
What is the proper way to use a viewbinding in an ArrayAdapter?
I have been using this method for fragments:
private var _binding: BINDING_FILE_NAME? = null
private val binding get() = _binding!!
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
_binding = BINDING_FILE_NAME.inflate(inflater, container, false)
return binding.root
}
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
I call my adapter like so:
var myadapter : MyCustomAdapter = MyCustomAdapter(requireContext(), R.layout.row_autocomplete_item, myListOfStrings())
MyCustomAdapter class
class MyCustomAdapter(ctx: Context, private val layout: Int, private val allItems: List<String>) : ArrayAdapter<String>(ctx, layout, allItems) {
var filteredItems: List<String> = listOf()
override fun getCount(): Int = filteredItems.size
override fun getItem(position: Int): String = filteredItems[position]
#SuppressLint("SetTextI18n")
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
val view = convertView ?: LayoutInflater.from(parent.context).inflate(layout, parent, false)
val item = filteredItems[position]
view.apply {
// HERE IS WHERE I AM NEEDING TO BIND THE VIEW
tvName?.text = item
}
return view
}
override fun getFilter(): Filter {
return object : Filter() {
override fun publishResults(charSequence: CharSequence?, filterResults: FilterResults) {
#Suppress("UNCHECKED_CAST")
filteredItems = filterResults.values as List<String>
notifyDataSetChanged()
}
override fun performFiltering(charSequence: CharSequence?): FilterResults {
val queryString = charSequence?.toString()?.lowercase(Locale.ROOT)
val results = FilterResults()
results.values = if (queryString == null || queryString.isEmpty())
allItems
else
allItems.filter {
it.lowercase(Locale.ROOT).contains(queryString)
}
return results
}
}
}
}
I did like this, its working. But Im not sure, whether it is correct way or not
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
val binding: LayoutCustomSpinnerBinding
var row = convertView
if (row == null) {
val inflater =
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
binding = LayoutCustomSpinnerBinding.inflate(inflater, parent, false)
row = binding.root
} else {
binding = LayoutCustomSpinnerBinding.bind(row)
}
binding.txtContent.text = spinnerList[position].ValueData
return row
}
Based on this answer, got this:
If convertView is not null, then bind to it. Inflate otherwise.
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
val binding: MyLayoutBinding =
if (convertView != null) MyLayoutBinding.bind(convertView)
else MyLayoutBinding.inflate(LayoutInflater.from(context), parent, false)
// use binding
val item = getItem(position)
binding.text = item.name
return binding.root
}
class HoursAdapter(private val hoursList: List<HoursItem>)
:RecyclerView.Adapter<HoursAdapter.HoursViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int):
HoursViewHolder {
val binding = HoursListItemsBinding
.inflate(LayoutInflater.from(parent.context), parent, false)
return HoursViewHolder(binding)
}
override fun getItemCount() = hoursList.size
override fun onBindViewHolder(holder: HoursViewHolder, position: Int) {
with(holder){
with(hoursList[position]) {
binding.topLearnerName.text = name
val hours = "$hours learning hours, $country"
binding.topLearnerTime.text = hours
GlideApp.with(holder.itemView.context)
.load(badgeUrl)
.into(binding.topLearnerImage)
holder.itemView.setOnClickListener {
Toast.makeText(holder.itemView.context, hours,
Toast.LENGTH_SHORT).show()
}
}
}
}
inner class HoursViewHolder(val binding: HoursListItemsBinding)
:RecyclerView.ViewHolder(binding.root)
}
Related
First of all, I am Spanish so my english is not very good.
I have a list of items on a Recyclerview, and I also have a SearchView to filter those items.
Every item has a favourite button, so when you click, the item adds to favorite table.
The problem is that, when I filter something and I start clicking those buttons, odd things happens: some items dissapear from the filtered list. It doesn't happen always, only sometimes. How can I fix this?
My class:
class CoasterFragment : Fragment() {
private val myAdapter by lazy { CoasterRecyclerViewAdapter(CoasterListenerImpl(requireContext(), viewModel),requireContext()) }
private lateinit var searchView: SearchView
private var _binding: FragmentCoasterBinding? = null
private val binding get() = _binding!!
private val viewModel: CoastersViewModel by viewModels()
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
_binding = FragmentCoasterBinding.inflate(inflater, container, false)
val root: View = binding.root
val recyclerView = binding.recyclerCoaster
recyclerView.adapter = myAdapter
recyclerView.layoutManager = LinearLayoutManager(requireContext())
viewModel.coasters().observe(viewLifecycleOwner){myAdapter.setData(it)}
searchView = binding.search
searchView.clearFocus()
searchView.setOnQueryTextListener(object: SearchView.OnQueryTextListener{
override fun onQueryTextSubmit(query: String?): Boolean {
if(query != null){
searchDatabase(query)
searchView.clearFocus()
}
return true
}
override fun onQueryTextChange(query: String?): Boolean {
if(query != null){
searchDatabase(query)
}
return true
}
})
return root
}
fun searchDatabase(query: String) {
val searchQuery = "%$query%"
viewModel.searchDatabase(searchQuery).observe(viewLifecycleOwner) { myAdapter.setData(it)
}
}
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
}
My adapter:
class CoasterRecyclerViewAdapter( val listener: CoasterListener,
val context: Context ) : RecyclerView.Adapter<CoasterRecyclerViewAdapter.ViewHolder>(){
private var coasterList = emptyList<CoasterFavorito>()
class ViewHolder private constructor(val binding: CoasterItemBinding, private val listener: CoasterListener,
private val context: Context): RecyclerView.ViewHolder(binding.root){
companion object{
fun crearViewHolder(parent: ViewGroup, listener: CoasterListener, context: Context):ViewHolder{
val layoutInflater = LayoutInflater.from(parent.context)
val binding = CoasterItemBinding.inflate(layoutInflater, parent, false)
return ViewHolder(binding, listener, context )
}
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = ViewHolder.crearViewHolder(parent, listener, context)
override fun onBindViewHolder(holder: ViewHolder, position: Int){
holder.binding.nombre.text = coasterList[position].coaster.nombre
holder.binding.parque.text = coasterList[position].coaster.parque
holder.binding.ciudad.text = coasterList[position].coaster.ciudad
holder.binding.provincia.text = coasterList[position].coaster.provincia
holder.binding.comunidad.text = coasterList[position].coaster.comunidadAutonoma
Glide
.with(context)
.load(coasterList[position].coaster.imagen)
.centerCrop()
.into(holder.binding.imagen)
holder.binding.check.isChecked = coasterList[position].favorito
holder.binding.check.setOnClickListener{
if (coasterList[position].favorito) {
listener.delFavorito(coasterList[position].coaster.id)
holder.binding.check.isChecked = false
} else {
listener.addFavorito(coasterList[position].coaster.id)
holder.binding.check.isChecked = true
}
}
}
override fun getItemCount(): Int{
return coasterList.size
}
fun setData(coaster: List<CoasterFavorito>){
coasterList = coaster
notifyDataSetChanged()
}
}
interface CoasterListener {
fun addFavorito(id: Long)
fun delFavorito(id: Long)
}
I tried changing the focus, changing the notifydatasetchanged with notifyitemchanged, and nothing happens...
I'm trying to filter a recyclerList that contains around 3000 items.
My filter kinda works but for some reason it doesn't update the list until I scroll far enough.
For example: the top 2 elements start with the letter A --> if my filter starts with B, the top 2 elements still get shown until I scroll far enough so that they are no longer visible. When I scroll back up, they disappeared from the view.
Adapter
class LocationAdapter(
private var locations: ArrayList<Costcenter>,
private val onLocationClick: (location: Costcenter) -> Unit
) : RecyclerView.Adapter<LocationAdapter.LocationViewHolder>(), Filterable {
var locationsFiltered = locations
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): LocationViewHolder {
val view = LayoutInflater
.from(parent.context)
.inflate(R.layout.location_row, parent, false)
return LocationViewHolder(view)
}
override fun getItemCount(): Int = locationsFiltered.size
override fun onBindViewHolder(holder: LocationViewHolder, position: Int) {
holder.bind(locationsFiltered[position])
}
inner class LocationViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
private val title: TextView = itemView.findViewById(R.id.txtCostCenter)
private val desc: TextView = itemView.findViewById(R.id.txtCostCenterDescription)
fun bind(loc: Costcenter) {
title.text = loc.goederenontvanger
desc.text = loc.goederenontvanger_omschrijving
itemView.setOnClickListener {
onLocationClick.invoke(loc)
}
}
}
override fun getFilter(): Filter {
return object : Filter() {
override fun performFiltering(constraint: CharSequence?): FilterResults {
val charSearch = constraint.toString()
locationsFiltered = if (charSearch.isEmpty()) {
locations
} else {
val resultList = ArrayList<Costcenter>()
for (row in locations) {
if (row.goederenontvanger.toLowerCase()
.contains(charSearch.toLowerCase()) || row.goederenontvanger_omschrijving.toLowerCase()
.contains(charSearch.toLowerCase())
) {
resultList.add(row)
}
}
resultList
}
val filterResults = FilterResults()
filterResults.values = locationsFiltered
return filterResults
}
override fun publishResults(constraint: CharSequence?, results: FilterResults?) {
}
}
}
}
Fragment where the recyclerview gets created
class LocationsFragment : Fragment() {
private lateinit var locationAdapter: LocationAdapter
private val storageViewModel by activityViewModels<StorageViewModel>()
private lateinit var currentView : View
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
var view = inflater.inflate(R.layout.fragment_locations, container, false)
var recycler = view.findViewById<RecyclerView>(R.id.recyclerLocations)
var filter = view.findViewById<SearchView>(R.id.editFilter)
locationAdapter = LocationAdapter(Utilities.costcenters) { loc -> setSelectedLocation(loc) }
val recycleMngr: RecyclerView.LayoutManager =
LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
filter.setOnQueryTextListener(object: SearchView.OnQueryTextListener{
override fun onQueryTextSubmit(query: String?): Boolean {
return false
}
override fun onQueryTextChange(newText: String?): Boolean {
locationAdapter.filter.filter(newText)
return false
}
})
recycler.layoutManager = recycleMngr
recycler.adapter = locationAdapter
currentView = view
return view
}
You need to update the original list locationsFiltered with the filtered results when the filter is published with publishResults() and then notifyDataSetChanged() to apply the changes on the RecyclerView
So add the below in publishResults() method:
override fun publishResults(constraint: CharSequence?, results: FilterResults?) {
locationsFiltered = filterResults.values as ArrayList<Costcenter>
notifyDataSetChanged()
}
For a school project, I made a custom arrayadapter with a context, resources and items attribute. Today I received feedback from my teacher and he wants me to find a solution where I don't have a Context attribute because he doesnt like that I always need to specify the context.
This is my code:
class TalentListAdapter(
var mCtx: Context,
var resources: Int,
var items: MutableList<Talent>
) : ArrayAdapter<Talent>(mCtx, resources, items) {
lateinit var mItem: Talent
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
val layoutInflater: LayoutInflater = LayoutInflater.from(mCtx)
val view: View = layoutInflater.inflate(resources, null)
mItem = items[position]
//set name of talent
val talentTextView: TextView = view.findViewById(R.id.talentName)
talentTextView.text = mItem.toString()
return view
}
}
He wants to get rid of the mCtx: Context attribute, but I don't find a solution for it. Any suggestions?
The adapter is created like this atm:
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val listView: ListView = binding.talentList
// set custom adapter for talent_list
val adapter = TalentListAdapter(view.context, R.layout.talentlayout, binding.talentViewModel?.getTalents()?.value as MutableList<Talent>)
listView.adapter = adapter
}
Can you try this
class YourAdapter() : RecyclerView.Adapter<YourAdapter.ViewHolder>() {
inner class ViewHolder(val view: View) : RecyclerView.ViewHolder(view)
var Data: List<YourResponse> = emptyList()
set(value) {
field = value
notifyDataSetChanged()
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(parent.context).inflate(
R.layout.your_layout, parent, false
)
return ViewHolder(view)
}
override fun getItemCount(): Int {
return Data.size
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val getYourModel = Data[position]
val Binding = holder.view
Binding.yourTextView.apply{
text = getYourModel.yourField
}
.....
}
}
and pass Data from your Fragment or Activity (my example uses Retrofit2)
private var theList: List<YourResponse> = emptyList()
private val theAdapter =yourAdapter()
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
onUpdateData()
}
private fun onUpdateData() {
...
override fun onResponse(call: Call<YourResponse>, response: Response<YourResponse>) {
val body = response.body()?.results
if (response.isSuccessful && body != null) {
onUpdateDataSuccess(body)
} else {
// Something
}
}
...
}
private fun onUpdateDataSuccess(data: List<YourResponse>) {
theList = data
theAdapter.Data = data
}
...
You only need context while inflating so you should use parent.context instead of explicit context.
Parent.Context represent the Activity/Fragment where this recycler view is implemented.
I am really new at Kotlin. I tried to make slidable pages using TabLayout. I have 3 different Fragments and I need to add ListView or RecyclerView on that fragments but in this point I can't figure out where is I have to add adapter of ListView. I add adapter class on fragment's Kotlin file but I can't connect that and fragment.(I think) I got an error which is ListView cannot be null so I can say about my adapter didn't working correctly. How can I solve it?
Here I add my codes:
My SecondFragment.kt code:
class SecondFragment : Fragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val listView = view!!.findViewById<ListView>(R.id.main_listview)
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
val listView = view!!.findViewById<ListView>(R.id.main_listview)
return inflater.inflate(R.layout.fragment_second, container, false)
}
private class MyCustomAdapter(context: Context): BaseAdapter(){
private val mContext : Context
private val names = arrayListOf<String>("Test Name", "Steve Jobs", "Tim Cook", "Kobe King")
init {
mContext = context
}
// BURADA KAC TANE ROW OLDUGUNUN SOYLER..
override fun getCount(): Int {
return names.size
}
override fun getItem(position: Int): Any {
return "Test String"
}
override fun getItemId(position: Int): Long {
return position.toLong()
}
override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
//val textView = TextView(mContext)
//textView.text = "Here my first row for list view.."
//return textView
val layoutInflater = LayoutInflater.from(mContext)
val rowMain = layoutInflater.inflate(R.layout.row_for_list, parent, false)
val nameTextView = rowMain.findViewById<TextView>(R.id.name_textView)
nameTextView.text = names.get(position)
val positionTextView = rowMain.findViewById<TextView>(R.id.position_textview)
positionTextView.text = "Row Number: $position"
return rowMain
}
}
}
Here my ViewPagerAdapter.kt file code:
class ViewPagerAdapter(supportFragmentManager: FragmentManager) : FragmentPagerAdapter(supportFragmentManager, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
private val mFragmentList = ArrayList<Fragment>()
private val mFragmentTitleList = ArrayList<String>()
override fun getCount(): Int {
return mFragmentList.size
}
override fun getItem(position: Int): Fragment {
return mFragmentList[position]
}
override fun getPageTitle(position: Int): CharSequence? {
return mFragmentTitleList[position]
}
fun addFragment(fragment: Fragment, title: String){
mFragmentList.add(fragment)
mFragmentTitleList.add(title)
}
}
And here my MainActivity.kt file code:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setUpTabs()
}
private fun setUpTabs() {
val adapter = ViewPagerAdapter(supportFragmentManager)
adapter.addFragment(HomeFragment(), "Home")
adapter.addFragment(SecondFragment(), "Second")
adapter.addFragment(ThirdFragment(), "Third")
viewPager.adapter = adapter
tabs.setupWithViewPager(viewPager)
tabs.getTabAt(0)!!.setIcon(R.drawable.ic_baseline_account_box_24)
tabs.getTabAt(1)!!.setIcon(R.drawable.ic_baseline_add_ic_call_24)
tabs.getTabAt(2)!!.setIcon(R.drawable.ic_baseline_adb_24)
}
}
You have to set to instantiate the adapter and set it to the listView, I also explained some things in the comments.
class SecondFragment : Fragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
//don't do this in onCreate, do it in onViewCreated or onCreateView
//val listView = view!!.findViewById<ListView>(R.id.main_listview)
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
val root = inflater.inflate(R.layout.fragment_second, container, false)
//you can set adapter here if you want, but i did it in onViewCreated
//return it
return root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
//get reference to ListView
val listView = root.findViewById<ListView>(R.id.main_listview)
//set adapter
listView.adapter = MyCustomAdapter(requireContext())
}
private class MyCustomAdapter(private val mContext: Context): BaseAdapter(){
//do initiation here as it needs to be done only once
private val layoutInflater: LayoutInflater = LayoutInflater.from(mContext)
private val names = arrayListOf<String>("Test Name", "Steve Jobs", "Tim Cook", "Kobe King")
// BURADA KAC TANE ROW OLDUGUNUN SOYLER..
override fun getCount(): Int {
return names.size
}
override fun getItem(position: Int): Any {
return "Test String"
}
override fun getItemId(position: Int): Long {
return position.toLong()
}
override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
//val textView = TextView(mContext)
//textView.text = "Here my first row for list view.."
//return textView
val rowMain = layoutInflater.inflate(R.layout.row_for_list, parent, false)
val nameTextView = rowMain.findViewById<TextView>(R.id.name_textView)
nameTextView.text = names.get(position)
val positionTextView = rowMain.findViewById<TextView>(R.id.position_textview)
positionTextView.text = "Row Number: $position"
return rowMain
}
}
}
Try this :
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
val view=inflater.inflate(R.layout.fragment_second, container, false)
val listView = view!!.findViewById<ListView>(R.id.main_listview)
val myListAdapter = MyCustomAdapter(container!!.context)
listView.adapter = myListAdapter
return view
}
I have an ExpandableListView where some groups have children and some not, what I need to do is to expand only the groups that have children.
Part of the body array elements is empty and because of that, I'm getting an IndexOutOfBoundsException
class ExpandableInnerCartAdapter(
var context: Context,
var expandableListView: ExpandableListView,
var header: MutableList<Cart>,
val isTerminadoFragment:Boolean
) : BaseExpandableListAdapter() {
val map = SparseBooleanArray()
var body: List<List<String>> = listOf()
override fun getGroup(groupPosition: Int): Cart {
return header[groupPosition]
}
override fun isChildSelectable(groupPosition: Int, childPosition: Int): Boolean {
return true
}
override fun hasStableIds(): Boolean {
return false
}
fun getCart(): MutableList<Cart> = header
fun getCheckedArray():SparseBooleanArray = map
override fun getGroupView(
groupPosition: Int,
isExpanded: Boolean,
convertView: View?,
parent: ViewGroup?
): View {
var convertView = convertView
if(convertView == null){
val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
convertView = inflater.inflate(R.layout.layout_group,null)
}
val item = header[groupPosition]
body = listOf(item.optionList)
expandableListView.expandGroup(groupPosition)
expandableListView.setGroupIndicator(null)
convertView.item_name.text = item.productName
return convertView
}
override fun getChildrenCount(groupPosition: Int): Int {
return body[groupPosition].size
}
override fun getChild(groupPosition: Int, childPosition: Int): Any {
return body[groupPosition][childPosition]
}
override fun getGroupId(groupPosition: Int): Long {
return groupPosition.toLong()
}
override fun getChildView(
groupPosition: Int,
childPosition: Int,
isLastChild: Boolean,
convertView: View?,
parent: ViewGroup?
): View {
var convertView = convertView
if(convertView == null){
val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
convertView = inflater.inflate(R.layout.layout_child,null)
}
if(getChildrenCount(groupPosition) > 0){
val title = convertView?.findViewById<TextView>(R.id.tv_title)
title?.text = "OpciĆ³n ${childPosition+1} -> ${getChild(groupPosition,childPosition)}"
}
return convertView!!
}
override fun getChildId(groupPosition: Int, childPosition: Int): Long {
return childPosition.toLong()
}
override fun getGroupCount(): Int {
return header.size
}
}
The error seems like it's happening when no group has children and try to do
expandableListView.expandGroup(groupPosition)
I have tried to fix the issue with an if statement:
if(body.isNotEmpty()){
expandableListView.expandGroup(groupPosition)
}
but this solution does not work.
How do I avoid groups that do not have children?
Thanks
As you use Kotlin you have a lot of useful extension functions at your disposal. One of them is filter for which you can specify condition, of course.
The solution would be to filter out empty arrays from the list you set as the new body value:
body = listOf(item.optionList).filter { it.isNotEmpty() }
Filter function definition can be seen here.