I have an activity that extends ExpandableListActivity. I use SimpleCursorTreeAdapter to fill ExpandableListView. My layout contains list view and empty view. On app start ExpandableListActivity automatically chooses the right view to display.
My steps:
Then I restart app:
Questions:
Tested on SDKs: 1.5r3, 1.6r1
Code:
public class MainActivity extends ExpandableListActivity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        dbHelper = new DBOpenHelper(this);
        rubricsDbAdapter = new RubricsDBAdapter(dbHelper);
        rubricsDbAdapter.open();
        itemsDbAdapter = new ItemsDBAdapter(dbHelper);
        itemsDbAdapter.open();
        rubricsCursor = rubricsDbAdapter.getAllItemsCursor();
        startManagingCursor(rubricsCursor);
        // Cache the ID column index
        rubricIdColumnIndex = rubricsCursor.getColumnIndexOrThrow(RubricsDBAdapter.KEY_ID);
        // Set up our adapter
        mAdapter = new MyExpandableListAdapter(rubricsCursor,
                this,
                android.R.layout.simple_expandable_list_item_1,
                android.R.layout.simple_expandable_list_item_1,
                new String[] {RubricsDBAdapter.KEY_NAME},
                new int[] {android.R.id.text1},
                new String[] {ItemsDBAdapter.KEY_NAME}, 
                new int[] {android.R.id.text1});
        setListAdapter(mAdapter);
    }
    public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
        Intent i = new Intent(this, ItemViewActivity.class);
        i.putExtra(ItemsDBAdapter.KEY_ID, id);
        startActivity(i);
        return super.onChildClick(parent, v, groupPosition, childPosition, id);
    }
    private void updateMyData() {
        int i;
        int k;
        long rubricId;
        for (i = 1; i <= 5; i++) {
            rubricId = rubricsDbAdapter.insert("rubric " + i);
            for (k = 1; k <= 5; k++) {
                itemsDbAdapter.insert("item " + i + "-" + k, rubricId);
            }
        }
        mAdapter.notifyDataSetChanged();
    }
    private void deleteMyData() {
        rubricsDbAdapter.deleteAll();
        itemsDbAdapter.deleteAll();
        mAdapter.notifyDataSetChanged();
    }
    public class MyExpandableListAdapter extends SimpleCursorTreeAdapter 
    {
        public MyExpandableListAdapter(Cursor cursor, Context context, int groupLayout,
                int childLayout, String[] groupFrom, int[] groupTo, String[] childrenFrom,
                int[] childrenTo) {
            super(context, cursor, groupLayout, groupFrom, groupTo, childLayout, childrenFrom,
                    childrenTo);
        }
        @Override
        protected Cursor getChildrenCursor(Cursor notebookCursor) {
            // Given the group, we return a cursor for all the children within that group 
            long id = notebookCursor.getLong(rubricIdColumnIndex);
            Cursor itemsCursor = itemsDbAdapter.getRubricItemsCursor(id);
            startManagingCursor(itemsCursor);
            return itemsCursor;
        }
    }
}
                So, the answer is:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With