问题
I have a delete button in my adapter call and when i press the delete button i want to
1. remove the value from list and,
2. Update the size of the list in the main activity
and . Have tried using Interface where i referred here but it's not working and i am so confused.
So can any one provide me an best way
My Adapter Code:
@Override
public void onBindViewHolder(final AddLineItem_Adapter.ViewHolder holder, final int position) {
final AddLineItem_ListView addLineItem_listView = addLineItem_listViews.get(position);
holder.tv_OrderID.setText(addLineItem_listView.getItemID());
holder.tv_ProductName.setText(addLineItem_listView.getProductName());
holder.tv_Quantity.setText(addLineItem_listView.getQuantity());
holder.tv_UnitPrice.setText(addLineItem_listView.getUnitPrice());
holder.tv_TotalAmount.setText(addLineItem_listView.getTotalAmount());
holder.btn_Delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
addLineItem_listViews.remove(position);
notifyDataSetChanged();
}
});
}
I want to update the addLineItem_listView
size on the main activity in TextView.
回答1:
- Create the interface..
- Pass it to the adapter..
- Call interface function after deleting (providing the size of list)
Create interface
public interface ShowDeleted {
void showDeleted(int size);
}
Initialize the interface anonymously in the activity like this
ShowDeleted showDeleted = new ShowDeleted() {
@Override
public void showDeleted(int size) {
// show the changed list size or update UI
}
};
Pass the interface to the recyclerAdapter
YourAdapter youradpter = new YourAdpater(context,list,showDeleted );
Initialize the constructor of recyclerAdapter like this:-
public YourAdapter(Context context, YourList yourList, ShowDeleted
showDeleted)
{
}
holder.onClick do this
holder.btn_Delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
addLineItem_listViews.remove(position);
notifyDataSetChanged();
this.showDeleted.showDeleted(addLineItem_listViews.size());
}
});
回答2:
try to use view.setTag(position)
@Override
public void onBindViewHolder(final AddLineItem_Adapter.ViewHolder holder, final int position) {
final AddLineItem_ListView addLineItem_listView = addLineItem_listViews.get(position);
holder.tv_OrderID.setText(addLineItem_listView.getItemID());
holder.tv_ProductName.setText(addLineItem_listView.getProductName());
holder.tv_Quantity.setText(addLineItem_listView.getQuantity());
holder.tv_UnitPrice.setText(addLineItem_listView.getUnitPrice());
holder.tv_TotalAmount.setText(addLineItem_listView.getTotalAmount());
holder.btn_Delete.setTag(position)
holder.btn_Delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
addLineItem_listViews.remove((Integer)v.getTag());
notifyDataSetChanged();
}
});
}
Good Luck!
回答3:
use this function
public void removeAt(int position) {
list.remove(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position, list.size());
}
来源:https://stackoverflow.com/questions/48904077/how-to-get-the-list-size-from-adapter-and-update-activity-ui-from-recycler-ada