Reputation: 21
When I delete an item from my recyvlerview reflects it correctly on my view, but I need to reflect it also in my textviews related to my recyvlerview.
Is there any way to reload the fragment by deleting an item from the recyvlerview from my adapter?
holder.layout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Remove");
builder.setTitle("Do you want to remove");
builder.setNeutralButton("Remove", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int witch) {
DatabaseHelper dbHelper = new DatabaseHelper(context);
dbHelper.deleteItem(cot.getId(), context);
list.remove(position);
recyclerView.removeViewAt(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position,list.size());
notifyDataSetChanged();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int witch ) {
dialog.dismiss();
}
});
builder.create().show();
}
});
AN UPDATE:
Maybe I didnt explain myself very well and that's why answers arent working. When I delete from my recyclerview items refresh correctly, i call my item from my adapter and put it in textviews. I use data from those texviews to do sums, subtractions, etc and put it the result in another textviews but those ones not change if i delete an item cause they are not part from my recyclerview. But if I open my fragment again I can see a different result in my textviews, so I think I really need to refresh the whole fragment whenever I delete an item of my recylcerview
Upvotes: 1
Views: 1909
Reputation: 1048
I am not sure about this but I want to tackle this issue. Put the following code in the remove button: -
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.detach(this).attach(this).commit();
Therefore, whenever an item is deleted, this code will be triggered to refresh your fragment.
Upvotes: 0
Reputation: 129
Yes there are many ways to deal with this... You can create this by, First,
If you are creating your fragment by XML, So you just need to get your fragment first by like
FragmentClassName object=getSupportFragmentManager().findFragmentById(R.id.id_frag);
If you are adding fragment by viewgroup programmatically
FragmentClassName object=getSupportFragmentManager().beginTransaction().add().commit();
Use this object to call any method from Fragment Class to update view or data
Like here,
holder.layout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Remove");
builder.setTitle("Do you want to remove");
builder.setNeutralButton("Remove", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int witch) {
DatabaseHelper dbHelper = new DatabaseHelper(context);
dbHelper.deleteItem(cot.getId(), context);
list.remove(position);
recyclerView.removeViewAt(position);
notifyItemRemoved(position);
object.methodCall();
// Don't do that only one call is enough to delete item from view
// notifyItemRangeChanged(position,list.size());
//notifyDataSetChanged();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int witch ) {
dialog.dismiss();
}
});
builder.create().show();
}
});
Others are some tricky methods which called Calling Mechanisms using Interface,
if you are aware of them then you can do the trick by making static method call to bind an interface, and use his object for calling.
Let me update my answer for more help by Calling Interface Mechanism
//write this on your activity or fragment to get delete notification
// Can be used to get reply on Any Class Either Activity or Fragment, no matter you recycler or adapter existence class.
Adapter.bindListener(new Adapter.NotifyMe() {
@Override
public void onItemDelete() {
// When ever your delete code run your this block run then you can
//Update your code from here if you want to update things of fragment class
}
});
Your Adapter Class
Class Adapter extend RecyclerView.Adapter<Adapter.ViewHolder> {
private static NotifyMe notifyMe;
public static void bindListener (NotifyMe notifyMeListener){
notifyMe = notifyMeListener;
}
@Override
public void onBindViewHolder (@NonNull ViewHolder viewHolder,int position){
holder.layout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
builder.setNeutralButton("Remove", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int witch) {
//....
notifyItemRemoved(position);
// object.methodCall();
//Call this here this will take you to implementation block.
if (notifyMe != null)
notifyMe.onItemDelete();
// Don't do that only one call is enough to delete item from view
// notifyItemRangeChanged(position,list.size());
//notifyDataSetChanged();
}
});
}
});
}
public interface NotifyMe {
void onItemDelete();
}
}
Or for more there is another implementation just for your learning
//Your Adapter Object Declaration code
Adapter adapter = new Adapter(new Adapter.NotifyMe() {
@Override
public void onItemDelete() {
// When ever your delete code run your this block run then you can
//Update your code from here if you want to update things of fragment class
}
});
Adapter Class Code :
Class Adapter extend RecyclerView.Adapter<Adapter.ViewHolder> {
private NotifyMe notifyMe;
public Adapter(NotifyMe notifyMe) {
this.notifyMe=notifyMe;
}
@Override
public void onBindViewHolder (@NonNull ViewHolder viewHolder,int position){
holder.layout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
builder.setNeutralButton("Remove", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int witch) {
//....
notifyItemRemoved(position);
// object.methodCall();
//Call this here this will take you to implementation block.
notifyMe.onItemDelete();
// Don't do that only one call is enough to delete item from view
// notifyItemRangeChanged(position,list.size());
//notifyDataSetChanged();
}
});
}
});
}
public interface NotifyMe {
void onItemDelete();
}
}
Upvotes: 1