0

I was wondering if somebody could further explain how to implement a delete button inside a listview that is populated from an SQLite database. I've read the responses to the following question that is essentially what I'm asking as well, but I don't understand it:

How can I implement a delete button in a ListView and delete from database?

In my custom row .xml file, I included a delete button that implements the method delete() onClick. It also includes an alert dialog, by the way. Here's the code that I have so far for my delete() method; whenever I try to use it, it never gets the right activtiy entry.

  public void delete(View view){

    final int position = listview.getPositionForView((View) view.getParent());
    String id = cursor.getString(cursor.getColumnIndex(SQLiteAdapter.KEY_ID));

    AlertDialog.Builder myDialog = new AlertDialog.Builder(MainActivity.this);

    myDialog.setTitle("Delete activity entry \"" + id + "\"?");
    myDialog.setPositiveButton("DELETE", new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface arg0, int arg1) {
            mySQLiteAdapter.delete(position);
            cursor.requery();
            }
        });

    myDialog.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface arg0, int arg1) {

            }
        });

    myDialog.show();
}

1 Answer 1

0

A solution is to implement a custom ArrayAdapter.

 public class MyArrayAdapter extends ArrayAdapter<YourObject>
    {

        private ArrayList<YourObject> items;

        public LiftArrayAdapter(Context context, int textViewResourceId, ArrayList<YourObject> items)
        {
            super(context, textViewResourceId, items);
            this.items = items;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent)
        {
            View v = convertView;
            if (v == null)
            {
                LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = vi.inflate(R.layout.your_view_xml, null);


}
        final YourObject obj = items.get(position);

        TextView lblLift = (TextView) v.findViewById(R.id.lbl_lift);
        ImageButton btnDelete = (ImageButton) v.findViewById(R.id.btn_delete);

        btnDelete .setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                //TODO delete 'obj' from database
            }
        });

        return v;
    }

}

Then, to bind a list of objects to your listview:

    List<YourObject> list = ...
    MyArrayAdapter myArrayAdapter = new MyArrayAdapter (.., .. ,  list);
    listView.setAdapter(myArrayAdapter);
Sign up to request clarification or add additional context in comments.

3 Comments

Will this work if I already am using another adapter? My listview is populated from an SQLite database and I used a simple cursor adapter to do so. To further explain my page, I have an edittext where users can input information which is saved into the SQLite database and displayed in the listview, but I want to be able to delete entries for the listview.
Yes, this custom adapter would have to replace your CursorAdapter. It would require you to create a List of your objects, and pass them as a argument to the MyArrayAdapter constructor.
I appreciate your help, but is there any way to do it without creating a new adapter? I'm a beginner to Android and I'm not sure how to populate a database without a cursor adapter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.