Android Firebase写入数据会加载另一个片段或使用这些片段重新加载活动

时间:2019-02-20 13:45:06

标签: android firebase firebase-realtime-database

我用底部导航创建了活动“ home”,其中包含2个片段:“ lecturerHome”和“ lecturerSubject”。我在连接到Firebase的“ lecturerSubject”列表视图中添加了显示年份和带有按钮的EditText,以将年份添加到列表视图。问题是,当我单击按钮时,firebase添加一切正常,新项目显示在列表视图中,但突然(可能是“ home”活动)开始重新加载。我的主要活动观点是“ lecturerHome”。我该如何阻止这种情况发生?

package com.example.studentapp;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.ChildEventListener;
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.time.Year;
import java.util.ArrayList;

import static android.R.layout.simple_list_item_1;

public class lecturerSubject extends Fragment {
private FirebaseAuth mAuth;
private DatabaseReference mDatabase;

private RelativeLayout layout;
private ListView yearsList, groupsList;
private ProgressBar progressBar;
private TextView addYear, addNextYear, addGroup, addNextGroup;
private Button addYearBtn, addGroupBtn;
private EditText addYearEdit;
private String newYear, nextYear;
private ArrayList<String> yearsArr = new ArrayList<>();
private ArrayList<String> groupsArr = new ArrayList<>();
private ArrayAdapter<String> adapter;
private ArrayAdapter<String> adapterGroups;
private lecturerSubject lecturerSubject;
private boolean firstYear, firstGroup;

SharedPreferences pref;
SharedPreferences.Editor editor;

public lecturerSubject() {
    // Required empty public constructor
}

public static lecturerSubject newInstance() {
    lecturerSubject fragment = new lecturerSubject();
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    pref = getContext().getSharedPreferences("com.example.studentApp", 0);
    editor = pref.edit();

    mAuth = FirebaseAuth.getInstance();
    mDatabase = FirebaseDatabase.getInstance().getReference().child("Users").child(mAuth.getCurrentUser().getUid());
    lecturerSubject = new lecturerSubject();
    View v = inflater.inflate(R.layout.fragment_lecturer_subject, container, false);

    layout = v.findViewById(R.id.lecturer_subject_layout);
    addYear = v.findViewById(R.id.add_year_text);
    addNextYear = v.findViewById(R.id.add_next_year_text);
    addGroup = v.findViewById(R.id.add_group_text);
    addNextGroup = v.findViewById(R.id.add_next_group_text);
    addYearBtn = v.findViewById(R.id.add_year_button);
    addGroupBtn = v.findViewById(R.id.add_group_button);
    addYearEdit = v.findViewById(R.id.add_year_edit);
    progressBar = v.findViewById(R.id.add_progress);
    yearsList = v.findViewById(R.id.years_list);
    groupsList = v.findViewById(R.id.group_list);

    adapter = new ArrayAdapter<String>(this.getContext(), android.R.layout.simple_list_item_1, yearsArr);
    yearsList.setAdapter(adapter);

    adapterGroups = new ArrayAdapter<String>(this.getContext(), android.R.layout.simple_list_item_1, groupsArr);
    groupsList.setAdapter(adapterGroups);

    firstYear = pref.getBoolean("firstYear", false);

    if(firstYear){
        addYear.setVisibility(View.VISIBLE);
    } else{
        addNextYear.setVisibility(View.VISIBLE);
    }

    addYearBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            yearsList.setVisibility(View.VISIBLE);
            newYear = addYearEdit.getText().toString();

            mDatabase.child("year").child(newYear).setValue(" ").addOnCompleteListener(new OnCompleteListener<Void>() {
                @Override
                public void onComplete(@NonNull Task<Void> task) {
                    if(task.isSuccessful()){
                        showMessage("OK");
                    }
                }
            });

        }
    });

在editText中写入任何内容并单击firebase按钮可以很好地写入数据,并在任务完成后将其重新加载到讲师的Home片段(或重新加载活动)。

enter image description here enter image description here

0 个答案:

没有答案