i want to delete a listview item on long press on that item . after deleting item i need to restart activity to see changes but i want to see changes instantly
i already tried notifyDataSetChanged(); but it didn't work i am taking data from sqlite database and put it in hashmaps and sending it to custom adapter on long press one row is deleted from database but no changes take place in listview.
@Override
public View getView( final int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
ImageView imageView = view.findViewById(R.id.detailsimage);
CardView cardView = view.findViewById(R.id.detailcardView);
TextView textmo =view.findViewById(R.id.detailsmo);
TextView texttu =view.findViewById(R.id.detailstu);
TextView textwe =view.findViewById(R.id.detailswe);
TextView textth =view.findViewById(R.id.detailsthu);
TextView textfr =view.findViewById(R.id.detailsfri);
TextView textsa =view.findViewById(R.id.detailssat);
TextView textsu =view.findViewById(R.id.detailssun);
final databasehandler db = new databasehandler(context);
cardView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
db.deleteTitle(position+1);
Toast.makeText(context,Integer.toString(position),Toast.LENGTH_SHORT).show();
notifyDataSetChanged();
return true;
}
});
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
HashMap<String, String> hashmap= arrayList.get(position);
String string= hashmap.get("repeat");
string = Objects.requireNonNull(string).trim();
Toast.makeText(context,string,Toast.LENGTH_SHORT).show();
}
});
HashMap<String, String> hashmap= arrayList.get(position);
String string= Objects.requireNonNull(hashmap.get("repeat")).trim();
String[] separated = Objects.requireNonNull(string).split("");
}
}}
return view;
}
}
You shouldn't call notifyDataSetChanged
it will refresh the entire list, which will affect the performance
cardView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
db.deleteTitle(position+1);
yourArrayList.remove(position);
Toast.makeText(context,Integer.toString(position),Toast.LENGTH_SHORT).show();
notifyItemRemoved(position);
return true;
}
});