我目前有一个Android ListView
,用于显示Firebase数据库中的数据。当我点击ListView
中的数据记录时,会显示Alert Dialog
和存档Button
。
我希望将选定的记录转移到我的应用程序中的另一个活动中的另一个ListView
,即“已归档”。
我知道这意味着要为我的Firebase数据库创建一个archive
节点。
以下是调出Alert Dialog
的方法,其中包含Button
来执行我的方法archiveMaintenance
。然而,我被困在哪里,从哪里开始放入archiveMaintenance
。
showProgressDialog
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();
}
});
buttonUpdateArchive.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
archiveMaintenance(id);
}
});
}
archivedMaintenance
private void archiveMaintenance(String id) {
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference recordsRef = rootRef.child("maintenance");
DatabaseReference recordIdRef = recordsRef.child(id);
DatabaseReference archivedRecordsRef = rootRef.child("archive");
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
MaintenanceList maintenanceList = dataSnapshot.getValue(MaintenanceList.class); //First step
archivedRecordsRef.child(id).setValue(maintenanceList); //Second step
dataSnapshot.getRef().removeValue(); //Third step
}
@Override
public void onCancelled(DatabaseError databaseError) {}
};
recordIdRef.addListenerForSingleValueEvent(valueEventListener);
}
MaintenanceList
public class MaintenanceList extends ArrayAdapter<Maintenance> {
private Activity context;
private List<Maintenance> maintenanceList;
public MaintenanceList(Activity context, List<Maintenance> maintenanceList) {
super(context, R.layout.maintenance_list_layout, maintenanceList);
this.context = context;
this.maintenanceList = maintenanceList;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View listViewItem = inflater.inflate(R.layout.maintenance_list_layout, null, true);
TextView textViewTitle = (TextView) listViewItem.findViewById(R.id.textViewTitle);
TextView textViewDesc = (TextView) listViewItem.findViewById(R.id.textViewDesc);
TextView textViewProperty = (TextView) listViewItem.findViewById(R.id.textViewProperty);
TextView textViewProgress = (TextView) listViewItem.findViewById(R.id.textViewProgress);
Maintenance maintenance = maintenanceList.get(position);
textViewTitle.setText(maintenance.getMaintenanceTitle());
textViewDesc.setText(maintenance.getMaintenanceDescription());
textViewProperty.setText(maintenance.getMaintenanceProperty());
textViewProgress.setText(maintenance.getMaintenanceProgress());
return listViewItem;
}
}
我已经创建了ListView
并准备好接收数据的活动 - ArchiveList
。
对此的任何想法/指示都将非常感激。
答案 0 :(得分:0)
在archiveMaintenance(id)
方法中,您需要添加可帮助您将记录从某个位置移动到另一个位置的代码。假设您有一个records
节点,其中存在所有记录,archivedRecords
节点存在所有存档记录。假设您有一个名为RecordModel
的帮助程序类,要将记录从一个位置移动到另一个位置,您需要按照以下步骤操作:
您的方法应如下所示:
private void archiveMaintenance(String recordId) {
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference recordsRef = rootRef.child("records");
DatabaseReference recordIdRef = recordsRef.child(recordId);
DatabaseReference archivedRecordsRef = rootRef.child("archivedRecords");
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
RecordModel recordModel = dataSnapshot.getValue(RecordModel.class); //First step
archivedRecordsRef.child(recordId).setValue(recordModel); //Second step
dataSnapshot.getRef().removeValue(); //Third step
}
@Override
public void onCancelled(DatabaseError databaseError) {}
};
recordIdRef.addListenerForSingleValueEvent(valueEventListener);
}