I use SimpleCursorTreeAdapter to display the tree:

  • categories
    • subcategories
      • products

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?

enter image description here

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); } 

}

    1 answer 1

    Can. Using this method setOnChildClickListener which is in ExpandableListView

    Add method to MyAdapter

     public void bindView(View view, Context context, Cursor cursor) { super.bindView(view, context, cursor); String someData = cursor.getString(COLUMN_INDEX_WITH_DATA_URI); view.setTag("value", someData); } 

    then in the setOnChildClickListener handler from the view parameter, pull out the necessary data using view.getTag("value");

    If you need to build a tree with more nesting, you can look at this library https://github.com/bmelnychuk/AndroidTreeView

    • those. There are no standard methods such as .setOnClickListener? This problem can be solved only by the library? - Kirill Stoianov
    • The setOnChildClickListener in ExpandableListView gives me only a position number, but does not give a cursor with data about an object that was selected, as in the case of a protected Cursor getChildrenCursor (Cursor groupCursor). I need to know what object was selected, because based on this, the further logic of the application will be determined. - Kirill Stoianov
    • one
      @KirillStoianov, but what if you try to override the bindChildView in SimpleCursorTreeAdapter and add the necessary information to the View (if it’s a bit, say an ID or something else you need) using the setTag () method, and already in setOnChildClickListener you can simply take this data from View using the method getTag () - Eugene Kuzmenko
    • @EugeneKuzmenko Suitable, it is enough for me to know the name or id of the category that was selected, but can I give an example of how to implement it in my code? - Kirill Stoianov
    • @KirillStoianov, updated the answer - Eugene Kuzmenko