I use SimpleCursorTreeAdapter to display the tree:
- categories
- subcategories
- products
- subcategories
The picture shows only groups and sub groups. Method
protected Cursor getChildrenCursor (Cursor groupCursor) {} is executed when you click on a category, I’m wondering if you can handle the click on 'under category' so that later you can somehow display the list of products in the same tree or in another activity, or in some other way?
public class AllProductsActivity extends Activity { ExpandableListView elvMain; DatabaseHandler db; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_market); db = new DatabaseHandler(this); Cursor categoryData = db.getCategoryData(); startManagingCursor(categoryData); String[] groupFrom = { DatabaseHandler.COLUMN_NAME }; int[] groupTo = { android.R.id.text1 }; String[] childFrom = { DatabaseHandler.COLUMN_NAME }; int[] childTo = { android.R.id.text1 }; // create adapter and a custom list SimpleCursorTreeAdapter sctAdapter = new MyAdapter(this, categoryData, android.R.layout.simple_expandable_list_item_1, groupFrom, groupTo, android.R.layout.simple_list_item_1, childFrom, childTo); elvMain = (ExpandableListView) findViewById(R.id.elvMain); elvMain.setAdapter(sctAdapter); } protected void onDestroy() { super.onDestroy(); db.close(); } class MyAdapter extends SimpleCursorTreeAdapter { public MyAdapter(AllProductsActivity context, Cursor cursor, int groupLayout, String[] groupFrom, int[] groupTo, int childLayout, String[] childFrom, int[] childTo) { super(context, cursor, groupLayout, groupFrom, groupTo, childLayout, childFrom, childTo); } protected Cursor getChildrenCursor(Cursor groupCursor) { // gettting cursot by elements for concrete group int idColumn = groupCursor.getColumnIndex(DatabaseHandler.COLUMN_ID); return db.getSubCategoryData(groupCursor.getInt(idColumn)); } } }
public class DatabaseHandler extends SQLiteOpenHelper { some code... //get category where idParent = 1 public Cursor getCategoryData() { return this.getReadableDatabase().query(TABLE_CATEGORY, null, COLUMN_ID_PARENT + " = " + 1, null, null, null, null); } //get subCategory where idParent = category.id public Cursor getSubCategoryData(long categoryID) { return this.getReadableDatabase().query(TABLE_CATEGORY, null, COLUMN_ID_PARENT + " = " + categoryID, null, null, null, null); } }
