RecyclerView.Adapter onBindViewHolder() gets wrong position - android

I'll show the code and after the steps to get the problem.
I have a recyclerview inside a tabbed fragment that takes the dataset from a custom object:
mRecyclerView = (RecyclerView) v.findViewById(R.id.recyclerview);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerAdapter = new MyRecyclerAdapter(mMes.getListaItens(), this, getActivity());
mRecyclerView.setAdapter(mRecyclerAdapter);
I set the longclick behavior of the list items in onBindViewHolder() of the adapter:
#Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
ItemMes item = mListaItens.get((position));
holder.descricao.setText(item.getDescrição());
holder.valor.setText(MainActivity.decimalFormatWithCod.format(item.getValor()));
...
holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
new MaterialDialog.Builder(mContext)
.title(holder.descricao.getText().toString())
.items(R.array.opcoes_longclick_item)
.itemsCallbackSingleChoice(-1, new MaterialDialog.ListCallbackSingleChoice() {
#Override
public boolean onSelection(MaterialDialog dialog, View view, int which, CharSequence text) {
switch (which) {
case 0:
mParentFragment.showUpdateItemDialog(position);
return true;
case 1:
mParentFragment.showDeleteItemDialog(position);
return true;
}
return false;
}
})
.show();
return true;
}
});
}
Then, the methods in the fragment that take care of delete the item itself:
public void showDeleteItemDialog(int position) {
final ItemMes item = mMes.getListaItens().get(position);
new MaterialDialog.Builder(getActivity())
.title("Confirmar Remoção")
.content("Tem certeza que deseja remover " + item.getDescrição() + "?")
.positiveText("Sim")
.negativeText("Cancelar")
.onPositive(new MaterialDialog.SingleButtonCallback() {
#Override
public void onClick(#NonNull MaterialDialog dialog, #NonNull DialogAction which) {
deleteItem(item);
}
})
.show();
}
public void deleteItem(ItemMes item) {
getMainActivity().deleteItemFromDatabase(item.getID());
int position = mMes.getListaItens().indexOf(item);
mMes.getListaItens().remove(position);
mRecyclerAdapter.notifyItemRemoved(position);
atualizaFragment();
}
And finally the method in activity that do the DB operation:
public int deleteItemFromDatabase(long id) {
SQLiteDatabase db = dataBaseHelper.getWritableDatabase();
String where = DBHelper.COLUNA_ID + " = ?";
String[] args = {String.valueOf(id)};
int rowsAffected = db.delete(DBHelper.TABELA_ITEM, where, args);
db.close();
return rowsAffected;
}
Now i'll reproduce the steps:
I'm showing 3 itens in the listview. Then I try to remove the first:
1 - The longclick is intercepted passing the correct index:
2 - The item is correctly deleted from the database:
3 - After all this, as expected, the adapter is storing and showing 2 items...
SO, if I try to delete the first item of this 2 item list I get the wrong position (should be 0, is 1):
And also if I try to delete the last item of this 2 item list I get the wrong position (should be 1, is 2):
The question is: If I have a dataset of size 2 (and the adapter knows it), how can it call onBindViewHolder(ViewHolder holder, int [last index +1])?
I have no idea what could be wrong. So I ask help cause I'm thinking about give up this project cause I do everything right but always something dont works, and Im tired.
Thanks in advance.

I've noticed that in method onBindViewHolder(VH holder, int position) while the position was comming wrong, the holder.getAdapterPosition() gives me always the correct position.
So I changed my code from:
ItemMes item = mListaItens.get((position));
...
mParentFragment.showUpdateItemDialog(position);
...
mParentFragment.showDeleteItemDialog(position);
....
To:
ItemMes item = mListaItens.get((holder.getAdapterPosition()));
...
mParentFragment.showUpdateItemDialog(holder.getAdapterPosition());
...
mParentFragment.showDeleteItemDialog(holder.getAdapterPosition());
....
And everything works well. This is very strange but...
Thanks everybody.

Took a look at the adapter code you provided in the comment and it's pretty straightforward. Try this: rather than call notifyItemRemoved(), call notifyDataSetChanged(). This is rather expensive as it will cause your adapter to re-bind the data set (and re-create ViewHolders), but since you're using an ArrayList where you are removing an element, it's really the simplest way to do it. Otherwise you'll have to track the position of the items and when an item is removed it cannot change the position of other items - or handle the case where items shift their position in the data set.

Try this code in onBindViewHolder()
int adapterPos=holder.getAdapterPosition();
if (adapterPos<0){
adapterPos*=-1;
}
ItemMes item = mListaItens.get((adapterPos));
mParentFragment.showUpdateItemDialog(adapterPos);
Use adapterPos instead of position variable.

According to RecyclerView's getAdapterPosition documentation:
RecyclerView does not handle any adapter updates until the next layout traversal. This
may create temporary inconsistencies between what user sees on the screen and what
adapter contents have. This inconsistency is not important since it will be less than
16ms but it might be a problem if you want to use ViewHolder position to access the
adapter. Sometimes, you may need to get the exact adapter position to do
some actions in response to user events. In that case, you should use this method which
will calculate the Adapter position of the ViewHolder.
So in case of implementing user events, using getAdapterPosition is a recommended way to go.

Related

Get spinner selected item inside RecyclerView

I have added Spinner inside RecyclerView , when i am trying to get spinner selected item data, its getting another/wrong position data, any one suggest me to get correct selected item and position from Spinner onItemSelected
Here is my code
#Override
public void onBindViewHolder(final QuestionHolder holder, final int position) {
if (position % 2 == 1)
holder.itemView.setBackgroundColor(Color.parseColor("#F8F8F8"));
adapter = new ArrayAdapter<Option>(binding.getRoot().getContext(),
R.layout.item_spinner, questionList.get(position).getOptions());
adapter.setDropDownViewResource(R.layout.item_spinner);
binding.optionSpinner.setAdapter(adapter);
binding.serialNo.setText((position + 1) + ".");
binding.setQuestion(questionList.get(position));
binding.executePendingBindings();
binding.optionSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(holder.itemView.getContext(), position+" : "+binding.optionSpinner.getSelectedItem().toString(), Toast.LENGTH_SHORT).show();
spinnerData.setSelectedData(position, binding.optionSpinner.getSelectedItem().toString());
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
I think you have to try this
showSpinnerList.setOnItemSelectedListener(new
AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int
position, long id) {
// On selecting a spinner item
String item = parent.getItemAtPosition(position).toString();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// todo for nothing selected
}
});
Check this, can be helpful and may fix your problem. If this won't fix your problem at least you get rid of Lint error. Lint error “Do not treat position as fixed; only use immediately…”. So everywhere you are using final int position** change it to getAdapterPosition();
The documentation of RecyclerView.Adapter.onBindViewHolder()
states:
Note that unlike ListView, RecyclerView will not call this method
again if the position of the item changes in the data set unless the
item itself is invalidated or the new position cannot be determined.
For this reason, you should only use the position parameter while
acquiring the related data item inside this method and should not keep
a copy of it. If you need the position of an item later on (e.g. in a
click listener), use getAdapterPosition() which will have the updated
adapter position
So, technically items may be re-arranged and binding will not be
necessary because items are not invalidated yet. The position
variable received holds true only for the scope of bind function and
will not always point to correct position in the data set. That's why
the function getAdapterPosition() must be called everytime updated
position is needed.
IMHO, mLastPosition = holder.getAdapterPosition(); is still
potentially wrong. Because item may be re-arranged and mLastPosition
still points to old position.
About why lint is silent, perhaps Lint rule is not that thorough. Its
only checking whether position parameter is being copied or not.

RecyclerView ItemTouchHelper getAdapterPosition() returns wrong value on fast swipe

I have a problem regarding RecyclerView + Swipe to delete + undo with the help of a snackbar. Whenever I try to fast swipe 3+ items to delete them, viewHolder.getAdapterPosition(); returns a wrong value. Seems like it can't react that fast.
RecyclerView list Example:
Test1
Test2
Test3
Test4
So, if I quickly swipe away Test3, Test2 and then Test1 viewHolder.getAdapterPosition(); returns the positions 2, 2 and then 0.
The correct return has to be 2, 1 and then 0.
For two items everything behaves normal, returns 2 and then 1.
Note: Last position is always returned 2 seconds later, because snackbar times out and then the item gets deleted. The snackbars which show before get dismissed as soon as the next item gets swiped away.
Any ideas how to solve this?
I tried to figure it out for several hours now.
ItemTouchHelper itemTouchHelper = new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT) {
#Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
return false;
}
#Override
public void onSwiped(final RecyclerView.ViewHolder viewHolder, int swipeDir) {
final int position = viewHolder.getAdapterPosition();
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
rusure = Snackbar.make(fab, getResources().getString(R.string.rule_deleted), Snackbar.LENGTH_SHORT).addCallback(new Snackbar.Callback() {
#Override
public void onDismissed(Snackbar snackbar, int event) {
switch(event) {
case Snackbar.Callback.DISMISS_EVENT_ACTION:
rulesAdapter.notifyDataSetChanged();
break;
case Snackbar.Callback.DISMISS_EVENT_TIMEOUT:
case Snackbar.Callback.DISMISS_EVENT_CONSECUTIVE:
case Snackbar.Callback.DISMISS_EVENT_MANUAL:
Log.d("test", Integer.toString(position));
if(position!=-1 && position<arrayList.size()){
// Define 'where' part of query.
String selection = DatabaseTablesColums.FeedEntry._ID + " LIKE ?";
// Specify arguments in placeholder order.
String[] selectionArgs = {Integer.toString(arrayList.get(position).getId())};
// Issue SQL statement.
arrayList.remove(position);
rulesAdapter.notifyItemRemoved(position);
rulesAdapter.notifyItemRangeChanged(position, rulesAdapter.getItemCount());
db.delete(DatabaseTablesColums.FeedEntry.TABLE_NAME, selection, selectionArgs);
// Remove item from backing list here
}
break;
}
}
#Override
public void onShown(Snackbar snackbar) {
}
}).setAction(getResources().getString(R.string.undo), new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
rusure.show();
}
});
itemTouchHelper.attachToRecyclerView(recyclerView);
Looks like you are removing the item from the backing data store, in your case, it's the arrayList and notifies the adapter only after the SnackBar dismisses.
So if you swipe the next item before the first Snackbar dismisses, RecyclerView adapter has no idea that the first item has been removed from the adapter. So it's giving you a wrong position.
You should remove the item from the backing data store (arrayList) and call notifyItemRemoved() as soon as you swipe the item rather than updating after the Snackbar dismisses.
You can update the database only after the SnackBar dismisses, though.

Android - modify the adapter and items of a recyclerview in on click listener

My situation is this: I set a recyclerView on runtime in a retrofit method async request, called in a loop.
So one by one the items are added to the recyclerview.
#Override
public void onResponse(Call<DirectionResults> call, Response<DirectionResults> response) {
Legs legs = response.body().getRoutes().get(0).getLegses().get(0);
NearbyBusStation current = new NearbyBusStation(currentItem, legs.getDistance().getValue(), legs.getDuration().getText());
int i;
for(i = 0; i < nearbyBusStations.size();i++){
if (current.getDistance() < nearbyBusStations.get(i).getDistance())
break;
}
nearbyBusStations.add(i,current);
mAdapter.notifyItemInserted(nearbyBusStations.size() - 1);
}
In The adapter:
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
// - get element from your dataset at this position
// - replace the contents of the view with that element
final NearbyBusStation currentItem = mDataset.get(position);
holder.name.setText(currentItem.getName().getName());
holder.distance.setText(currentItem.getDistance() +" m");
holder.time.setText(currentItem.getTime() + " a piedi");
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//I suppose that code go here
}
I want that when an item of the recyclerview is clicked, it expand adding new element below or that the recyclerview was cleared and the new element are added, with a button to return to initial recyclerview.
(on the click on a bus stop, it will open the next buses which pass from it)
I've tried to use external libraries to create expandable recyclerview but they work very well with fixed item, in my case items are adding by retrofit at runtime.
If is correct to use the onclick inside onBindViewHolder, how? with another adapter?
I need to insert items one by one because I used this library for animations
compile 'jp.wasabeef:recyclerview-animators:2.2.4'
which work only inserting or deleting elements one by one, but if is it a problem, I can change library for animation without any problem.
Thank you in advance!

Android Adapter "java.lang.IndexOutOfBoundsException: Invalid index 4, size is 4"

I have problem with deleting item's from ArrayList and synchronising Adapter.
I have my RecyclerView adapter with some ArrayList inside it called items. I download some list from the server and dispaly inside it. Whenever I click on some of list items I would like to delete it from server, from local ArrayList and notify the adapter about it. The problem is that when I delete everything from down to up from the list everything is ok, but when f.e. I delete 1st element from the list and then randomly some of the elements it deletes element after the one I clicked. In some cases the app crashes (f.e. I delete 1st element then the last one). The error I get is f.e.:
java.lang.IndexOutOfBoundsException: Invalid index 4, size is 4
Look like it's something with list size but i don't know what is wrong?
Here is the function where I got position from (setPopUpListener(popupMenu, position)):
// Binding New View
#Override
public void onBindViewHolder(ViewHolder holder, final int position) {
RecipeItem item = items.get(position);
// Binding Recipe Image
Picasso.with(context).load(item.getImgThumbnailLink()).into(holder.recipeItemImage);
// Binding Recipe Title
holder.recipeItemTitle.setText(item.getTitle());
// Binding Recipe Subtitle
String subtitle = "Kuchnia " + item.getKitchenType() + ", " + item.getMealType();
holder.recipeItemSubtitle.setText(subtitle);
// Binding Recipe Likes Count
holder.recipeItemLikesCount.setText(Integer.toString(item.getLikeCount()));
// Binding Recipe Add Date
holder.recipeItemAddDate.setText(item.getAddDate());
// Binding Recipe Options Icon
holder.recipeItemOptionsIcon.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
PopupMenu popupMenu = new PopupMenu(context, v);
setPopUpListener(popupMenu, position); // Setting Popup Listener
inflatePopupMenu(popupMenu); // Inflating Correct Menu
popupMenu.show();
}
});
// Item Click Listener
holder.setClickListener(new RecipeItemClickListener() {
#Override
public void onClick(View view, int position) {
// taking to recipe activity
}
});
}
Here is setPopUpListener() - just look at removeFromFavourites(position):
// Setting Popup Listener
private void setPopUpListener(PopupMenu popupMenu, final int position) {
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (popupType) {
// Add To Favourites Menu
case 0: {
switch (item.getItemId()) {
case R.id.item_add: {
addToFavourites(position);
return true;
}
}
}
// Remove From Favourites Menu
case 1: {
switch (item.getItemId()) {
case R.id.item_remove: {
removeFromFavourites(position);
return true;
}
}
}
}
return false;
}
});
}
Here is where the error appears (removeFromFavourites(position)):
// Removing User's Favourite
private void removeFromFavourites(int position) {
// Checking Connection Status
if (!FormValidation.isOnline(context)) {
showSnackbarInfo(context.getString(R.string.err_msg_connection_problem),
R.color.snackbar_error_msg);
} else {
SQLiteHandler db = new SQLiteHandler(context);
// Getting User Unique ID
String userUniqueId = db.getUserUniqueId();
db.close();
RecipeItem listItem = items.get(position);
// Getting Recipe Unique ID
String recipeUniqueId = listItem.getUniqueId();
// Removing From User's Favourites
removeFromUserFavouritesOnServer(recipeUniqueId, userUniqueId);
// Removing Item From Local Array List
items.remove(position);
// Notifying Adapter That Item Has Been Removed
notifyItemRemoved(position);
}
}
SOLUTION - HOPE THIS WILL HELP SOMEBODY
I have found the soution for this. If somebody will ever try to dynamicly remove elements from his array list and notifyItemRemoved(position) do not send clicked position as a parameter inside onBindViewHolder(ViewHolder holder, int position). You will meet with exactly the same situation as I did.
If you have 4 displayed elements in a list f.e. [0, 1, 2, 3] and try to remove from the end of the list everything will be fine cause clicked positions will match exactly the same positions in ArrayList. For example if you click 4th element:
position = 3 - position you will get when clicked on list element; myArray.remove(position) - will remove element with index = 3 and notifyItemRemoved(position) - will animate the list and remove deleted element from the displayed list. You are going to have following list: [0, 1, 2]. This is fine.
Situation changes when you want to delete random element. Let's say I want to delete 3rd displayed list element. I click on it to delete so i get:
position = 2 -> myArray.remove(position) -> notifyItemRemoved(position)
In this case the ArrayList I am going to get will be like this: [0, 1, 3]. In I now click on the last dispalyed element and would like to delete it that's what I will get:
position = 3->myArray.remove(position) -> notifyItemRemoved(position)
But what happens? App suddenly crashes with exception: java.lang.IndexOutOfBoundsException: Invalid index 3, size is 3. It means that we are trying to get element at the position that does not exist. But why? I got my clicked position from element... This is what happened:
At the beggining we had:
ARRAY LIST INDEXES -> [0, 1, 2, 3]
POSITIONS FROM CLICK -> [0, 1, 2, 3]
After Deleting 3rd element:
ARRAY LIST INDEXES -> [0, 1, 2]
POSITIONS FROM CLICK -> [0, 1, 3]
Now when I try to delete element at position = 3 we can't do that. We do not have that position. The max position we can get is 2. Thats why we get the exception. How to manage that problem?
In onBindViewHolder(ViewHolder holder, int position) we used position in
removeFromFavourites(position). But we also have our returned holder. If we use method called: getAdapterPosition() from class RecyclerView.ViewHolder we are at home.
getAdapterPosition
From developer site: http://developer.android.com/reference/android/support/v7/widget/RecyclerView.ViewHolder.html#getAdapterPosition()
This will always return index identical to that in ArrayList. So summaring all we had to do was changing position parameter with holder.getAdapterPosition():
// Binding New View
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
RecipeItem item = items.get(position);
// Binding Recipe Image
Picasso.with(context).load(item.getImgThumbnailLink()).into(holder.recipeItemImage);
// Binding Recipe Title
holder.recipeItemTitle.setText(item.getTitle());
// Binding Recipe Subtitle
String subtitle = "Kuchnia " + item.getKitchenType() + ", " + item.getMealType();
holder.recipeItemSubtitle.setText(subtitle);
// Binding Recipe Likes Count
holder.recipeItemLikesCount.setText(Integer.toString(item.getLikeCount()));
// Binding Recipe Add Date
holder.recipeItemAddDate.setText(item.getAddDate());
// Binding Recipe Options Icon
holder.recipeItemOptionsIcon.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
PopupMenu popupMenu = new PopupMenu(context, v);
setPopUpListener(popupMenu, holder.getAdapterPosition()); // Setting Popup Listener
inflatePopupMenu(popupMenu); // Inflating Correct Menu
popupMenu.show();
}
});
// Item Click Listener
holder.setClickListener(new RecipeItemClickListener() {
#Override
public void onClick(View view, int position) {
// taking to recipe activity
}
});
}
Use
notifyItemRangeChanged(position, getItemCount());
after
notifyItemRemoved(position);
You don't need to use index, just use position.
You are changing internal data structure which is not a good way
Suggestions:
In case of add/remove operation, do to the main array (e.g. ArrayList) which is passed to the adapter
Never initialize the main array (which is passed to the adapter) to null, it will lost the adapter reference
After adding/removing just call adapter.notifyDataSetChanged(); for adapter
just Use
product_list.remove(product_list.get(position));
notifyItemRemoved(position);
notifyItemRangeChanged(position, itemCount);

StickyGridHeaders custom adapter

I'm trying to implement my own StickyGridHeadersBaseAdapter, my current source code here - http://paste.org.ru/?11jrjh, and I use it like
ModeAdapter adapter = new ModeAdapter(this);
modeGridView.setAdapter(adapter);
Problems which I have is that
1) I have no idea how to call notifyDataSetChanged() for this adapter, so I can't change items
2) And implementation of AdapterView.OnItemClickListener (http://paste.org.ru/?mvgt7b) works strange
Mode mode = (Mode) adapter.getItem(position);
returns null for items with 1st and 2nd positions, item on 3rd position is actual 1st item in adapter.
Where is my fault here?
One more question is why I can't cast adapterView.getAdapter() in my OnItemClickListener to my ModeAdapter class. What if I want to call notifyDataSetChanged() here?
I didn't find any examples for custom implementation of StickyGridHeadersBaseAdapter here.
Thanks in advance.
I had the same issue as you 2):
after the first header i got the item of the previous row, after the second header got the item of two rows up, etc...
The reason is the following:
StickyHeadersGridView:
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
mOnItemClickListener.onItemClick(parent, view, mAdapter.translatePosition(position).mPosition, id);
}
The position is corrected. so in your onItemClick you get the corrected value of position.
If than you request the item with: Mode mode = (Mode) adapter.getItem(position);
you get StickyGridHeadersBaseAdapterWrapper.getItem(int pos)
#Override
public Object getItem(int position) throws ArrayIndexOutOfBoundsException {
Position adapterPosition = translatePosition(position);
if (adapterPosition.mPosition == POSITION_FILLER || adapterPosition.mPosition == POSITION_HEADER) {
// Fake entry in view.
return null;
}
return mDelegate.getItem(adapterPosition.mPosition);
}
In StickyGridHeadersBaseAdapterWrapper.getItem() position gets corrected for the second time which causes the wrong item to be returned...
I added a work-around:
In StickyHeadersBaseAdapterWrapper I added:
public Object getItemAtDelegatePosition(int pos) {
return mDelegate.getItem(pos);
}
And I use this in onItemClick:
Mode item = (Mode) ((StickyGridHeadersBaseAdapterWrapper)parent.getAdapter()).getItemAtDelegatePosition(position);
An easier way to get an item would be:
StickyGridHeadersBaseAdapterWrapper wrapper = (StickyGridHeadersBaseAdapterWrapper) parent.getAdapter();
Mode item = (Mode ) wrapper.getWrappedAdapter().getItem(position);

Categories

Resources