我有一个活动,该活动使用RecyclerView
在FirebaseRecyclerAdapter
中显示一个列表。查询必须是动态的-我需要在其结尾添加一个日期。因此,我设置了一个DatePicker DialogFragment。 dialogfragment
有一个接口,该接口已在活动中实现,以便从DatePicker
接收日期。日期又回来了。麻烦的是,当我将新的适配器重新连接到RecyclerView
时,适配器代码永远不会运行。通过使用调试器进行遍历,日期将返回到活动中,然后代码在片段中结束,并且适配器永远不会重新连接。因此,我只剩下空白屏幕而不是数据列表。
活动:
public class DailyScheduleActivity extends AppCompatActivity implements
HourlyTaskDialogFragment.HourlyTaskDialogListener,
DatePickerFragment.OnFragmentInteractionListener {
private DatabaseReference mDatabase;
private RecyclerView mProtocolRv;
private String protocolKey;
private LocalDate chosenDate;
private ProtocolRecyclerViewAdapter mProtocolAdapter;
private String path;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_daily_schedule);
if (chosenDate == null) {
chosenDate = new LocalDate();
}
protocolKey = "daily/" + FirebaseAuth.getInstance().getCurrentUser().getUid() + "_";
path = protocolKey + chosenDate;
mDatabase = FirebaseDatabase.getInstance().getReference();
Query query = mDatabase.child(path);
FirebaseRecyclerOptions<Hour> options =
new FirebaseRecyclerOptions.Builder<Hour>()
.setQuery(query, Hour.class)
.build();
mProtocolRv = findViewById(R.id.rv_protocol);
mProtocolRv.setHasFixedSize(false);
LinearLayoutManager mLayoutManager = new LinearLayoutManager(this);
mProtocolRv.setLayoutManager(mLayoutManager);
mProtocolAdapter = new ProtocolRecyclerViewAdapter(this, mDatabase, path, options);
mProtocolRv.setAdapter(mProtocolAdapter);
}
//####################################################################
//#### This is what runs after the date has been chosen. When this is
//#### finished, the dialogfragment finishes running. When control
//#### returns to the activity, the adapter never loads the data.
//####################################################################
@Override
public void onFragmentInteraction(String date) {
chosenDate = new LocalDate(date);
path = protocolKey + chosenDate;
Query query = mDatabase.child(path);
FirebaseRecyclerOptions<Hour> options =
new FirebaseRecyclerOptions.Builder<Hour>()
.setQuery(query, Hour.class)
.build();
final FirebaseRecyclerAdapter oldAdapter = (FirebaseRecyclerAdapter) mProtocolRv.getAdapter();
mProtocolAdapter = new ProtocolRecyclerViewAdapter(this, mDatabase, path, options);
mProtocolRv.setAdapter(mProtocolAdapter);
mProtocolAdapter.notifyDataSetChanged();
if (oldAdapter != null) {
//oldAdapter.cleanup(); - This can't run here because cleanup is not public so not sure what to do with it!
}
}
public static class ProtocolRecyclerViewAdapter extends FirebaseRecyclerAdapter<Hour, ProtocolRecyclerViewAdapter.HourHolder> {
private final DailyScheduleActivity mParentActivity;
private DatabaseReference mDb;
private String protocolUserDateKey;
ProtocolNonMalignant mProtocol = new ProtocolNonMalignant();
ProtocolRecyclerViewAdapter(DailyScheduleActivity parent, DatabaseReference db, String path, FirebaseRecyclerOptions<Hour> options) {
super(options);
mParentActivity = parent;
mDb = db;
protocolUserDateKey = path;
}
@Override
public void onDataChanged() {
super.onDataChanged();
}
@NonNull
@Override
public HourHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.rv_protocol_row, parent, false);
return new HourHolder(view);
}
@Override
public void onBindViewHolder(final @NonNull HourHolder holder, final int position, final Hour currentHour) {
holder.mHourCheckBox.setText(currentHour.toString() + protocolUserDateKey);
}
class HourHolder extends RecyclerView.ViewHolder {
final IndeterminateCheckBox mHourCheckBox;
final ImageView mOpenTasks;
final TextView mHour;
HourHolder(View view) {
super(view);
mHourCheckBox = (IndeterminateCheckBox) view.findViewById(R.id.cb_hour_all);
mOpenTasks = (ImageView) view.findViewById(R.id.open_tasks);
mHour = (TextView) view.findViewById(R.id.hour);
}
}
}
@Override
protected void onStart() {
super.onStart();
mProtocolAdapter.startListening();
}
@Override
protected void onStop() {
super.onStop();
mProtocolAdapter.stopListening();
}
}
Datepicker DialogFragment:
public class DatePickerFragment extends DialogFragment {
private OnFragmentInteractionListener mListener;
private String chosenDate;
public DatePickerFragment() {}
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
return new DatePickerDialog(getActivity(), dateSetListener, year, month, day);
}
private DatePickerDialog.OnDateSetListener dateSetListener =
new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int month, int day) {
chosenDate = view.getYear() + "-" + (view.getMonth()+1) + "-" + view.getDayOfMonth();
if (mListener != null) {
mListener.onFragmentInteraction(chosenDate);
}
}
};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_date_picker, container, false);
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public interface OnFragmentInteractionListener {
void onFragmentInteraction(String date);
}
}