如何应用适配器的notifyDataSetChanges()方法?

时间:2018-08-03 18:43:06

标签: java android android-recyclerview

我正在使用带有自定义适配器的recyclerview扩展了RecyclerView.Adapter类

我在适配器类的onBindViewHolder()方法中编写了一个弹出窗口 弹出窗口包含两个项目,编辑和删除都工作正常,但是问题是当我从recyclerview删除一个项目时,我必须关闭该应用程序并再次运行以查看它是否已被删除。 我想查看更改而不退出应用程序 我试图在适配器内使用notifyDataSetChanged(),但是它不起作用 我该如何解决这个问题?

这是我在OnBindViewHolder中的代码

holder.dotsOptions.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        PopupMenu popup = new PopupMenu(ctx, holder.dotsOptions);
        //Inflating the Popup using xml file
        popup.getMenuInflater().inflate(R.menu.dots_menu, popup.getMenu());
        popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {

                if(item.getItemId()==R.id.edit){

                }
                else if(item.getItemId()==R.id.delete){
                    AlertDialog.Builder builder=new AlertDialog.Builder(ctx);
                    builder.setTitle("Are you sure to delete:");
                    builder.setMessage(holder.contactDisplayName.getText().toString());
                    builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {


                            Uri contactUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(contactInfo[1]));
                            Cursor cur = ctx.getContentResolver().query(contactUri, null, null, null, null);
                            try {
                                if (cur.moveToFirst()) {
                                    do {
                                        if (cur.getString(cur.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME)).equalsIgnoreCase(contactInfo[0])) {
                                            String lookupKey = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
                                            Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_LOOKUP_URI, lookupKey);
                                            ctx.getContentResolver().delete(uri, null, null);

                                        }

                                    } while (cur.moveToNext());
                                    notifyDataSetChanged();

                                }

                            } catch (Exception e) {
                                System.out.println(e.getStackTrace());
                            } finally {
                                cur.close();


                            }


                        }
                    });
                    builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                        }
                    });
                    AlertDialog alert=builder.create();
                    alert.show();
                }

                return false;
            }
        });
        popup.show();

    }
});

1 个答案:

答案 0 :(得分:1)

notifyDataSetChanged()之前致电yourList.remove(item)。就是全部!