我有一个带有RecyclerView的片段,该片段根据我存储在sharedPrefs中的year属性显示数据。显示的数据仅与该年份相关,尽管我可以单击单击可打开包含所有年份列表的对话框的按钮来更改年份。单击任何行,都会更新sharedPrefs上的年份,单击“关闭”按钮上的年份,将关闭对话框,并且应该更新我的RecyclerView,但不是。在对话框中,我有一个desmissListener,而我的fragment活动实现了它。问题在于notifyDataSetChanged不会更新列表。
我试图重新加载所有片段,并且可以正常工作,但是我想避免删除和添加片段,而只是更新数据。
我的DisciplineAdapter
public class DisciplineAdapter extends
RecyclerView.Adapter<DisciplineAdapter.MyViewHolder> {
private List<Dados> dataset;
private Context mContext;
public DisciplineAdapter(Context mContext, List<Dados> dataset) {
this.mContext = mContext;
this.dataset = dataset;
}
public static class MyViewHolder extends RecyclerView.ViewHolder {
public TextView disciplineName;
public TextView disciplineContext;
public TextView disciplineAcronym;
public TextView disciplineCurricular;
public TextView disciplineSemester;
public MyViewHolder(@NonNull View containingView) {
super(containingView);
disciplineName = containingView.findViewById(R.id.tv_discipine_name);
disciplineContext = containingView.findViewById(R.id.tv_discipine_context);
disciplineAcronym = containingView.findViewById(R.id.tv_discipine_acronym);
disciplineCurricular = containingView.findViewById(R.id.tv_discipine_curricular_year);
disciplineSemester = containingView.findViewById(R.id.tv_discipine_semester);
}
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(mContext)
.inflate(R.layout.row_discipline_layout, viewGroup, false);
return new MyViewHolder(v);
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
holder.disciplineName.setText(dataset.get(position).getName());
holder.disciplineAcronym.setText(dataset.get(position).getAcronym());
holder.disciplineCurricular.setText(dataset.get(position).getCurricularYear()+"");
holder.disciplineSemester.setText(dataset.get(position).getSemesterQuarter()+"");
holder.disciplineContext.setText(dataset.get(position).getContext());
}
@Override
public int getItemCount() {
return this.dataset.size();
}
}
列出数据的我的HomeFragment。
public class HomeFragment extends Fragment {
private DisciplineAdapter disciplineAdapter;
private String year;
private List<Dados> datasetTemp;
private List<Dados> dataset;
public HomeFragment() {
}
private void refreshData() {
SharedPreferences sharedPreferences=this.getActivity().getSharedPreferences("teacherapp",Context.MODE_PRIVATE);
year=sharedPreferences.getString("year","");
dataset = datasetTemp.stream().filter(s -> year.equals(s.getCurrentYear())).collect(Collectors.toList());
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home, container, false);
refreshData();
Button btnYear = (Button) view.findViewById(R.id.btn_current_year);
btnYear.setText(year);
ImageView btnNewDiscipine = (ImageView) view.findViewById(R.id.btn_add_discipline);
final YearDialog yearDialog = new YearDialog();
RecyclerView myRecyclerView = view.findViewById(R.id.homeRecyclerView);
disciplineAdapter = new DisciplineAdapter(getContext(), dataset);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity());
myRecyclerView.setLayoutManager(layoutManager);
myRecyclerView.setItemAnimator(new DefaultItemAnimator());
myRecyclerView.setAdapter(disciplineAdapter);
yearDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
refreshData();
disciplineAdapter.notifyDataSetChanged();
}
});
btnYear.setOnClickListener(v -> {
yearDialog.show(getFragmentManager(), "Current year");
});
btnNewDiscipine.setOnClickListener(v -> {
Fragment disciplineFraggment = new DisciplineFragment();
FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
ft.replace(R.id.fragment_container, disciplineFraggment, "discipline");
ft.addToBackStack(null);
ft.commit();
});
return view;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//refreshData();
datasetTemp = new ArrayList<>();
datasetTemp.add(new Dados("A1", "Programação Web 18", "PW", "Mestrado de Eng. Informática", "2017/18", 1, 2));
datasetTemp.add(new Dados("A2", "Programação Web 19", "PW", "Mestrado de Eng. Informática", "2018/19", 1, 2));
datasetTemp.add(new Dados("A3", "Arquitetura de Software 17", "AS", "Mestrado de Eng. Informática", "2016/17", 2, 1));
datasetTemp.add(new Dados("A4", "Arquitetura de Software 18", "AS", "Mestrado de Eng. Informática", "2017/18", 2, 1));
datasetTemp.add(new Dados("A5", "Arquitetura de Software 19", "AS", "Mestrado de Eng. Informática", "2018/19", 2, 1));
datasetTemp.add(new Dados("A6", "Computacao movel 19", "CM", "Mestrado de Eng. Informática", "2018/19", 2, 2));
datasetTemp.add(new Dados("A7", "Computacao movel 18", "CM", "Mestrado de Eng. Informática", "2017/18", 2, 2));
}
}
我的YearDialog
public class YearDialog extends DialogFragment {
private DialogInterface.OnDismissListener onDismissListener;
// UI elements;
private Button btnDismiss;
private ListView listViewYear;
public void setOnDismissListener(DialogInterface.OnDismissListener onDismissListener) {
this.onDismissListener = onDismissListener;
}
@Override
public void onDismiss(@NonNull DialogInterface dialog) {
super.onDismiss(dialog);
if (onDismissListener != null) {
onDismissListener.onDismiss(dialog);
}
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.dialog_year, null);
List<String> currentYear = new ArrayList<String>();
currentYear.add("2016/17");
currentYear.add("2017/18");
currentYear.add("2018/19");
getDialog().setTitle("Current Year");
listViewYear = rootView.findViewById(R.id.lv_year);
btnDismiss = rootView.findViewById(R.id.btn_dialog_year_dismiss);
// Define new adapter and Assign adapter to ListView
ArrayAdapter<String> adapterYear = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, currentYear);
listViewYear.setAdapter(adapterYear);
// Listener for Button Dismissed
btnDismiss.setOnClickListener(v -> {
dismiss();
});
listViewYear.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String yearSelected = (String) listViewYear.getItemAtPosition(position);
btnDismiss.setText("CHANGE YEAR");
changeYear(yearSelected);
}
});
return rootView;
}
private void changeYear(String year) {
this.getActivity().getSharedPreferences("teacherapp", Context.MODE_PRIVATE)
.edit()
.putString("year", year)
.apply();
Toast.makeText(getContext(), year, Toast.LENGTH_LONG).show();
}
}
我希望我的列表重新加载新的数据集。