I am giving data to adapter in the fragment:
List<Profile> profileList = profileService.getAll();
RecyclerView recyclerView = rootView.findViewById(R.id.RCViewProfile);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
adapter = new ProfileRCViewAdapter(getActivity(), profileList);
adapter.setClickListener(this::onItemClick);
recyclerView.setAdapter(adapter);
and i am updating this data with alertDialog in recyclerView:
holder.editBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Profile selected = profiles.get(holder.getAdapterPosition());
LayoutInflater inflater = ((AppCompatActivity) context).getLayoutInflater();
View dialog = inflater.inflate(R.layout.edit_dialog, null);
AlertDialog.Builder builder = new AlertDialog.Builder(context);
ImageView deleteButton = dialog.findViewById(R.id.updateCancelBtn);
ImageView saveButton = dialog.findViewById(R.id.updateSaveBtn);
TextInputEditText profileNameText = dialog.findViewById(R.id.editDialogProfileName);
profileNameText.setText(selected.getProfileName());
builder.setView(dialog);
saveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Profile temp = new Profile();
temp.setId(selected.getId());
temp.setProfileName(profileNameText.getText().toString());
AppDatabase appDatabase = AppDatabase.getAppDatabase(context);
ProfileService profileService = new ProfileService(appDatabase);
profileService.update(temp);
Fragment1.refresh(context);
Toast.makeText(context,"UPDATED",Toast.LENGTH_SHORT).show();
}
});
But for updating data i must change fragment page, otherwise data is not updating. I tried to write code "adapter.notifyDataSetChanged()" in fragment and i called it but it doesn't work.
public static void refresh(Context context) {
adapter.notifyDataSetChanged();
}