如何显示在相关活动的对话框中输入的信息?

时间:2018-02-28 13:32:37

标签: java android firebase firebase-realtime-database

在之前的应用程序中做了类似的事情,我想开始使用按钮启动一个对话框,在所述对话框中填写信息,然后将输入的信息显示在List View中。

虽然我觉得解决方案非常接近,但我遇到了一些麻烦,有几个错误不断蔓延。

我遇到的一个问题是使用按钮id: buttonAddMaintenance来启动对话框。这意味着我无法测试我在对话框中输入的信息是否存储在列表视图中。

为大型代码转储道歉。对此问题的任何帮助将不胜感激。到目前为止,已经花了大约半天的时间,并且没有在哪里。

MaintenanceActivity

        package com.example.benchalmers.myapplication;

import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

import java.util.ArrayList;
import java.util.List;

import static com.example.benchalmers.myapplication.MaintenanceActivity.MAINTENANCE_ID;

public class MaintenanceActivity extends AppCompatActivity {

    public static final String MAINTENANCE_TITLE = "maintenance_title";
    public static final String MAINTENANCE_ID = "maintenance_id";

    EditText editTextTitle;
    EditText editTextDesc;
    Spinner spinnerPrimary;
    Spinner spinnerSecondary;
    Spinner spinnerProperty;
    Button buttonAddMaintenance;
    Button buttonSubmit;

    DatabaseReference databaseMaintenance;

    ListView listViewIssues;

    List<Maintenance> maintenanceList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maintenance);

        databaseMaintenance = FirebaseDatabase.getInstance().getReference("maintenance");

        Button buttonAddMaintenace = (Button) findViewById(R.id.buttonAddMaintenance);
        buttonAddMaintenace.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {

                AlertDialog alertDialog = new AlertDialog.Builder(<MaintenanceActivity>this).create(); //Read Update

                alertDialog.setButton("Continue..", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        alertDialog.show();
                    }
                });

            }

        });

    private void showMaintenanceDialog(final String id, String maintenanceTitle, String maintenanceDescription, String maintenancePrimary, String maintenanceSecondary, String maintenanceProperty) {

        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);

        LayoutInflater inflater = getLayoutInflater();

        final View dialogView = inflater.inflate(R.layout.add_maintenance_dialog, null);

        dialogBuilder.setView(dialogView);

        final EditText editTextTitle = (EditText) dialogView.findViewById(R.id.editTextTitle);
        final EditText editTextDesc = (EditText) dialogView.findViewById(R.id.editTextDesc);
        final Button buttonSubmit = (Button) dialogView.findViewById(R.id.buttonAddMaintenance);
        final Spinner spinnerPrimary = (Spinner) dialogView.findViewById(R.id.spinnerPrimary);
        final Spinner spinnerSecondary = (Spinner) dialogView.findViewById(R.id.spinnerSecondary);
        final Spinner spinnerProperty = (Spinner) dialogView.findViewById(R.id.spinnerProperty);

        dialogBuilder.setTitle("Adding Maintenance " + maintenanceTitle);

        final AlertDialog alertDialog = dialogBuilder.create();
        alertDialog.show();

        buttonSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String title = editTextTitle.getText().toString().trim();
                String desc = editTextDesc.getText().toString().trim();
                String primary = spinnerPrimary.getSelectedItem().toString();
                String secondary = spinnerSecondary.toString().trim();
                String property = spinnerProperty.getSelectedItem().toString();

                if (TextUtils.isEmpty(title)) {

                    editTextTitle.setError("Title Required");
                    return;
                }

                addMaintenance();

                alertDialog.dismiss();
            }
        });
    }

        private void addMaintenance(){

            databaseMaintenance = FirebaseDatabase.getInstance().getReference("maintenance");

            String title = editTextTitle.getText().toString().trim();
            String desc = editTextDesc.getText().toString().trim();
            String primary = spinnerPrimary.getSelectedItem().toString();
            String secondary = spinnerSecondary.getSelectedItem().toString();
            String property = spinnerProperty.getSelectedItem().toString();

            if(!TextUtils.isEmpty(title)){

                String id = databaseMaintenance.push().getKey();

                Maintenance maintenance = new Maintenance(id, title, desc, primary, secondary, property);

                databaseMaintenance.child(id).setValue(maintenance);

                Toast.makeText(this, "Maintenance Added", Toast.LENGTH_LONG).show();

            } else {

                Toast.makeText(this, "You must enter an issue", Toast.LENGTH_LONG).show();
            }

        }
}

0 个答案:

没有答案