2
votes

I am kind of new to android. For the life of me I can not figure out how to delete an item from a ListView from within my Adapter. Each row in the list view has an edit and delete option. I populate a dialog asking user to confirm deletion, after confirming, i'd like the ListView to remove that Item. Any help would be greatly appreciated.

Contacts.java

public class Contacts extends ActionBarActivity {

    private Button mButton;
    private ContactsAdapter mAdapter;
    private ListView mListView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.emergency_contacts);

        mAdapter = new ContactsAdapter(Contacts.this,new ArrayList<SingleContactInfo>());
        mListView = (ListView) findViewById(R.id.listView);
        mListView.setAdapter(mAdapter);
        mButton = (Button) findViewById(R.id.addContactButton);


        updateData();

        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent goToAddContacts = new Intent(getApplicationContext(), AddContactInfo.class);
                startActivity(goToAddContacts);
            }
        });

    }

    public void updateData(){
        ParseQuery<SingleContactInfo> query = ParseQuery.getQuery(SingleContactInfo.class);
        query.whereEqualTo("user", ParseUser.getCurrentUser());
        query.setCachePolicy(ParseQuery.CachePolicy.CACHE_THEN_NETWORK);
        query.findInBackground(new FindCallback<SingleContactInfo>() {
            @Override
            public void done(List<SingleContactInfo> contacts, ParseException error) {
                if(contacts != null){
                    mAdapter.clear();
                    for (int i = 0; i < contacts.size(); i++) {
                        mAdapter.add(contacts.get(i));
                        mAdapter.notifyDataSetChanged();
                    }
                }
            }
        });
    }

ContactsAdapter.java

public class ContactsAdapter extends  ArrayAdapter<SingleContactInfo> {
    private final Context mContext;
    private List<SingleContactInfo> mContacts;
    public TextView mContactName;
    public TextView mNumber;
    public TextView mEdit;
    public TextView mDelete;

    public ContactsAdapter(Context context, List<SingleContactInfo> objects) {
        super(context, R.layout.add_emergency_contacts_two, objects);
        this.mContext = context;
        this.mContacts = objects;
    }


    public View getView(final int position, View convertView, final ViewGroup parent) {
        if (convertView == null) {
            LayoutInflater mLayoutInflater = LayoutInflater.from(mContext);
            convertView = mLayoutInflater.inflate(R.layout.add_emergency_contacts_two, null);
        }

        final SingleContactInfo contact = mContacts.get(position);

        mContactName = (TextView) convertView.findViewById(R.id.emergency_contact_name);
        mNumber = (TextView) convertView.findViewById(R.id.emergency_contact_number);
        mEdit = (TextView) convertView.findViewById(R.id.edit_textView);
        mDelete = (TextView) convertView.findViewById(R.id.delete_textView);



        mDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                deleteContactDialog(contact, mContext, position);
                Toast.makeText(getContext(), "Deleted", Toast.LENGTH_LONG).show();
            }
        });

        mContactName.setText(contact.getName());
        mNumber.setText(contact.getNumber());



        return convertView;
    }
//TODO Look below here
    public void deleteContactDialog(final SingleContactInfo contact, Context context, final int position) {
        AlertDialog.Builder confirmDelete = new AlertDialog.Builder(context);
        confirmDelete.setMessage("Are You Sure You Want to Delete Contact Person")
                .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        try {
                        } catch (ParseException e) {
                            e.printStackTrace();
                        }
                    }
                })
                .setNegativeButton("No", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });

        AlertDialog dialog = confirmDelete.create();

        dialog.show();

    }



}
2

2 Answers

1
votes

You just need to remove the item at the desired position from the ArrayList of contacts, and then notify the dataset:

public void onClick(DialogInterface dialog, int which) {
    try {
        mContacts.remove(position);
        notifyDataSetChanged();
    } 
    catch (ParseException e) {
        e.printStackTrace();
    }
}

Also, it's enough to call mAdapter.notifyDataSetChanged(); from outside your for cycle.

1
votes
 public void deleteContactDialog(final SingleContactInfo contact, Context context, final int position) {
    AlertDialog.Builder confirmDelete = new AlertDialog.Builder(context);
    confirmDelete.setMessage("Are You Sure You Want to Delete Contact Person")
            .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                   //Just add these two lines and you should be good.

                   mContacts.remove(position);
                   ContactsAdapter.this.notifyDataSetChanged();
                }
            })
            .setNegativeButton("No", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                }
            });

    AlertDialog dialog = confirmDelete.create();

    dialog.show();

}