配器的onBindViewHolder中更新recyclerVi

配器的onBindViewHolder中更新recyclerVi

本文介绍了如何在其适配器的onBindViewHolder中更新recyclerView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在recyclerView中列出了卡片列表.持卡一段时间后,其在数据库中的对应条目将被删除,因此之后必须将其删除.可以通过调用设置新的更新的适配器来完成.但是我无法在onBindViewHolder中使用setAdapter函数,因为我不知道如何获得相同的recyclerView.请帮忙.

I've made a list of cards inside recyclerView. When card is held for sometime, its corresponding entry in database is deleted so after that, the card must be removed. It can be done by calling setting new updated adapter. But I'm unable to use setAdapter function inside onBindViewHolder because I don't know how to get same recyclerView. Please help.

代码:

public class RecyclerViewAdapterMain extends RecyclerView.Adapter<RecyclerViewAdapterMain.ViewHolder> {

String[] strings;
DBManager dbManager;

public RecyclerViewAdapterMain(String[] arrayList) {
    this.strings = arrayList;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_list,parent,false);
    ViewHolder viewHolder = new ViewHolder(view);
    return viewHolder;
}

@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
    holder.textView.setText(strings[position]);
    holder.cardView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(holder.itemView.getContext(),SecondActivity.class);
            Bundle bundle = new Bundle();
            bundle.putString("list",strings[position]);
            intent.putExtras(bundle);
            holder.itemView.getContext().startActivity(intent);
            //overridePendingTransition(R.anim.abc_fade_in,R.anim.abc_fade_out);
        }
    });
    holder.cardView.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(final View view) {
            DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    switch (which){
                        case DialogInterface.BUTTON_POSITIVE:
                            dbManager = new DBManager(holder.itemView.getContext(),null,null,1);
                            dbManager.deleteList(strings[position]);
                            String[] strings = dbManager.showLists();
                            RecyclerViewAdapterMain recyclerViewAdapter = new RecyclerViewAdapterMain(strings);
                            //I WANT TO SET UPDATED ADAPTER HERE
                            break;

                        case DialogInterface.BUTTON_NEGATIVE:
                            //No button clicked
                            break;
                    }
                }
            };
            AlertDialog.Builder builder = new AlertDialog.Builder(holder.itemView.getContext());
            builder.setMessage("Are you sure that you want to delete \""+strings[position]+"\"?").setPositiveButton("Yes", dialogClickListener)
                    .setNegativeButton("No", dialogClickListener).show();
            return true;
        }
    });


}

@Override
public int getItemCount() {
    return strings.length;
}

public class ViewHolder extends RecyclerView.ViewHolder{
    CardView cardView;
    TextView textView;
    public ViewHolder(View itemView) {
        super(itemView);
        textView = (TextView) itemView.findViewById(R.id.textView);
        cardView = (CardView) itemView.findViewById(R.id.cardView);
    }
}

}

推荐答案

您需要更新现有变量

String[] strings;

然后告诉RecyclerView重绘项目.

And then tell the RecyclerView to redraw the items.

notifyItemRemoved(position);

notifyDataSetChanged();

然后您得到

case DialogInterface.BUTTON_POSITIVE:
                        dbManager = new DBManager(holder.itemView.getContext(),null,null,1);
                        dbManager.deleteList(strings[position]);
                        strings = dbManager.showLists();
                        notifyItemRemoved(position);
                        break;

这篇关于如何在其适配器的onBindViewHolder中更新recyclerView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 12:08