I already made the update and delete button inside item_report.xml, and the function in my Adapter.class. I put my recyclerview inside fragment which i made 2 tab in tablayout inside MainActivity, but when i pressed Delete or Edit/Update it's not working.
But the toast "This should be working" i put inside Update worked but not with alertdialog for update, and AlertDialog in Delete appeared & worked. I think something wrong with my Data Update & Delete but idk where cuz i thought it's no error in Run Log.
Here is my Edit & Delete in my Adapter.class:
@Override
public void onBindViewHolder(@NonNull Holder holder, @SuppressLint("RecyclerView") int position) {
ReportDraft reportDraft = arrayList.get(position);
final String id = reportDraft.getId();
final String token = reportDraft.getToken();
final String email = reportDraft.getEmail();
final String judul = reportDraft.getJudul();
final String kategori = reportDraft.getKategori();
final String tanggal = reportDraft.getTanggal();
final String lokasi = reportDraft.getLokasi();
final String deskripsi= reportDraft.getDeskripsi();
final String img1 = reportDraft.getImage1();
final String img2 = reportDraft.getImage2();
final String img3 = reportDraft.getImage3();
final String img4 = reportDraft.getImage4();
final String img5 = reportDraft.getImage5();
final String vid = reportDraft.getVideo();
final String longitude= reportDraft.getLongitude();
final String latitude = reportDraft.getLatitude();
holder.judul.setText(judul);
holder.kategori.setText(kategori);
holder.tanggal.setText(tanggal);
holder.lokasi.setText(lokasi);
holder.deskripsi.setText(deskripsi);
if (holder.imagev1==null){
holder.imagev1.setImageURI(Uri.parse(img1));
}else if (Uri.parse(img1)==null){
holder.imagev1.setImageURI(Uri.parse(img2));
} else if (Uri.parse(img2)==null){
holder.imagev1.setImageURI(Uri.parse(img3));
} else if (Uri.parse(img3)==null){
holder.imagev1.setImageURI(Uri.parse(img4));
} else if (Uri.parse(img4)==null){
holder.imagev1.setImageURI(Uri.parse(img5));
} else if (Uri.parse(img1)==null && Uri.parse(img2)==null && Uri.parse(img3)==null && Uri.parse(img4)==null && Uri.parse(img5)==null){
holder.imagev1.setImageResource(R.drawable.ic_image);
}
holder.editbtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Toast.makeText(view.getContext(), "This should be working", Toast.LENGTH_SHORT).show();
editDialog(
"" position,
"" id,
"" token,
"" email,
"" judul,
"" kategori,
"" tanggal,
"" lokasi,
"" deskripsi,
"" img1,
"" img2,
"" img3,
"" img4,
"" img5,
"" vid,
"" longitude,
"" latitude
);
}
});
holder.deletebtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
deleteDialog(
"" id
);
}
});
}
private void deleteDialog(final String id){
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Delete");
builder.setMessage("Are you sure to delete it?");
builder.setCancelable(false);
builder.setIcon(R.drawable.ic_delete);
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
databaseHelper.deleteInfo(id);
//FragmentDraft frd = (FragmentDraft) ;
((MainActivity)context).onResume();
Toast.makeText(context, "Aw", Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
builder.create().show();
}
private void editDialog(String position, String id, String token, String email, String judul, String kategori, String tanggal, String lokasi, String deskripsi, String img1, String img2, String img3, String img4, String img5, String vid, String longitude, String latitude) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Update");
builder.setMessage("Update data?");
builder.setCancelable(false);
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Intent intent = new Intent(context, EditReportActivity.class);
intent.putExtra("ID", id);
intent.putExtra("TOKEN", token);
intent.putExtra("EMAIL", email);
intent.putExtra("JUDUL", judul);
intent.putExtra("KATEGORI", kategori);
intent.putExtra("TANGGAL", tanggal);
intent.putExtra("LOKASI", lokasi);
intent.putExtra("DESKRIPSI", deskripsi);
intent.putExtra("IMAGE1", img1);
intent.putExtra("IMAGE2", img2);
intent.putExtra("IMAGE3", img3);
intent.putExtra("IMAGE4", img4);
intent.putExtra("IMAGE5", img5);
intent.putExtra("VIDEO", vid);
intent.putExtra("LONGITUDE", longitude);
intent.putExtra("LATITUDE", latitude);
intent.putExtra("editMode", true);
context.startActivity(intent);
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.cancel();
}
});
}
Here is my FragmentDraft.class:
public class FragmentDraft extends Fragment {
View v;
private RecyclerView dRecyclerView;
private DatabaseHelper databaseHelper;
private RecyclerView recyclerView;
String email, token;
public FragmentDraft() {
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
v = inflater.inflate(R.layout.fragment_draft, container,false);
dRecyclerView = v.findViewById(R.id.recyclerDraft);
Adapter adapter = new Adapter(getContext(), databaseHelper.getAllData(ReportSQL.R_TANGGAL " DESC"));
dRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
dRecyclerView.setAdapter(adapter);
return v;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
}
is this bcuz my fragment or main activity or what? cuz i put recyclerview inside fragment, the fragemnt is inside tablayout in my MainActivity.
UPDATE
The delete button success to delete but the list is not changed unless i open other activity and go back
it SOLVED, sorry i forgot to set builder.create().show()
. Thank you
CodePudding user response:
Your deletion, while removing the item from your database, is not removing the item from the Adapter's data source (your arrayList) and also not notifying the Adapter that there was a change in data.
Pass the selected item position to your deleteDialog method:
holder.deletebtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
deleteDialog(
position,
"" id
);
}
});
Add a deletion of the item from your data source:
private void deleteDialog(final int position, final String id){
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Delete");
builder.setMessage("Are you sure to delete it?");
builder.setCancelable(false);
builder.setIcon(R.drawable.ic_delete);
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
databaseHelper.deleteInfo(id);
arrayList.remove(position);
notifyDatasetChanged();
Toast.makeText(context, "Aw", Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
builder.create().show();
}
The added lines are:
arrayList.remove(position);
which removes the item from the Adapter's data source
And:
notifyDatasetChanged();
Which tells the Adapter that the data was changed and it needs to reload.
As per your edit dialog not showing, it looks like you are missing the show() call:
Add this at the end of editDialog method:
builder.show()