更新Android ListView

时间:2018-04-04 10:18:57

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

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

点击记录时,会出现一个AlertDialog对话框,其中包含一个微调器(spinnerProgress)。通过更改此spinner上的选项并点击“确定”按钮',ListView,当然还有Firebase数据库将会更新。

*编辑*

所以,我对这个问题有所进展。当我点击buttonUpdateProgress时,ListViewprogress)的部分会完美更新。

然而,除此之外,它还会从title中删除我的字符串descriptionListView。任何想法只会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;

    }

1 个答案:

答案 0 :(得分: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;
}

注意:只放下你想要更新的参数,如下例所示,此处如果你想改变进度,那么代码片段就像,

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

params.put("progress", progress); 

ref.updateChildren(params);