gpt4 book ai didi

java - 更新 Android ListView 中显示的 Firebase 记录

转载 作者:行者123 更新时间:2023-12-02 11:25:37 25 4
gpt4 key购买 nike

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

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

* 编辑*

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

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

下面是我的代码:

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

一些解释:

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

注意:仅放置您想要更新的参数,如下例所示,如果您想更改进度,则代码 fragment 将如下所示,

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

params.put("progress", progress);

ref.updateChildren(params);

关于java - 更新 Android ListView 中显示的 Firebase 记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49648561/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com