madhu527
madhu527

Reputation: 4782

How to refresh a listview in fragment page : Android

Just i want to update my list view after deleting an item from fragment. I am able to delete from my sq lite database but its not updating to the list view

My code in Base Adapter, on Delete Button item.

holder.deleItem.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                AlertDialog.Builder helpBuilder = new AlertDialog.Builder(
                        mcontext);
                helpBuilder.setTitle("Are you sure..you want to delete?");
                helpBuilder.setNegativeButton("Yes",
                        new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog,
                                    int which) {
                                db = new DataBaseHandler(mcontext);
                                db.deleteItem(mylist.get(position)
                                        .get("id"));
                                System.out.println(mylist.get(position)
                                        .get("id")
                                        + "ID :::::::::::::::::::");

                                Toast.makeText(
                                        mcontext,
                                        "DELETED SUCCESSFULLY"
                                                + " "
                                                + mylist.get(position).get(
                                                        "itemName"),
                                        Toast.LENGTH_LONG).show();




                            adapter.notifyDataSetChanged();




                            }
                        });

                helpBuilder.setNeutralButton("No",
                        new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog,
                                    int which) {
                                // Do nothing
                            }
                        });

                // Remember, create doesn't show the dialog
                AlertDialog helpDialog = helpBuilder.create();
                helpDialog.show();

            }

        });

Upvotes: 2

Views: 858

Answers (3)

Naveen Tamrakar
Naveen Tamrakar

Reputation: 3339

helpBuilder.setNegativeButton("Yes",
                    new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog,
                                int which) {
                            db = new DataBaseHandler(mcontext);
                            db.deleteItem(mylist.get(position)
                                    .get("id"));
                            System.out.println(mylist.get(position)
                                    .get("id")
                                    + "ID :::::::::::::::::::");
                                  notifyDataSetChanged();
                            Toast.makeText(
                                    mcontext,
                                    "DELETED SUCCESSFULLY"
                                            + " "
                                            + mylist.get(position).get(
                                                    "itemName"),
                                    Toast.LENGTH_LONG).show();

                        }
                    });

Upvotes: 0

Simulant
Simulant

Reputation: 20112

if your myList holds the data for your BaseAdapter you need to remove your Item from this list, too. Not only from your database. Afterwards notify your Adapter, that your DataSet has changed and it will update by itself.

mylist.remove(i);
notifyDataSetChanged();

Upvotes: 1

Murtaza Khursheed Hussain
Murtaza Khursheed Hussain

Reputation: 15336

notifyDatasetChanged to your adapter.

Upvotes: 1

Related Questions