In my app I have a bottom navigation including home and favorites. in the recyclerview when I click on like button it'll be added to favourites. now when clicked again, I want to remove it from favorite at the moment.
In my code the item is deleted but it won't be removed until I go to home and return.
Note that I can't use arraylist.remove(position) because if so, even in home fragment the item is removed and I don't want that.
code in adapter:
itemHolder.like.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int rowId = databaseHelper.GetId((String) itemHolder.headline.getText().toString());
if (databaseHelper.IsLiked(rowId)){
databaseHelper.UpdateLiked(rowId, false);
notifyItemRemoved(position);
notifyItemChanged(position);
}
else{
databaseHelper.UpdateLiked(rowId, true);
notifyDataSetChanged();
notifyItemChanged(position);
}
}
yourarraylist.remove(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position, yourarraylist.size());
Related
I have a RecyclerView and adapter. Now in that adapter, I'm inflating one row. In that row, there are one delete button and one progressbar. So what I'm doing is when user clicks on delete button, I make invisible that delete button, and make visible small progress bar in place of delete button from Adapter class. And also I'm sending position via listener to that attached activity, from that I'm calling AsyncTask.
Now the problem is:
When I got to know via AsyncTask that item is deleted, I again want to make visible delete button and to make invisible progressbar. But this time - from Activity (not from adapter), because I want to do something in activity when I get to know that item is deleted. So I can't implement AsyncTask in adapter.
code:
Adapter
delete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (listener != null) {
delete.setVisibility(View.GONE);
progressBar.setVisibility(View.VISIBLE);
listener.onClicked(getAdapterPosition(), eventList.get(getAdapterPosition()).getEventId());
}
}
});
Activity (in activity I want to visible/invisible adapter row button and p.bar:
#Override
public void onDeleteDataReceived(Boolean status, int position) {
stopShimmerLayout();
if (status) {
try {
eventsList.remove(position);
mAdapter.notifyItemRemoved(position);
showToast(context, "Deleted", Toast.LENGTH_SHORT);
} catch (Exception e) {
e.printStackTrace();
}
} else {
showToast(context, "Failed", Toast.LENGTH_SHORT);
}
}
See the video for better understanding: https://drive.google.com/open?id=13ZAtnyfGbi2X4JjUTmJsIDy-gt5y51Gr
To fix your problem you can take the below approach.
1) Inside your Eventpojo/model class, declare a boolean isSelectedwhich will be initially false. Now whenever user clicks the row, do `
delete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
eventList.get(position).isSelected=true;
delete.setVisibility(View.GONE);
progressBar.setVisibility(View.VISIBLE);
}
So by doing this, we are keeping in memory which object is selected and which is not, but once you recycle your views bindViewHolder will be invoked aagain and UI elements setter will be called again so put a check inside onBindViewHolder()
if(eventList.get(position).isSelected){
//show progress bar
}else{
// show delete icon
}
To remove the item, just do the following changes in your adapter-
public void removeItem(int position){
eventList.remove(position)
notifyItemRemoved(position)
}
I followed this answer:
https://stackoverflow.com/a/30046476/8793443
and got it to work: only one item is selected at a time (which is what I want).
However, when the same item is clicked for a second time, it remains selected. How can I deselect it so that it goes back to it's original color background?
Any help is appreciated.
Thanks!
The OnClick event must be this instead:
#Override
public void onClick(View view) {
if (selectedPos == getAdapterPosition()) {
selectedPos = RecyclerView.NO_POSITION;
notifyDataSetChanged();
return;
}
selectedPos = getAdapterPosition();
notifyDataSetChanged();
}
It works like a charm! Thanks to Quick learner's answer for inspiration.
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();
}
}
}
});
i am using android studio in listview i have added adapter. when user click delete button from listview item then i want to refresh activity.
adapter delete button definition
deletebtn =convertView.findViewById(R.id.delete_btn);
deletebtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
databaseAccess.deleteFavorite(favoriteModel.getWid());
}
});
i have tried this code but not working
deletebtn =convertView.findViewById(R.id.delete_btn);
deletebtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
databaseAccess.deleteFavorite(favoriteModel.getWid());
Intent intent1=new Intent(context,FavoriteNameslist.class);
context.startActivity(intent1);
((Activity)context).finish();
}
});
i am new to Android - please help
i want when user click on delete button then refresh activity
There is no need to call your Activity to referesh listview you can use
notifyDataSetChanged()
remove the item and set notifyDataSetChanged()
yourList.remove([INDEX]);
arrayAdapter.notifyDataSetChanged();
If you are removing a single item from the view, you need to call
yourAdapter.notifyItemChanged(position) //Just pass the position to this. This also has a default animation. Try this in your adapter class instead of refreshing the layout.
Try below lines of code on Deleting Row to Update adapter
arrayList.remove(position);
mAdapter.notifyItemRemoved(position);
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
}