如何解决将数据添加到Firebase数据库的问题

时间:2019-12-30 03:36:43

标签: java android firebase firebase-realtime-database

enter image description here我是Firebase的新手,我正在尝试制作一个Android应用,用户可以通过创建活动来跟踪锻炼,然后将这些活动存储在数据库(Firebase)中。但是,当我尝试将每个活动对象添加到数据库时,仅插入某些字段。请参见下面的相关代码

活动课

public class Activity {
    private String workout, user, exercise, duration, weight, reps, date, activity_workout_user;

    Activity(String workout, String user, String exercise, String duration, String weight, String reps, String date) {
        this.workout = workout;
        this.user = user;
        this.exercise = exercise;
        this.duration = duration;
        this.weight = weight;
        this.reps = reps;
        this.date = date;
        this.activity_workout_user = exercise + "_" + workout + "_" + user;
    }

    //getters and setters
}

AddActivity类

public class AddActivity extends AppCompatActivity {
    FirebaseDatabase database;
    DatabaseReference ref;
    String user, workoutName, timeCreated;
    Spinner AAexerciseSP;
    EditText AArepsET, AAdurationET, AAweightET;
    TextView AAworkoutNameTV, AAerrorsTV, AAinfoTV;
    Button AAaddActivityBTN, AAsaveWorkoutBTN;
    static ArrayList<Activity> activities;
    int totalActivities = 0;

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

        database = FirebaseDatabase.getInstance();
        ref = database.getReference();
        SQLquery = "";
        activities = new ArrayList<>();

        Intent intent = getIntent();
        user = intent.getStringExtra("username");
        workoutName = intent.getStringExtra("workout_name");
        timeCreated = DateTime.TimeDate();

        AAworkoutNameTV = findViewById(R.id.AAworkoutNameTV);
        AAerrorsTV = findViewById(R.id.AAerrorsTV);
        AAinfoTV = findViewById(R.id.AAinfoTV);
        AAexerciseSP = findViewById(R.id.AAexerciseSP);
        AAaddActivityBTN = findViewById(R.id.AAaddActivityBTN);
        AAsaveWorkoutBTN = findViewById(R.id.AAsaveWorkoutBTN);
        AArepsET = findViewById(R.id.AArepsET);
        AAdurationET = findViewById(R.id.AAdurationET);
        AAweightET = findViewById(R.id.AAweightET);

        AAworkoutNameTV.setText(String.format("Workout name: %s", workoutName));

        // Create an ArrayAdapter using the string array and a default spinner layout
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
                R.array.exercise_list, android.R.layout.simple_spinner_item);

        // Specify the layout to use when the list of choices appears
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        // Apply the adapter to the spinner
        AAexerciseSP.setAdapter(adapter);

        AAexerciseSP.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                String item = (String) parent.getSelectedItem();
                String[] array = getResources().getStringArray(R.array.cardio_exercises);
                List<String> exercises = Arrays.asList(array);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }

    public String selectedExercise(){
        return (String) AAexerciseSP.getSelectedItem();
    }

    public void addActivity(View view){
        String activityExercise = selectedExercise();
        String activityUser = user;
        String activityWorkout = workoutName;
        String activityDuration = AAdurationET.getText().toString();
        String activityReps = AArepsET.getText().toString();
        String activityWeight = AAweightET.getText().toString();
        String activityDate = DateTime.TimeDate();

        Activity activity = new Activity(activityWorkout, activityUser, activityExercise,
                activityDuration, activityWeight, activityReps, timeCreated);

        activities.add(activity);
        totalActivities += 1;

    }

    public void saveWorkout(View view){
        for (Activity activity:activities){
            String key = database.getReference("activities").push().getKey();

            assert key != null;
            ref.child("activities").child(key).setValue(activity);
        }

        Workout workout = new Workout(workoutName,user,timeCreated,"");
        String key = database.getReference("workouts").push().getKey();

        assert key != null;
        ref.child("workouts").child(key).setValue(workout);

        startActivity(new Intent(this, HomePage.class));

    }

}

将每个活动对象插入数据库都是通过“ saveWorkout”方法进行的,但是不是插入类中的所有字段,而是仅插入“ activity_workout_user”和“ user”字段。请参见下面的数据库摘录。

任何帮助将所有字段都插入的方法将不胜感激。

预先感谢

欢呼

1 个答案:

答案 0 :(得分:0)

我想发表评论,但是我对Stackoverflow非常陌生,所以我还不能发表评论。 我最近才开始自己使用Firebase,但我没有学会100%正确地使用它。 但是我读到,谈到向数据库中添加自定义对象时,这些对象应该具有一个没有参数的附加构造函数,并且每个变量都应该有getter和setter方法。