4

I found only one similiar question and it didn't help me. I hope you can help me, because I'm stuck.

This is my custom adapter (only the relevant part):

MyExpandableListAdapter:

public class MyExpandableListAdapter extends BaseExpandableListAdapter {

    //...

    @Override
    public View getChildView(int groupPosition, int childPosition,
            boolean isLastChild, View view, ViewGroup parent) {     
        ChildInfo childInfo = (ChildInfo) getChild(groupPosition, childPosition);
        if (view == null) {
            LayoutInflater infalInflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = infalInflater.inflate(R.layout.prisustva_detaljno_child_row,
                    null);
        }

        Button delete = (Button) view.findViewById(R.id.delete);
        delete.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                RelativeLayout Rl = (RelativeLayout) v.getParent();
                TextView id = (TextView) Rl.findViewById(R.id.id_prisustvo);
                PrisustvaAdapter deleteByID = new PrisustvaAdapter(context);

                deleteByID.openToWrite();
                deleteByID.openToRead();
                deleteByID.deleteID(id .getText().toString());
                deleteByID.close();

                Toast.makeText(context, "Successfully deleted!",
                        Toast.LENGTH_LONG).show();
            }
        });

        // ...
        return view;
    }

// ...
}

Like you can see I have a button in every child row of my ExpandableListView. When I click on that button it triggers the deletion process in my database, as you can also see I have to do the deletion from within my adapter, because only there I can create the click event for the button inside the child row. I want to know how can I refresh my ExpandableListView when I remove the data from database? Do I have to do it from inside the adapter? If not, what’s the alternative?

P.S: If you guys need more code, please let me know.

Aksiom
  • 1,565
  • 4
  • 25
  • 39

1 Answers1

8

I can't be sure without seeing the rest of your code (how you instantiate your BaseExpandableAdapter) but calling notifyDatasetChanged() at the end of your onClick() method should do the trick.

znat
  • 13,144
  • 17
  • 71
  • 106
  • I tried that already but it didn't work. Shouldn't I call `notifyDatasetChanged()` after i delete the child row. Because I'm deleteing it only from my database, not from the list too. When I worked with an ListView i had this methods `adapter.remove()` and `adapter.add()` after that i called `notifyDatasetChanged()` and it worked like a charm, but i can't figure it out with ExpandableListView. – Aksiom Mar 12 '13 at 01:14
  • 1
    If your adapter is based on a list (and not on the db), you must remove it from the list and then call notifyDataSetChanged(). – znat Mar 12 '13 at 01:21
  • 2
    I managed it after I found out how to remove the specific item from my list. That was my only problem, and it was so simple. Thank you for your hints, helped me a lot. P.S. I had an ArrayList of ArrayLists that I linked with LinkedHashMap. Didnt know at first how to get the specific item from there. But at the end it was simple. – Aksiom Mar 12 '13 at 01:33