简体   繁体   English

更新Android ListView中显示的Firebase记录

[英]Updating Firebase records that are displayed in an Android ListView

So far, I have been able to display my Firebase records for node maintenance in an Android ListView . 到目前为止,我已经能够在Android ListView显示我的Firebase记录以进行节点maintenance However, there is one part of these records that I wish to update. 但是,我希望更新这些记录中的一部分。

When clicking on a record, an AlertDialog dialog will appear with a spinner ( spinnerProgress ). 单击记录时,将显示一个带有微调spinnerProgressspinnerProgress )的AlertDialog对话框。 By changing the option on this spinner and clicking an OK 'button', then the ListView and of course the Firebase database will update. 通过更改此spinner上的选项并单击“确定”“按钮”,即可更新ListView ,当然还可以更新Firebase数据库。

* EDIT * *编辑*

So, I have progressed with this problem a bit. 因此,我在解决该问题上已有所进展。 When I click my buttonUpdateProgress , the part of the ListView ( progress ) updates perfectly. 当我单击buttonUpdateProgressListView的一部分( progress )会完美更新。

However, along with it, it removes my strings title and description from the ListView . 但是,与此同时,它从ListView删除了我的字符串titledescription Any ideas so that it only progress changes? 有什么想法让它只改变progress吗?

Below is my code: 下面是我的代码:

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;

    }

Can you please try replacing this function with your function and try 您能否尝试用您的函数替换此函数并尝试

Some explanation: 一些解释:

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

Function to replace 替换功能

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM