更新Android ListView中显示的Firebase记录 [英] Updating Firebase records that are displayed in an Android ListView

查看:66
本文介绍了更新Android ListView中显示的Firebase记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

So far, I have been able to display my Firebase records for node maintenance in an Android ListView. However, there is one part of these records that I wish to update.

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

When clicking on a record, an AlertDialog dialog will appear with a spinner (spinnerProgress). By changing the option on this spinner and clicking an OK 'button', then the ListView and of course the Firebase database will update.

*编辑*

因此,我在解决该问题上已有所进展.当我单击我的buttonUpdateProgress时,ListView(progress)的部分会完美更新.

So, I have progressed with this problem a bit. When I click my buttonUpdateProgress, the part of the ListView (progress) updates perfectly.

但是,它与之一起从ListView中删除了我的字符串titledescription.有什么想法可以让它仅更改progress吗?

However, along with it, it removes my strings title and description from the ListView. Any ideas so that it only progress changes?

下面是我的代码:

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;

    }

推荐答案

能否请尝试用您的功能替换此功能,然后尝试

一些解释:

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;
}

注意:像下面的示例一样,仅放入要更新的参数,如果只想更改进度,则代码段将像这样,

Note: put only params you want to update like in example below, here if you want to change progress only then snippet will be like,

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

params.put("progress", progress); 

ref.updateChildren(params);

这篇关于更新Android ListView中显示的Firebase记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆