Android - Listview delete item and Refresh
Asked Answered
L

9

9

I am trying to implement ListView with Delete functionality to delete item from the listview. I am successful to delete but failed to refresh the listview after deletetion of an item from the database.

Actually, Click on listitem, i am displaying AlertBox for "Delete" and "Cancel" action, on clicking "Delete", item should be removed from the database and as well as from the listview and listview should be refreshed. I have also used notifyDataSetChanged() method.

lview = (ListView) findViewById(R.id.lview);
adapter = new ListView_CustomAdapter(this, listitemDisplay);
lview.setAdapter(adapter);

lview.setOnItemClickListener(new OnItemClickListener() 
{
     @Override
     public void onItemClick(AdapterView<?> a, View v, int position, long id)
     {
        Show_Alert_box(v.getContext(),"Please select action.",position);
     }
});

and the code for Show_Alert_box:

 public void Show_Alert_box(Context context, String message,int position)
     {
         final int pos = position;

         final AlertDialog alertDialog = new  AlertDialog.Builder(context).create();
            alertDialog.setTitle(getString(R.string.app_name_for_alert_Dialog));
            alertDialog.setButton("Delete", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {

                    try
                    {
                        db.open();
                                 String[] whereArgs={String.valueOf(pkID)};
        return db.delete(DATABASE_TABLE_4,"pk_pkID == ?",whereArgs);    
                        adapter.notifyDataSetChanged();
                        db.close();
                    }
                    catch(Exception e)
                    {

                    }
            } }); 
            alertDialog.setButton2("Cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    alertDialog.dismiss();
            } }); 

            alertDialog.setMessage(message);
            alertDialog.show();
     }
Lixiviate answered 11/1, 2011 at 10:56 Comment(2)
What kind of adapter you're using? If you're using CursorAdapter what you described should work.Cancel
Mods don't delete questions because you're not happy with the answers, sorry.Costin
L
9

Call that Activity once again Using Intent

Larder answered 11/1, 2011 at 13:17 Comment(2)
At the end, i have already implemented the same as you mentioned. ThanxLixiviate
so if you call activity once again as per the answer will the activity "scroll up" everytime you delete an item even if you scrolled down to delete an item at the bottom?Amundsen
F
11

Does it remove it from your list adapter? If not that would be the reason the notifyDataSetChanged() won't do you much good.

Actually looking at your code again i can only find that you're removing it from your database and not the adapter itself.

edit (to answer comment): Well that's hard to do without your ListView_CustomAdapter class. The problem is, in this adapter there's a data set (the one you put in the constructor (listitemDisplay)) which needs to be updated as well. Only then the notifyDataSetChanged() will work.

Fid answered 11/1, 2011 at 10:59 Comment(2)
pls show me a way by which i can implement Delete functionalityLixiviate
Actually I have designed custom listview adapter and thereby when i try to access "remove" method for the adapter, it is not appearing in the menu pop-up.Lixiviate
L
9

Call that Activity once again Using Intent

Larder answered 11/1, 2011 at 13:17 Comment(2)
At the end, i have already implemented the same as you mentioned. ThanxLixiviate
so if you call activity once again as per the answer will the activity "scroll up" everytime you delete an item even if you scrolled down to delete an item at the bottom?Amundsen
D
6

I'm guessing that using

getActivity().recreate();

instead of restarting the activity via a new Intent is better because using a new Intent will only stop the current activity and not destroy it.

Anyway, it works.

Deane answered 21/1, 2013 at 3:22 Comment(0)
J
5

if you have the cursor, call requery() before calling notifyDataChanged()

Jestinejesting answered 11/1, 2011 at 11:18 Comment(0)
C
4

I did something like this in my adapter:

((Activity)cxt).finish();
Intent intent = new Intent(cxt, YourActivity.class);
cxt.startActivity(intent);

This ends the activity and then starts the same one again.

Cameo answered 1/8, 2013 at 13:21 Comment(0)
P
3

I think instead of calling the activity again, you should set the adapter to the listview on the alertBox delete option after getting the updated data from the database and putting into listitemDisplay list like this.

alertDialog.setButton("Delete", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                try
                {
                    db.open();
                             String[] whereArgs={String.valueOf(pkID)};
                    return db.delete(DATABASE_TABLE_4,"pk_pkID == ?",whereArgs); 
                    listitemDisplay = db.getItemFromDB();
                    adapter = new ListView_CustomAdapter(this, listitemDisplay);
                    lview.setAdapter(adapter);
                    db.close();
                }
                catch(Exception e)
                {

                }
        } }); 

This will refresh the listView

Packet answered 6/1, 2014 at 5:10 Comment(0)
C
3

I have the solution:

If you want to delete a row from a list view clicking on the DELETE button of each of that row do this. Here you have an example of a custom adapter class with a name and a delete button. Every time you press the button the row is deleted

public class UserCustomAdapter extends ArrayAdapter<User>{ 

Context context;
int layoutResourceId;
ArrayList<User> data = new ArrayList<User>();

public UserCustomAdapter(Context context, int layoutResourceId,ArrayList<User> data) {
    super(context, layoutResourceId, data);
    this.layoutResourceId = layoutResourceId;
    this.context = context;
    this.data = data;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    View row = convertView;
    UserHolder holder = null;


    if (row == null) {
        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        row = inflater.inflate(layoutResourceId, parent, false);

        holder = new UserHolder();
        holder.textName = (TextView) row.findViewById(R.id.textView1);
        holder.btnDelete = (Button) row.findViewById(R.id.button2);
        row.setTag(holder);
    } else {
        holder = (UserHolder) row.getTag();

    }

    User user = data.get(position);


    holder.btnDelete.setTag(position);
    holder.textName.setText(user.getName());



    holder.btnDelete.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            String pos = v.getTag().toString();
            int _posicion = Integer.parseInt(pos);
            data.remove(_posicion);
            notifyDataSetChanged();

        }
    });

    return row;


}

static class UserHolder {
    TextView textName;
    Button btnDelete;
}
}
Cade answered 17/11, 2014 at 14:52 Comment(0)
N
0

Try calling refreshDrawableState to tell the list to redraw.

N answered 11/1, 2013 at 3:7 Comment(0)
F
0

Make a new function outside your onCreate block {something like... getdata()} and inside that insert and get all your data and set to the adapter.
Then call the function again in your onResume() block. So whenever you will delete the data from the list it will reflect immediately.

Flann answered 12/5, 2016 at 5:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.