Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7970477
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:24:20+00:00 2026-06-04T07:24:20+00:00

I have two tables in my database – rooms and devices. Each room can

  • 0

I have two tables in my database – rooms and devices. Each room can have many devices. I want to make an expandable listview with room name as group and devices name and state as children (on or off).

Can any one give me a simple idea because I am kind of new with Android and all tutorials are hard to make

This is my database:

public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table if not exists rooms (
                                    id_room integer primary key autoincrement, "
                + "name_room text not null" 
                + ");");    
        db.execSQL("create table if not exists devices (
                                    id_device integer primary key autoincrement, "
                + "name_device text not null," 
                + "state_device text not null,"
                + "id_room references rooms"
                + ");");
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T07:24:21+00:00Added an answer on June 4, 2026 at 7:24 am

    Following is a very simple, no frills expandable list implementation.

    You need two methods in your db helper class to gather the cursors you will need.

    public Cursor fetchGroup() {
        String query = "SELECT * FROM rooms"
        return mDb.rawQuery(query, null);
    }
    
    public Cursor fetchChildren(String room) {
        String query = "SELECT * FROM devices WHERE id_room = '" + room + "'";
        return mDb.rawQuery(query, null);
    }
    

    Then you need to set up your adapter (in your activity):

    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 groupCursor) {
            Cursor childCursor = mDbHelper.fetchChildren(groupCursor.getString(groupCursor.getColumnIndex("id_room"));            
            getActivity().startManagingCursor(childCursor);
            childCursor.moveToFirst();
            return childCursor;
        }
    }
    

    And finally call the adapter and set it to your list (in your activity):

    private void fillData() {
        mGroupsCursor = mDbHelper.fetchGroup();
        getActivity().startManagingCursor(mGroupsCursor);
        mGroupsCursor.moveToFirst();
    
        ExpandableListView elv = (ExpandableListView) getActivity().findViewById(android.R.id.list);
    
        mAdapter = new MyExpandableListAdapter(mGroupsCursor, getActivity(),
            R.layout.rowlayout_expgroup,                     // Your row layout for a group
            R.layout.rowlayout_itemlist_exp,                 // Your row layout for a child
            new String[] { "id_room" },                      // Field(s) to use from group cursor
            new int[] { android.R.id.room },                 // Widget ids to put group data into
            new String[] { "name_device", "state_device" },  // Field(s) to use from child cursors
            new int[] { R.id.device, R.id.state });          // Widget ids to put child data into
    
            lv.setAdapter(mAdapter);                         // set the list adapter.
        }
    }
    

    Hope this helps!

    EDIT

    It should all come together like so:

    public class List_Exp extends Activity {
        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
            mDbHelper = new YourDB(getActivity());
            mDbHelper.open();
            fillData();
        }
    
        private void fillData() { 
            // set list adapter here
        }
    
        public class MyExpandableListAdapter extends SimpleCursorTreeAdapter {
            // Your adapter
        }
    }
    

    EDIT 2

    To catch clicks, set listeners in you activity:

    lv.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
        @Override
        public boolean onChildClick(ExpandableListView parent, View v,
            int groupPosition, int childPosition, long id) {
            // Your child click code here
            return true;
        }
    });
    
    lv.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
        @Override
        public boolean onGroupClick(ExpandableListView parent, View v,
            int groupPosition, int groupPosition, long id) {
            // Your group click code here
            return true;
        }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables in my Database.This can increase later on.I want to add
I have two related tables in my database: Page and Tag. One Page can
I have two database tables that have a many to many relationship: People and
I have two tables in my database which have a one-to-one relationship. I want
I have two tables in my database: page and link. In each one I
I have a two same tables.Database mysql. How can I compare two tables? Table1,Table2
I have two tables in my MySQL database (table1 and table 2). I want
I have two database tables with a one-to-many relationship. Is it possible to select
I have two database tables, subscription and transaction , where one subscription can have
I have two database tables (lets call them A and B) with many-to-many bridge

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.