从列表视图中删除行后刷新片段

时间:2020-09-20 03:54:50

标签: android listview android-fragments android-listview

我设法创建了一个自定义列表视图,并向每行添加了一个删除按钮。删除按钮运行良好,当我单击该按钮时,该行将从数据库中删除。问题是,当我删除一行时,它会保留在列表中,直到我通过移动到另一个片段来刷新该片段并返回然后再从列表中消失为止。我想要的是,当我删除一行时,它立即从列表视图中消失。我尝试了一些解决方案,以在删除完成后刷新整个片段或列表视图,但是它们不起作用!

这是我为适配器类中的删除按钮编写的代码

public View getView(final int position, View convertView, ViewGroup parent) {

        View theView = super.getView(position, convertView, parent);     // It took me three days to fix :) I posed a question for it in StackOverFlow
        Button deleteButt = (Button) theView.findViewById(R.id.deleteButton);
        deleteButt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                // This code to delete a row from the class Orders was taken from Back4app help https://www.back4app.com/docs/android/parse-objects/crud
                ParseQuery<ParseObject> query = ParseQuery.getQuery("Orders");
                query.whereEqualTo("objectId", mainObjectsArray.get(position)[3]);  // mainObjectsArray.get(position)[3] We fetched the object id and stored it in mainObjectArray. position is the position of the item in the list which is the same position of the the same item in the mainObjectArray. Smart isn't it?! :)
                query.findInBackground(new FindCallback<ParseObject>() {
                    @Override
                public void done(final List<ParseObject> object, ParseException e) {
                    if (e == null) {

                        // The dialog 
                        AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
                        alert.setMessage("Are you sure you want to delete this order?")
                                .setNegativeButton("No", null)
                                .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {

                                        //Delete based on the position
                                        object.get(0).deleteInBackground(new DeleteCallback() {
                                            @Override
                                            public void done(ParseException e) {
                                                if (e == null) {
                                                    Toast.makeText(getActivity(), "Deleted!", Toast.LENGTH_SHORT).show();
                                                    
                                                } else {
                                                    Toast.makeText(
                                                            getApplicationContext(),
                                                            e.getMessage().toString(),
                                                            Toast.LENGTH_LONG
                                                    ).show();
                                                }
                                            }
                                        });

                                    }
                                        });

                        alert.create().show();


                    } else {
                        Toast.makeText(
                                getApplicationContext(),
                                e.getMessage().toString(),
                                Toast.LENGTH_LONG
                        ).show();
                    }
                };
            });

            }
        });

        return theView;
    }

之后

Toast.makeText(getActivity(), "Deleted!", Toast.LENGTH_SHORT).show();

我应该编写代码来刷新片段,以便从数据库中获取新数据。 任何帮助都将得到解决! 非常感谢。

2 个答案:

答案 0 :(得分:0)

一旦删除订单,只需从适配器列表中删除该订单并通知适配器。

答案 1 :(得分:0)

单击删除按钮后,立即从适配器列表中删除该项目并使用notifyDataSetChanged(),它将更新您的列表项目。您无需为此更新完整的片段。

编辑:-您可以使用notifyItemRemoved()而不是使用notifyDataSetChanged(),因为这将更新整个recyclerView,这是一项繁重的操作。 Here,您可以在notifyItemRemoved()上找到更多详细信息。

相关问题