Recyclerview data appear again after deleting - android

I have a recyclerview. In its item layout , I have a plus button . If user click on that button it will become minus and new layout will added below it . If user click on minus it will remove from recyclerview as well as from the dataset .
Problem is :
When user click minus it removes properly from both recyclerview and dataset but on adding new layout it appears with old data again . I make sure that it deletes from dataset but still appears again.
My Adapter Code:
#Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
holder.img_delete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (if it is plus) {
holder.img_delete.setImageResource(set minus drawable);
MyModel model = new MyModel();
model.setPrice("");
data.add(model);
notifyItemInserted(position+1); //+1 Because first time im adding one data manually in onCreate to set adapter
} else {
data.remove(position);
notifyItemRemoved(position);
}
}
});

Related

how to hide an item in list by clicking another item of the same list in recycler view?

I have a list of elements in recycler view, my need is to hide some elements by clicking a specific element of the same list. For instance, my list contains 10 elements and I want to hide all the elements from position 6 by clicking the element in the 5th position. how can I do that?
You have to remove that element from dataset and call notifyOnDataSetChanged Method, otherwise you can create a model class with boolean/hide show flag and then on item click listener you can set flag accordingly to remove/hide element.
Recyclerview react to notifyDatasetChanged method to redraw each visible row.
try to change behind model of recycler view then notifyDatasetChanged
bindViewHolder(VH holder, int position){
view.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
// Do you business to change data model in other position which
//identified whether the view must be visible or not
notifyDataSetChanged();
}
});
}
Just try this...
public void onBindViewHolder(final ViewHolder viewHolder,
final int position) {
viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// set your logic like this..
if(position==5){
if(list.size()>6){
list.remove(6); // here list will be your data list.
notifyDataSetChanged();
}
}
}
});

RecyclerView changes multiple rows although only one row was targeted

I got a RecyclerView and want to change the appearance of any clicked row. For that I have a callbackFunction in my Activity which I pass to the Adapter, which then is called inside the Adapter, as soon as I click on any row in the RecyclerView.
The clicked row is then changed, but it happens, that not only the clicked rows are changed but also other rows, that weren't clicked and were never clicked before. I checked the ArrayList that contains the data, but everything is fine there. Only the clicked elements contain the trigger to change the appearance of the row.
What is causing the other rows to change, although they have not been clicked?
Interface inside activity for callback
public interface onHeaderClickListener{
void onHeaderClicked(int index);
}
Inside RecyclerView Adapter
#Override
public void onBindViewHolder(#NonNull RecyclerView.ViewHolder holder, final int position) {
if (holder instanceof ViewHolderHeader){
((ViewHolderHeader)holder).dateHeaderTextView.setText( Integer.toString(((objClass_offerDateHeader) arrayList.get(position)).getDate()));
if(((objClass_offerDateHeader) arrayList.get(position)).isSelected()){
((ViewHolderHeader)holder).dateHeaderTextView.setBackgroundColor(Color.parseColor("#b642f4"));
}
((ViewHolderHeader)holder).dateHeaderTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onHeaderClickListener.onHeaderClicked(position);
}
});
}
}
Adapter initialisation inside activity
customAdapterRecyclerViewAddOffersTo = new customAdapterRecyclerViewAddOffers(offerArrayList,"dragTo", new onHeaderClickListener() {
#Override
public void onHeaderClicked(int index) {
if (offerArrayList.get(index) instanceof objClass_offerDateHeader){
if(((objClass_offerDateHeader) offerArrayList.get(index)).isSelected()){
((objClass_offerDateHeader) offerArrayList.get(index)).setSelected(false);
}
else {
((objClass_offerDateHeader) offerArrayList.get(index)).setSelected(true);
}
customAdapterRecyclerViewAddOffersTo.notifyDataSetChanged();
}
}
});
In your onBindViewHolder method you have to set the background of the unselected cell, keep in mind the the cells are reused and you only set the background of selected cells so when it is reused the background is not returned to the normal color
So in code you will have to add an else condition
if(((objClass_offerDateHeader) arrayList.get(position)).isSelected()){
((ViewHolderHeader)holder).dateHeaderTextView.setBackgroundColor(Color.parseColor("#b642f4"));
} else {
((ViewHolderHeader)holder).dateHeaderTextView.setBackgroundColor(Color.parseColor("#FFFFFF")); // I assume you need it to be white you can change it to any other color
}
You need to add an else condition here:
if(((objClass_offerDateHeader) arrayList.get(position)).isSelected()){
((ViewHolderHeader)holder).dateHeaderTextView.setBackgroundColor(Color.parseColor("#b642f4"));
}
Viewholders get recycled, so you cannot be sure of the current state when onBindViewHolder is called.

Remove item from RecyclerView but View below not adjust

I have a delete button in each item of RecyclerView. It works fine but the view below RecyclerView doesn't follow up.
I try to follow Android RecyclerView addition & removal of items but it still not works
Here my code
public ViewHolder(View itemView) {
super(itemView);
btnDelete = (ImageButton)itemView.findViewById(R.id.detail_delete);
btnDelete.setOnClickListener(this);
}
#Override
public void onClick(View v) {
removeAt(getAdapterPosition());
}
private void removeAt(int position) {
scheduleList.remove(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position, scheduleList.size());
}
I think the problem lies here...
notifyItemRangeChanged(position, scheduleList.size());
you're technically lying to the RecyclerView and the adapter by saying you've removed all items located after the specified position...that's what the last parameter is for...number of items to remove which is incorrect because you are passing in the size of the whole list. This what the last parameter stands for...from the javadocs...
itemCount - Number of items removed from the dataset
Basically, you will need to remove that line in the removeAt method so that you only notify the observers once...
private void removeAt(int position) {
scheduleList.remove(position);
notifyItemRemoved(position);
}
if it still doesn't work, then you might be having yet another problem somewhere else
Add the function notifyDataSetChanged(). I was facing the same problem
#Override
public void onClick(View v) {
removeAt(getAdapterPosition());
adapter.notifyDataSetChanged(); //adapter is your card adapter
}

Recyclerview not updating rows correctly when removing data from arraylist?

my RecyclerView contains Text and Delete Button when delete is clicked specific row will be deleted from ArrayList
pending.remove(p);// pending is arraylist
after this updating RecyclerView by calling this function which is inside Adapter
swap(pending);
public void swap(ArrayList<DownloadingActivity.scheduleListType> newList) {
if (pendingList != null) {
pendingList.clear();
pendingList.addAll(newList);
} else {
pendingList = newList;
}
notifyDataSetChanged();
}
but the problem is that correct item deleted successfully from ArrayList pending but on RecyclerView wrong update is displayed.
if i delete second item from array list pending but in RecyclerView updated
deleted item is last
when activity is reloaded then values in RecyclerView are showing correctly
searched lot of about it but did not found any way to solve
Instaead of using swap() to delete the item from your array list and adding them back into diffrent array list, I will suggest that you use notifyItemRemoved in your adapter. So, your adapter will look like below.
RecyclerViewAdapter.java :
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
DownloadingActivity.scheduleListType> p = pendingList.get(position);
//....
//....
holder.deleteBnt.setOnClickListner(new View.OnClickListener() {
#Override
public void onClick(View view) {
pendingList.remove(p);
notifyItemRemoved(position)
}
})
}

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!

Categories

Resources