Rashid Hussain
Rashid Hussain

Reputation: 11

how to refresh activity from adapter when a record is deleted?

i am using android studio in listview i have added adapter. when user click delete button from listview item then i want to refresh activity.

adapter delete button definition

    deletebtn =convertView.findViewById(R.id.delete_btn);

    deletebtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

        databaseAccess.deleteFavorite(favoriteModel.getWid());

        }
    });

i have tried this code but not working

   deletebtn =convertView.findViewById(R.id.delete_btn);

    deletebtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

        databaseAccess.deleteFavorite(favoriteModel.getWid());


            Intent intent1=new Intent(context,FavoriteNameslist.class);

            context.startActivity(intent1);
            ((Activity)context).finish();

        }
    });

i am new to Android - please help
i want when user click on delete button then refresh activity

Upvotes: 1

Views: 4984

Answers (3)

Android
Android

Reputation: 1434

Try below lines of code on Deleting Row to Update adapter

 arrayList.remove(position);
 mAdapter.notifyItemRemoved(position);

Upvotes: 0

rohiththammaiah
rohiththammaiah

Reputation: 153

If you are removing a single item from the view, you need to call

yourAdapter.notifyItemChanged(position) //Just pass the position to this. This also has a default animation. Try this in your adapter class instead of refreshing the layout.

Upvotes: 0

Bunny
Bunny

Reputation: 1064

There is no need to call your Activity to referesh listview you can use

notifyDataSetChanged()

remove the item and set notifyDataSetChanged()

yourList.remove([INDEX]);
arrayAdapter.notifyDataSetChanged();

Upvotes: 0

Related Questions