【问题标题】:Updating Firebase records that are displayed in an Android ListView更新 Android ListView 中显示的 Firebase 记录
【发布时间】:2018-04-04 21:05:14
【问题描述】:

到目前为止,我已经能够在 Android ListView 中显示节点 maintenance 的 Firebase 记录。但是,我希望更新这些记录的一部分。

单击记录时,将出现一个带有微调器 (spinnerProgress) 的 AlertDialog 对话框。通过更改此spinner 上的选项并单击“确定”按钮,然后ListView 和Firebase 数据库将更新。

* 编辑 *

所以,我在这个问题上取得了一些进展。当我点击我的buttonUpdateProgress 时,ListView (progress) 的部分会完美更新。

但是,它同时从ListView 中删除了我的字符串titledescription。有什么想法让它只有progress 改变吗?

下面是我的代码:

listViewIssues.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            Maintenance maintenance = maintenanceList.get(position);

            showProgressDialog(maintenance.getMaintenanceId(), maintenance.getMaintenanceTitle(), maintenance.getMaintenanceDescription(), maintenance.getMaintenanceProperty(), maintenance.getMaintenanceProgress());
        }
    });

--

private void showProgressDialog(final String id, String title, String description, String property, String maintenanceTitle) {

        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);

        LayoutInflater inflater = getLayoutInflater();

        final View dialogView = inflater.inflate(R.layout.archive_maintenance, null);

        dialogBuilder.setView(dialogView);

        final Spinner spinnerProgress = (Spinner) dialogView.findViewById(R.id.spinnerProgress);
        final Button buttonUpdateProgress = (Button) dialogView.findViewById(R.id.buttonUpdateProgress);

        dialogBuilder.setTitle("Maintenance: " + maintenanceTitle);

        final AlertDialog alertDialog = dialogBuilder.create();
        alertDialog.show();

        buttonUpdateProgress.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String title = editTextTitle.getText().toString().trim();
                String desc = editTextDesc.getText().toString().trim();

                String progress = spinnerProgress.getSelectedItem().toString();
                String property = spinnerProperty.getSelectedItem().toString();

                updateProgress(title, desc, id, property, progress);

                alertDialog.dismiss();
            }
        });
    }

--

private boolean updateProgress (String title, String desc, String id, String property, String progress) {

        DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("maintenance").child(id);
        Maintenance maintenance = new Maintenance(id, title, desc, property, progress);

        FirebaseUser user = mAuth.getCurrentUser();
        String uid = user.getUid();

        databaseMaintenance.child(uid).child(id).setValue(maintenance);

        databaseReference.setValue(maintenance);

        Toast.makeText(this, "Maintenance Updated Updated Successfully", Toast.LENGTH_LONG).show();

        return true;

    }

【问题讨论】:

  • 还有什么错误吗
  • 有什么问题?你没有提到这个
  • @HemalHerath 我已经更新了问题。
  • @DeepPatel 我已经更新了问题。
  • @wright_arturo :请在下面查看我的答案。添加了一些空间和评论以方便您使用

标签: java android firebase firebase-realtime-database android-recyclerview


【解决方案1】:

能否请您尝试用您的函数替换此函数并尝试

一些解释:

ref.updateChildren(hashMap); // will let you update the only data you want to change

ref.setValue(model);  // will replace the existing data with the model you provided

替换功能

private boolean updateProgress (String title, String desc, String id, String property, String progress) {
    DatabaseReference databaseReference = FirebaseDatabase.getInstance()
                           .getReference().child("maintenance");
    FirebaseUser user = mAuth.getCurrentUser();
    String uid = user.getUid();

    Map<String, Object> params = new HashMap<>();

    params.put("progress", progress);  // put only params you want to update

    databaseReference.child(uid)
                     .child(id)
                     .updateChildren(params);

    Toast.makeText(this, "Maintenance Updated Updated Successfully", Toast.LENGTH_LONG).show();
    return true;
}

注意:只输入你想更新的参数,如下例所示,如果你只想改变进度,那么 sn-p 就像,

Map<String, Object> params = new HashMap<>();

params.put("progress", progress); 

ref.updateChildren(params);

【讨论】:

  • 谢谢。值得庆幸的是,现在其他值(标题、描述)并没有消失。但是progress 现在不会改变。 LogCat 说有一个 DB 错误 - 权限被拒绝。有什么想法吗?
  • 请填写完整的logcat数据
  • 04-04 11:33:11.233 14691-14743/com.example.xxx.myapplication W/RepoOperation: updateChildren at /maintenance/-L9FPsVynRtOjPg7m9DD failed: DatabaseError: Permission denied
  • 这很奇怪,考虑到我能够改变它。尽管以titledesc 为代价
  • 这看起来像是您的 google-services.json 的问题。确保应用名称和客户端 ID 与 Firebase 控制台中的相同。如果您不确定从项目控制台重新下载 google-services.json 并将其添加到您的项目中。
猜你喜欢
  • 2017-11-27
  • 2021-06-03
  • 1970-01-01
  • 2020-06-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多