引用更改并变为空时,FirebaseUI RecyclerView不会更新



所以我使用FirebaseUI FirebaseRecyclerAdapter来填充我的回收器视图。

我的情况是,我希望用户能够选择一个日期,并查看firebase数据库中该日期的数据。

为了实现这一点,我现在有一个编辑文本,它打开一个日期选择器对话框,并将编辑文本中的文本设置为ddMMyyyy格式的选定日期。

之后,当用户单击按钮时,回收器视图应显示该特定日期的数据。

问题是,当单击该按钮时,我可以验证适配器是否确实从数据库中获取了新数据,但在打开键盘之前,回收器视图将完全为空。

这是一个很好的方法吗?或者我应该采取另一种方法?任何帮助都将不胜感激。

这是我在这里的第一个问题,很抱歉有任何错误。

讲师OnDate.java

public class LecturesOnDate extends AppCompatActivity {
EditText dateSelector;
Button setDate;
RecyclerView rcViewOnDate;
Calendar calendar;
Date date;
FirebaseUIAdapterAdmin rcAdapter;
DatabaseReference dateReference, weekReference;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lecture_on_date);
rcViewOnDate = findViewById(R.id.RCViewOnDate);
dateSelector = findViewById(R.id.pickDate);
setDate = findViewById(R.id.setButton);
rcViewOnDate.setHasFixedSize(true);
rcViewOnDate.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
calendar = Calendar.getInstance();
date = calendar.getTime();
String today = new SimpleDateFormat("ddMMyyyy", Locale.getDefault()).format(date);
DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker datePicker, int year, int month, int day) {
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, month);
calendar.set(Calendar.DAY_OF_MONTH, day);
SimpleDateFormat dateFormat=new SimpleDateFormat("ddMMyyyy", Locale.US);
dateSelector.setText(dateFormat.format(calendar.getTime()));
}
};
dateSelector.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new DatePickerDialog(AddLectureOnDate.this, date, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)).show();
}
});

setDate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String dateOn = dateSelector.getText().toString();

dateReference = FirebaseDatabase.getInstance().getReference("Lecture/5A3/edited/" + dateOn);

FirebaseRecyclerOptions<Lecture> options =
new FirebaseRecyclerOptions.Builder<Lecture>()
.setQuery(dateReference, Lecture.class)
.setLifecycleOwner(Lecture.this)
.build();
rcAdapter = new FirebaseUIAdapterAdmin(options);
rcViewOnDate.setAdapter(rcAdapter);
rcAdapter.notifyDataSetChanged();
}
});
}
}

这是问题的GIF

在我选择一个日期并点击设置日期按钮后,回收器视图将变为空白,直到我点击另一个编辑文本(不在视频中(打开键盘

这是我的数据库(经过编辑以澄清并缩短(

这是我的数据库的屏幕截图

{
"Lecture": {
"5A3": {
"edited": {
"12102022": [     //This should represent the dateOn used in reference
{
"classCode": "UCSE571 LAB",
"dateOn": "12102022",  //This is just for testing
"endTime": "12:30",
"startTime": "9:30",
"teacherName": "BKC"
}
],
"18102022": [
{
"classCode": "UCSE503",
"dateOn": "18102022",  //This is just for testing
"endTime": "9:30",
"startTime": "8:30",
"teacherName": "RNM"
}
]
}
}
}
}

即使引用存在于数据库中,数据仍然不会加载到回收器视图中,直到键盘打开然后关闭。

这是型号类别

package xyz.coolidance.xpscheduler.helpers;
public class Lecture {
String classCode, teacherName, startTime, endTime, dateOn;
Lecture(){
}
public Lecture(String classCode, String teacherName, String startTime, String endTime, String dateOn) {
this.classCode = classCode;
this.teacherName = teacherName;
this.startTime = startTime;
this.endTime = endTime;
this.dateOn = dateOn;
}
public String getClassCode() {
return classCode;
}
public String getTeacherName() {
return teacherName;
}
public String getStartTime() {
return startTime;
}
public String getEndTime() {
return endTime;
}
public String getDateOn() {
return dateOn;
}
}

这是我为解决问题而应用的临时修复程序。

我从布局中删除了Recycler View,并将其替换为Frame layout

当用户设置日期时,将创建片段并将其与数据一起显示给用户。

以下是的相关文件

添加讲座日期

public class AddLectureOnDate extends AppCompatActivity {
EditText dateSelector;
Calendar calendar;
Date date;
String dateOn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lecture_on_date);
dateSelector = (EditText) findViewById(R.id.pickDate);
calendar = Calendar.getInstance();
date = calendar.getTime();
String today = new SimpleDateFormat("ddMMyyyy", Locale.getDefault()).format(date);
String mDay = today.substring(0, 2);
String mMonth = today.substring(2, 4);
String mYear = today.substring(4, 8);
dateSelector.setText(String.join("/", mDay, mMonth, mYear));
openFragment(today);
dateOn = today;
DatePickerDialog.OnDateSetListener dateDialog = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker datePicker, int year, int month, int day) {
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, month);
calendar.set(Calendar.DAY_OF_MONTH, day);
SimpleDateFormat dateFormat = new SimpleDateFormat("ddMMyyyy", Locale.US);
dateOn = dateFormat.format(calendar.getTime());
String mDay = dateOn.substring(0, 2);
String mMonth = dateOn.substring(2, 4);
String mYear = dateOn.substring(4, 8);
dateSelector.setText(String.join("/", mDay, mMonth, mYear));
openFragment(dateOn);
}
};
dateSelector.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
DatePickerDialog datePickerDialog = new DatePickerDialog(AddLectureOnDate.this, dateDialog, calendar.get(calendar.YEAR), calendar.get(calendar.MONTH), calendar.get(calendar.DAY_OF_MONTH));
datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis());
datePickerDialog.show();
}
});    
}
private void openFragment(String string) {
LectureOnDate fragment = new LectureOnDate();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
Bundle data = new Bundle();
data.putString("date", string);
fragment.setArguments(data);
transaction.replace(R.id.recyclerFragmentContainer, fragment).commit();
}
private void hideKeyboard() {
InputMethodManager imm = (InputMethodManager) AddLectureOnDate.this.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), 0);
}
}

这是AddLectureOnDate(activity_lature_on_date.xml(布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/background">
<TextView
android:id="@+id/tvLectures"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart="30dp"
android:paddingTop="30dp"
android:paddingEnd="30dp"
android:paddingBottom="10dp"
android:text="LECTURES"
android:textColor="@color/text"
android:textSize="30sp">
</TextView>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/dateSelect"
style="@style/InputText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/tvLectures"
android:layout_marginStart="30dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="30dp"
android:hint="Select Date"
app:counterMaxLength="20"
app:helperText="Required"
app:shapeAppearance="@style/Rounded">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/pickDate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:imeOptions="actionNext"
android:inputType="text"
android:textColor="@color/text"
android:longClickable="false"
android:focusable="false">
</com.google.android.material.textfield.TextInputEditText>
</com.google.android.material.textfield.TextInputLayout>

<ImageView
android:id="@+id/line"
android:layout_width="match_parent"
android:layout_height="6dp"
android:layout_below="@+id/dateSelect"
android:layout_marginStart="30dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="30dp"
android:contentDescription="TODO"
android:visibility="visible"
app:srcCompat="@drawable/line" />
<FrameLayout
android:id="@+id/recyclerFragmentContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/line"
android:layout_marginTop="-1dp">
</FrameLayout>

</RelativeLayout>

这是片段(LectureOnDate.java(

public class LectureOnDate extends Fragment {
RecyclerView recyclerView;
DatabaseReference databaseDay, databaseWeek;
FirebaseUIAdapterAdmin adapterAdmin;
Calendar calendar;
Date today;
String recievedDate;
TextView header;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_lecture_on_date, container, false);
Context context = view.getContext();
header = view.findViewById(R.id.textViewOnDate);
Bundle data = getArguments();
if (data != null) {
recievedDate = data.getString("date");
}
Log.d("Fragment", "onCreateView: LectureOnDate" + recievedDate);

//Changing the recievedDate String to a date so that we can determine the weekday
DateFormat sourceFormat = new SimpleDateFormat("ddMMyyy");
today = null;
try {
today = sourceFormat.parse(recievedDate);
} catch (ParseException e) {
e.printStackTrace();
}
String date = recievedDate;
String weekday_name = new SimpleDateFormat("EEEE", Locale.ENGLISH).format(today);
String day = recievedDate.substring(0, 2);
String month = recievedDate.substring(2, 4);
String year = recievedDate.substring(4, 8);
header.setText(new StringBuilder().append(String.join("/", day, month, year)).append(",  ").append(weekday_name).toString());
recyclerView = view.findViewById(R.id.RCViewOnDate);
databaseDay = FirebaseDatabase.getInstance().getReference("Lecture/5A3/edited/" + date);
databaseWeek = FirebaseDatabase.getInstance().getReference("Lecture/5A3/general/" + weekday_name);

recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(context));
FirebaseRecyclerOptions<Lecture> options =
new FirebaseRecyclerOptions.Builder<Lecture>()
.setQuery(databaseDay, Lecture.class)
.setLifecycleOwner(getViewLifecycleOwner())
.build();
adapterAdmin = new FirebaseUIAdapterAdmin(options);
recyclerView.setAdapter(adapterAdmin);
return view;
}
@Override
public void onStart() {
super.onStart();
}
@SuppressLint("NotifyDataSetChanged")
@Override
public void onStop() {
super.onStop();
if (adapterAdmin != null) {
adapterAdmin.notifyDataSetChanged();
}
}
@Override
public void onDestroy() {
Log.i("Fragment", "onDestroy: " + recievedDate);
super.onDestroy();
}
}

这是片段布局(Fragment_lature_on_date.xml(

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".fragments.Today">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
android:id="@+id/linearLayoutOnDate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/background"
android:orientation="horizontal"
android:paddingStart="35dp"
android:paddingTop="30dp"
android:paddingEnd="20dp"
android:paddingBottom="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0">
<TextView
android:id="@+id/textViewOnDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/today_top"
android:textColor="@color/text"
android:textSize="35sp" />
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/RCViewOnDate"
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="@color/background"
android:paddingStart="10dp"
android:paddingTop="5dp"
android:paddingEnd="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/linearLayoutOnDate"
app:layout_constraintVertical_bias="0.0" />
</androidx.constraintlayout.widget.ConstraintLayout>

</FrameLayout>

这是我正在使用的数据库的一小部分:-数据库部分

这是我的最终应用程序所需的输出:-输出

最新更新