LiveData为什么不刷新UI?

时间:2020-02-19 15:38:10

标签: database android-recyclerview android-livedata customdialog

我仍在学习编程,遇到了无法再进一步的问题:

我使用RecyclerView来显示用户可用记录的概述。现在,我想让该用户可以在我的数据库中编辑一个名称。为此,我打开一个对话框并显示当前名称。用户可以在此处输入新名称并保存。很好该名称已保存在数据库中,但没有调用LiveData观察器的onChange Methode更新RecyclerView。新名称仅在重新启动应用程序后显示或离开并再次输入“活动”。

以下是相关的代码数字:

我的活动的onCreate-Method:

@Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle(R.string.tv_ao_headline);
        setContentView(R.layout.activity_child_accounts);

        // get floating ActionButton
        FloatingActionButton bAddAccount = findViewById(R.id.fabAddAccount);
        final TextView tvEmptyAccountList = findViewById(R.id.tv_empty_account_list);

        // create AccountList and add AccountAdapter
        LinearLayoutManager layoutmanager = new LinearLayoutManager(this);
        layoutmanager.setOrientation(LinearLayoutManager.VERTICAL);

        final RecyclerView recyclerView = findViewById(R.id.rv_account_list);
        recyclerView.setLayoutManager(layoutmanager);
        recyclerView.setHasFixedSize(true);

        final AccountAdapter adapter = new AccountAdapter();
        recyclerView.setAdapter(adapter);

        // get newest Data from Database
        bookdataViewModel = ViewModelProviders.of(this).get(BookdataViewModel.class);
        bookdataViewModel.getAllEntries().observe(this, new Observer<List<LSBookdata>>() {
            @Override
            public void onChanged(List<LSBookdata> lsBookdata) {
                if (lsBookdata.isEmpty()) {
                    recyclerView.setVisibility(View.GONE);
                    tvEmptyAccountList.setVisibility(View.VISIBLE);
                } else {
                    recyclerView.setVisibility(View.VISIBLE);
                    tvEmptyAccountList.setVisibility(View.GONE);
                    adapter.setAccounts(lsBookdata);
                }
            }
        });

调用我的“ showEditDialog”的onSwipe方法-显示自定义对话框的方法:

// react of actions on RecyclerView
        new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {

            private Drawable icon, iconDelete, iconEdit;
            private ColorDrawable background, backgroundDelete, backgroundEdit;

            @Override
            public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
                return false;
            }

            @Override
            public void onSwiped(@NonNull final RecyclerView.ViewHolder viewHolder, int direction) {
                // get context
                Context context = viewHolder.itemView.getContext();


                if (direction == ItemTouchHelper.RIGHT) {

                    showEditDialog(viewHolder, adapter);

                }
            }

和showEditDialog-Method来显示对话框并更新数据库:

public void showEditDialog(final RecyclerView.ViewHolder viewHolder, final AccountAdapter adapter) {
        final AlertDialog dialogBuilder = new AlertDialog.Builder(this).create();
        LayoutInflater inflater = this.getLayoutInflater();
        View dialogView = inflater.inflate(R.layout.custom_dialog, null);

        // Get Data from RecyclerView to edit
        final LSBookdata lsBookdata = adapter.getAccoutAtPosX(viewHolder.getAdapterPosition());

        final EditText et_accountChildName = (EditText) dialogView.findViewById(R.id.et_cd_childname);
        et_accountChildName.setText(lsBookdata.child_name);

        final TextView tv_editDialogHeadline = (TextView) dialogView.findViewById(R.id.tv_dialog_headline);
        tv_editDialogHeadline.setText(R.string.tv_editdialog_headline);

        Button bCreate = (Button) dialogView.findViewById(R.id.b_create);
        bCreate.setText(R.string.bao_edit_text); // Change Button Text


        Button bCancel = (Button) dialogView.findViewById(R.id.b_cancel);

        bCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialogBuilder.dismiss();
                // refresh Account Overview
                adapter.notifyItemChanged(viewHolder.getAdapterPosition());
            }
        });

        bCreate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                UserLokalStore userLokalStore = UserLokalStore.getInstance(getApplicationContext());

                // Create query for updating Child_name in Database
                SupportSQLiteQuery query = new SimpleSQLiteQuery("UPDATE " + getString(R.string.tableName) + " " +
                        "SET child_name = '" + et_accountChildName.getText().toString() + "' WHERE customer_id = " + userLokalStore.getLoggedInUser().getUserId() + " AND id = " + lsBookdata.id);

                try {
                    // update Child_Name in Database
                    bookdataViewModel.update(query);

                } catch (Exception e) {
                    e.printStackTrace();
                    Toast.makeText(getApplicationContext(), "Fehler: " + e, Toast.LENGTH_LONG).show();
                }

                dialogBuilder.dismiss();
                // refresh Account Overview
                adapter.notifyItemChanged(viewHolder.getAdapterPosition());
            }
        });
        dialogBuilder.setView(dialogView);
        dialogBuilder.show();
    }

有人可以进一步帮助我吗?

更新:现在,我知道当我在@Dao中使用RawQuery时,它不会触发Observer.onChange。但是为什么而且有人知道如何解决此问题?它如何触发Observer.onChange?

0 个答案:

没有答案