ReyclerView isn't working
You should return
the size
of your data
ArrayList in getItemCount()
Use this:
@Override
public int getItemCount() {
return data.size();
}
Instead of this:
@Override
public int getItemCount() {
return 0;
}
selection not working in reyclerview item in android
Your adapter never reads from selectedItemPosition
, so how could it ever change which item is shown as selected?
By using the defaultValue
from the list items every time you bind the items, they will never be able to change either.
To simplify this, let your adapter treat selectedItemPosition
as the single source of truth about what should be selected. The Activity should tell it which item to start with:
val selectionAdapter = SelectionAdapter()
val list = createList()
selectionAdapter.selectedItemPosition = list.indexOfFirst { it.defaultValue == true }
selectionAdapter.submitList(list)
Your adapter should exclusively use selectedItemPosition
as its source for whether an item should appear selected. And the click listener only needs to change the selected item. It's pointless to have it change the item's appearance manually because this can only briefly change how it looks until the next time the view holder is bound.
override fun onBindViewHolder(holder: SelectionViewHolder, position: Int) {
holder.binding.root.setOnClickListener {
selectedItemPosition = holder.adapterPosition
}
holder.bindItem(getItem(position), selectedItemPosition == position)
}
inner class SelectionViewHolder(val binding: SelectionItemLayoutBinding) :
RecyclerView.ViewHolder(binding.root) {
fun bindItem(item: MainActivity.Data, isSelected: Boolean) = with(binding) {
textView.text = item.value
root.isSelected = isSelected
}
}
RecyclerView EmptyView has RecyclerItem
I find out one issue, in your empty adapter, your code
@Override
public int getItemCount() {
return 0;
}
so nothing be show, if you want show your empty text, please using
@Override
public int getItemCount() {
return 1;
}
RecyclerView doesn't set adapter
you forgot to set size in your adapter:-
@Override
public int getItemCount() {
return mDataset.size();
}
Recycler View Not Showing Items
You're returning zero for the itemCount, therefore your adapter thinks you don't have any items. Try this in your adapter:
@Override
public int getItemCount() {
return itemList.size();
}
ViewPager2 not working with RecyclerView.Adapter
change return type in ImagesViewerFragment.java to
return fragmentImagesViewerBinding.getRoot();
Firebase ReyclerView adapter not displaying on activity start
you need to use the messageAdapter.notifyDataSetChanged();
inside the indide the firebase request not outside it cause it's asynchrone so firebase doesn't block the main thread until it finish
ListAdapter not updating item in RecyclerView
Edit: I understand why this happens that wasn't my point. My point is that it at least needs to give a warning or call the notifyDataSetChanged()
function. Because apparently I am calling the submitList(...)
function for a reason. I am pretty sure people are trying to figure out what went wrong for hours until they figure out the submitList() ignores silently the call.
This is because of Google
s weird logic. So if you pass the same list to the adapter it does not even call the DiffUtil
.
public void submitList(final List<T> newList) {
if (newList == mList) {
// nothing to do
return;
}
....
}
I really don't understand the whole point of this ListAdapter
if it can't handle changes on the same list. If you want to change the items on the list you pass to the ListAdapter
and see the changes then either you need to create a deep copy of the list or you need to use regular RecyclerView
with your own DiffUtill
class.
Related Topics
Java.Lang.Noclassdeffounderror: Failed Resolution Of: Landroid/Support/V4/Os/Buildcompat
Nexus 5X Reverse Landscape Sensor Fix in a Android Camera Preview App
Android - Build Separate APKs for Different Processor Architectures
Android Change Text Color of Items in Spinner
The Import Android.Support.V7.App Cannot Be Resolved
How to Store and Retrieve a Byte Array (Image Data) to and from a SQLite Database
Display Unity Scene as Sub View in Android Studio
Pass Arraylist Data into Soap Web Service in Android
Call Getlayoutinflater() in Places Not in Activity
How to Scroll to Top of Long Scrollview Layout
Android Studio: How to Generate Signed APK Using Gradle
Load Image from Url in Notification Android
How to Change Letter Spacing in a Textview
View Binding - How to Get a Binding for Included Layouts
Android Set Custom Font to a Paint
Display Fb Profile Pic in Circular Image View in Application