简体   繁体   English

如何使用适配器中的对话框在更新后刷新 RecyclerView

[英]How to refresh RecyclerView after update using Dialog from Adapter

I have apps where the requirement is update & delete the item of RecyclerView using dialog.我的应用程序要求使用对话框更新和删除 RecyclerView 的项目。 The dialog will open after click the popup menu单击弹出菜单后将打开对话框

在此处输入图像描述

I create the dialog function on Adapter class in onBindViewHolder .我在 onBindViewHolder 的 Adapter class 上创建对话框onBindViewHolder The function successfully update and delete the data on server. function 成功更新和删除服务器上的数据。 How do I refresh the RecyclerView after it?之后如何刷新 RecyclerView?

Adapter.java

holder.cBtnMore.setOnClickListener(v -> {
            PopupMenu popupMenu = new PopupMenu(v.getContext(), holder.cBtnMore);
            popupMenu.getMenuInflater().inflate(R.menu.more_menu, popupMenu.getMenu());

            popupMenu.setOnMenuItemClickListener(menuItem -> {
                if (menuItem.getItemId() == R.id.menu_update) {
                    final Dialog dialog = new Dialog(v.getContext());
                    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                    dialog.setContentView(R.layout.dialog_update);
                    dialog.getWindow().setLayout(Cons.widthScreen, Cons.heightScreen);

                    // Declaration & Set Text
                 ...

                    btnUpdate.setOnClickListener(unused -> {
                        updateBarang(
                                v.getContext(),
                                id,
                                etNama.getText().toString(),
                                etAlamat.getText().toString(),
                                etNoPenjual.getText().toString(),
                                etKodeBarang.getText().toString(),
                                etJumlahPenjualan.getText().toString(),
                                etHargaSatuan.getText().toString(),
                                etDiskon.getText().toString(),
                                etTotalHarga.getText().toString()
                        );
                    });

                    btnCancel.setOnClickListener(unused -> {
                        dialog.dismiss();
                    });

                    dialog.show();
                } else if (menuItem.getItemId() == R.id.menu_delete) {
                    Toast.makeText(v.getContext(), "Delete", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(v.getContext(), "Error", Toast.LENGTH_SHORT).show();
                }

                return true;
            });

            popupMenu.show();
        });

MainActivity.java MainActivity.java

public class MainActivity extends AppCompatActivity {

    FloatingActionButton fabAdd;
    RecyclerView rvBarang;
    ApiInterface apiInterface;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        apiInterface = ApiConnection.Connection().create(ApiInterface.class);

        ...

        rvBarang.setLayoutManager(new LinearLayoutManager(this));
    }

    @Override
    protected void onResume() {
        Call<List<Barang>> tampilBarang = apiInterface.listBarang();
        tampilBarang.enqueue(new Callback<List<Barang>>() {
            @Override
            public void onResponse(Call<List<Barang>> call, Response<List<Barang>> response) {
                ArrayList<Barang> barangArrayList = (ArrayList<Barang>) response.body();
                BarangAdapter barangAdapter = new BarangAdapter(barangArrayList);
                rvBarang.setAdapter(barangAdapter);
            }

            @Override
            public void onFailure(Call<List<Barang>> call, Throwable t) {
                // TODO
            }
        });
        super.onResume();
    }
}

If you want to update your recyclerview then you have to update your data source of adapter means list data which you are using in adapter to show and after that you need to notify adapter to refresh如果你想更新你的 recyclerview 那么你必须更新你的适配器的数据源意味着你在适配器中使用的列表数据来显示然后你需要通知适配器刷新

To update recyclerview after delete, add below method in your adapter class要在删除后更新 recyclerview,请在您的适配器中添加以下方法 class

public void deleteItem(int position){
   sourceList.removeAt(position); // updating source
   notifyItemRemoved(position); // notify adapter to refresh 
}

To refresh recyclerview after update, you must have position along with updated object so add below method also in adapter class要在更新后刷新 recyclerview,您必须具有 position 以及更新后的 object,因此也在适配器 class 中添加以下方法

public void updateData(int position, Barang updatedObject){
  sourceList.set(position,updatedObject); // updating source
  notifyDataSetChanged(); // notify adapter to refresh
}

after adding above methods, you can just call it to refresh添加以上方法后,调用刷新即可

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM