I have a sqlite database in my application. I want to make an expandable list view with that.
I am fixed with the approach I should take for that.
Tried a lot to find a tutorial for the same, but could not find a single one, where one is populating the Expandable list with the local database.
There is this one tutorial in the android site where they are filling the expandable list with the Contact detail in the phone. They are doing this from the content provider ExpandableList2.java
So my question is should I also create a content provider for my database which is inside my own application ?
Is this the only approach or is there any other better one?
I had created a project to try out expandablelistview and database, hope this helps
final class ExpAdapter extends CursorTreeAdapter { LayoutInflater mInflator; public ExpAdapter(Cursor cursor, Context context) { super(cursor, context); mInflator = LayoutInflater.from(context); } @Override protected void bindChildView(View view, Context context, Cursor cursor, boolean isLastChild) { TextView tvChild = (TextView) view.findViewById(android.R.id.text1); tvChild.setText(cursor.getString(cursor .getColumnIndex(DBHelper.COL_TABLE_CHILD_NAME))); } @Override protected void bindGroupView(View view, Context context, Cursor cursor, boolean isExpanded) { TextView tvGrp = (TextView) view.findViewById(android.R.id.text1); tvGrp.setText(cursor.getString(cursor .getColumnIndex(DBHelper.COL_TABLE_MAIN_NAME))); } @Override protected Cursor getChildrenCursor(Cursor groupCursor) { int groupId = groupCursor.getInt(groupCursor .getColumnIndex(DBHelper.COL_ID)); return aDBHelper.getChildCursor(groupId); } @Override protected View newChildView(Context context, Cursor cursor, boolean isLastChild, ViewGroup parent) { View mView = mInflator.inflate( android.R.layout.simple_expandable_list_item_1, null); TextView tvChild = (TextView) mView .findViewById(android.R.id.text1); tvChild.setText(cursor.getString(cursor .getColumnIndex(DBHelper.COL_TABLE_CHILD_NAME))); return mView; } @Override protected View newGroupView(Context context, Cursor cursor, boolean isExpanded, ViewGroup parent) { View mView = mInflator.inflate( android.R.layout.simple_expandable_list_item_1, null); TextView tvGrp = (TextView) mView.findViewById(android.R.id.text1); tvGrp.setText(cursor.getString(cursor .getColumnIndex(DBHelper.COL_TABLE_MAIN_NAME))); return mView; } }
You don't have to create a content provider for that.
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