问题
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.
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.
* EDIT *
So, I have progressed with this problem a bit. When I click my buttonUpdateProgress, the part of the ListView (progress) updates perfectly.
However, along with it, it removes my strings title and description from the ListView. Any ideas so that it only progress changes?
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;
}
回答1:
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);
来源:https://stackoverflow.com/questions/49648561/updating-firebase-records-that-are-displayed-in-an-android-listview