How to get the List size() from adapter and update Activity UI from recycler Adapter itself?

吃可爱长大的小学妹 提交于 2019-12-02 13:23:30
  • 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());
    }
});
Venkat Rao

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!

use this function

 public void removeAt(int position) {
    list.remove(position);
    notifyItemRemoved(position);
    notifyItemRangeChanged(position, list.size());
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!