Pavan Kumar
Pavan Kumar

Reputation: 157

android: Data refresh in listview after deleting from database

I have an application which retrieves data from DB and displays it in a list view. I have a custom adapter for the same. So when I press the "delete" button, a delete button for each row in the list is displayed. If I press that, the particular row gets deleted in the DB and the same should be reflected in the listview also. The problem is, its not reflecting this change, I should close the app and reopen or move into some other activity and get back to see the updated results.

So my question is: where do I call the notifyDataSetChanged() method to get it updated instantly?

Here is my customadapter view:

public View getView(int position,  View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    MenuListItems menuListItems = menuList.get(position);


      if (convertView == null) {
           LayoutInflater inflater = (LayoutInflater) c
             .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
           convertView = inflater.inflate(R.layout.customlist, parent, false);

          }

      Button ck = (Button) convertView.findViewById(R.id.delbox);
      if(i){

          ck.setVisibility(View.VISIBLE);}
          else{               
              ck.setVisibility(View.GONE);        

      }


        ck.setTag(menuListItems.getSlno());
        ck.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                final Integer Index = Integer.parseInt((String) v.getTag());
                final DataHandler enter = new DataHandler(c);
                DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        switch (which){
                        case DialogInterface.BUTTON_POSITIVE:
                            //Yes button clicked
                            enter.open();
                            enter.delet(Index);
                            enter.close();
                            notifyDataSetChanged();
                            dialog.dismiss();

                            break;

                        case DialogInterface.BUTTON_NEGATIVE:
                            //No button clicked
                            dialog.dismiss();
                            break;
                        }
                    }
                };

                AlertDialog.Builder builder = new AlertDialog.Builder(c);
                builder.setMessage("Are you sure you want to Delete?").setPositiveButton("Yes", dialogClickListener)
                    .setNegativeButton("No", dialogClickListener).show();

            }
        });


            TextView id = (TextView) convertView.findViewById(R.id.tvhide);
            id.setText(menuListItems.getSlno());

          TextView title = (TextView) convertView.findViewById(R.id.tvtitle);
          title.setText(menuListItems.getTitle());
          TextView phone = (TextView) convertView.findViewById(R.id.tvpnumber);
          phone.setText(menuListItems.getPhone());
         // ck.setChecked(menuList.)
          notifyDataSetChanged();
          return convertView;
}

Upvotes: 2

Views: 18703

Answers (3)

SarcasticTA
SarcasticTA

Reputation: 1

A very basic solution to this would be restarting the activity.

In the Delete button click listener, after you delete the record, restart the activity.

Now it will fetch recent data!

    Intent myIntent = new Intent(context,MainActivityName.class);
    context.startActivity(myIntent);

Upvotes: -1

jbiral
jbiral

Reputation: 1459

You are storing the data that shows your ListView in a List somewhere (which you didn't show in the code your paste. It would be nice to update your question)

Then, when you remove your data from the dialog, you are deleting the real data, but not the one stored in your List, that you adapter uses to show the ListView. So you simply need to remove this data from this List and then call notifyDataSetChanged() and it will work.

Upvotes: 1

Jay
Jay

Reputation: 1478

You need to delete the data from the adapter of the listview via adapter.remove(adapter.getItem(position)); and then call notifyDataSetChanged() on adapter.

Upvotes: 7

Related Questions