android - OnBackPressed 重复列表在 android 中添加片段
问题描述
嗨,在下面的从一个片段到当前片段重复列表的后按中,使用 android 中的 recyclerview 创建。
如果我加载我的片段,它会给我正确的响应。但是移动到下一个片段并返回到当前片段。当我回到当前片段时,它会显示重复列表而不是关闭应用程序。
public class HomeFragment extends Fragment implements ViewAppointmentAdapter.SelectIemClickListner,ViewAppointmentAdapter.StartSelectIemClickListner,DoctorAdapter.SelectIemClickListner, SpeclializationAdapter.MyItemClickListener, PatientAdapter.SelectIemClickListner,DoctorPatientListAdapter.SelectIemClickListner,DoctorPatientListAdapter.StartSelectIemClickListner,ViewAppointmentAdapter.SelectIemCancelClickListner {
public HomeFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
((NavigationViewActivity) getActivity()).setActionBarTitle("Home");
View rootView = inflater.inflate(R.layout.activity_home, container, false);
data_viewpatientlists = new ArrayList<>();
viewpatientdataLists = new ArrayList<>();
recyclerAppointmentlist = rootView.findViewById(R.id.recyclerAppointmentlist);
recyclerAppointmentlist.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
viewAppointmentAdapter = new ViewAppointmentAdapter(requireContext(), data_viewpatientlists, this, this, this);
data_viewpatientlists.clear();
viewPatientList();
}
private void viewPatientList() {
// progressDialog = new ProgressDialog(getContext());
// progressDialog.setIndeterminate(true);
// progressDialog.setMessage("Loading...");
// progressDialog.setCanceledOnTouchOutside(false);
// progressDialog.setCancelable(false);
// progressDialog.show();
String RegistrationNo = LoginId;
final APIService service = RetroClass.getRetrofitInstance().create(APIService.class);
Call<Patient_ViewPatientlist> call = service.ViewPatientList(RegistrationNo);
Log.wtf("URL Called", call.request().url() + "");
call.enqueue(new Callback<Patient_ViewPatientlist>() {
@Override
public void onResponse(Call<Patient_ViewPatientlist> call, Response<Patient_ViewPatientlist> response) {
Log.e("response", new Gson().toJson(response.body()));
if (response.isSuccessful()) {
Log.e("response", new Gson().toJson(response.body()));
Patient_ViewPatientlist patient_patientlist = response.body();
viewpatientdataLists = patient_patientlist.getData();
for (Data_Viewpatientlist view_patientList : viewpatientdataLists) {
String First_name = view_patientList.getFName();
String last_name = view_patientList.getLName();
String SlotFrom = view_patientList.getSlotFrom();
String SlotTo = view_patientList.getSlotTo();
String FromDate = view_patientList.getForDate();
String booking_id = view_patientList.getBookingId();
String doct_id = view_patientList.getDocId();
String profile_photo = view_patientList.getProfllePhoto();
DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd");
DateFormat outputFormat = new SimpleDateFormat("dd-MMM-yyyy");
Date date = null;
try {
date = inputFormat.parse(FromDate);
} catch (ParseException e) {
e.printStackTrace();
}
String outputDateStr = outputFormat.format(date);
Data_Viewpatientlist data_doctorpatientlist = new Data_Viewpatientlist(booking_id, First_name, last_name, outputDateStr, SlotFrom, SlotTo);
data_viewpatientlists.add(data_doctorpatientlist);
Log.d("data_viewpatientlists", String.valueOf(data_viewpatientlists.size()));
recyclerAppointmentlist.setAdapter(viewAppointmentAdapter);
viewAppointmentAdapter.notifyDataSetChanged();
}
}
// progressDialog.dismiss();
}
@Override
public void onFailure(Call<Patient_ViewPatientlist> call, Throwable t) {
Log.d("error", t.getMessage());
// progressDialog.dismiss();
}
});
}
解决方案
尝试这个:
首先在 onResponse() 中清除 'recyclerAppointmentlist' 的数据,然后添加新记录
它看起来像这样:
call.enqueue(new Callback<Patient_ViewPatientlist>() {
@Override
public void onResponse(Call<Patient_ViewPatientlist> call, Response<Patient_ViewPatientlist> response) {
Log.e("response", new Gson().toJson(response.body()));
if (response.isSuccessful()) {
viewpatientdataLists.clear()
Patient_ViewPatientlist patient_patientlist = response.body();
viewpatientdataLists = patient_patientlist.getData();
推荐阅读
- css - 图像按高度缩放
- javascript - WebRTC onnegotiationneeded
- javascript - JQuery 从一个值中设置数据名称
- javascript - 未选择的元素也被突出显示,为什么?
- c# - 在哪里/如何存储或生成客户端机密?
- django - 在 Django 应用程序中,在哪里以及如何设置 LANGUAGE_CODE?
- c++ - 三元运算符内的赋值
- javascript - 我在 Reactjs 中遇到 Node_modules 错误
- php - Windows 10 / PHP CLI:未从 ProgramFiles 文件夹加载 php.ini 文件
- java - Springboot 使用 IMAP 监听电子邮件