Your getChildrenCursor should:

1 - get the _ID value from "Cursor groupCursor"

2 - use that value in the query below, as the "where" condition, to only return those switched that are in the room from step 1.

Cursor groupCursor1 = sampleDB.rawQuery ("SELECT DISTINCT
RoomName AS RoomName1 ,_id , SwitchFullName FROM SwitchTable ORDER BY
RoomName",null);


-- Kostya

15.12.2010 13:18, pramod.deore пишет:
Hi Kostya Thanks for reply after changing code as
public class ExpandableList extends ExpandableListActivity
{
     private int mGroupIdColumnIndex;
     SQLiteDatabase sampleDB = null;
     String SAMPLE_DBNAME = "NewHomeAutoDataBase";
        String ROOM_TABLE_NAME = "RoomTable";
        private final String LOADTYPE_TABLE_NAME = "LoadTable";
        String SWITCH_TABLE_NAME = "SwitchTable";

     private String mPhoneNumberProjection[] = new String[] {
                "RoomName", "SwitchFullName"
     };


     private ExpandableListAdapter mAdapter;


     @Override
     public void onCreate(Bundle savedInstanceState)
     {
         super.onCreate(savedInstanceState);
         sampleDB =  this.openOrCreateDatabase(SAMPLE_DBNAME,
MODE_PRIVATE, null);

         Cursor groupCursor = sampleDB.rawQuery ("SELECT DISTINCT
RoomName AS RoomName1 ,_id , SwitchFullName FROM SwitchTable ORDER BY
RoomName",null);
         System.out.println ("#######"+groupCursor.getCount());
         // Cache the ID column index
         mGroupIdColumnIndex =
groupCursor.getColumnIndexOrThrow("_id");

         // Set up our adapter
         mAdapter = new MyExpandableListAdapter(groupCursor,
                 this,
                 android.R.layout.simple_expandable_list_item_1,
                 android.R.layout.simple_expandable_list_item_2,
                 new String[] {"RoomName1"}, // Name for group layouts
                 new int[] {android.R.id.text1},
                 new String[] {"SwitchFullName"}, // Number for child
layouts
                 new int[] {android.R.id.text1});
         setListAdapter(mAdapter);
     }

     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 groupCursor1 = sampleDB.rawQuery ("SELECT DISTINCT
RoomName AS RoomName1 ,_id , SwitchFullName FROM SwitchTable ORDER BY
RoomName",null);
                System.out.println ("#######"+groupCursor1.getCount());
                return groupCursor1;
         }

     }
}

But here now room name is printed more than once (if room has 3
switches then it is printed 3 times and so on). And you told that Use
that _ID value when querying the children table, But when I tried to
use "mGroupIdColumnIndex" then each time it return 1.


On Dec 15, 1:29 pm, Kostya Vasilyev<[email protected]>  wrote:
Pramod,

The "get field slot from row 0 col -1 failed" should already be familiar
to you.

It's caused by "DISTINCT RoomName" in the query. Since it's an
expression, it produces a column name that is *not* just "RoomName", and
getColumnIndex("RoomName") inside SimpleCursorAdapter gets -1 for column
index, which indicates failure. When this column index is used to
retrieve the value, you get the error message in log you included below.

To fix, assign a name to the expression-based column. You should also
always include _ID in any query you give to Android, as it expects that
to uniquely identify each row.

SELECT _ID, DISTINCT RoomName AS RoomName FROM SwitchTable
Secondly, your getChildrenCursor returns all children rows there are,
which is wrong. You are supposed to run a query that returns children of
the requested group. The group is identified by data at groupCursor's
current position.

So:

- Get the _ID from groupCursor - to determine the group you're supposed
to get the children for.

- Use that _ID value when querying the children table, to only get the
children for that group.

-- Kostya

15.12.2010 8:52, pramod.deore пишет:

Now when I changes cursor as
Cursor groupCursor = sampleDB.rawQuery ("SELECT DISTINCT RoomName FROM
SwitchTable ",null);
then it shows 2 rooms(i.e Hall and study room) but when i click on any
item then it throgh exception . my logact is :
12-15 11:16:56.612: ERROR/CursorWindow(893): Bad request for field
slot 0,-1. numRows = 2, numColumns = 1
12-15 11:16:56.642: DEBUG/AndroidRuntime(893): Shutting down VM
12-15 11:16:56.652: WARN/dalvikvm(893): threadid=3: thread exiting
with uncaught exception (group=0x4001b188)
12-15 11:16:56.663: ERROR/AndroidRuntime(893): Uncaught handler:
thread main exiting due to uncaught exception
12-15 11:16:56.742: ERROR/AndroidRuntime(893):
java.lang.IllegalStateException: get field slot from row 0 col -1
failed
12-15 11:16:56.742: ERROR/AndroidRuntime(893):     at
android.database.CursorWindow.getLong_native(Native Method)
12-15 11:16:56.742: ERROR/AndroidRuntime(893):     at
android.database.CursorWindow.getLong(CursorWindow.java:380)
12-15 11:16:56.742: ERROR/AndroidRuntime(893):     at
android.database.AbstractWindowedCursor.getLong(AbstractWindowedCursor.java:
108)
12-15 11:16:56.742: ERROR/AndroidRuntime(893):     at
android.widget.CursorTreeAdapter
$MyCursorHelper.getId(CursorTreeAdapter.java:435)
12-15 11:16:56.742: ERROR/AndroidRuntime(893):     at
android.widget.CursorTreeAdapter.getGroupId(CursorTreeAdapter.java:
190)
12-15 11:16:56.742: ERROR/AndroidRuntime(893):     at
android.widget.ExpandableListConnector.getItemId(ExpandableListConnector.java:
422)
12-15 11:16:56.742: ERROR/AndroidRuntime(893):     at
android.widget.AdapterView.getItemIdAtPosition(AdapterView.java:745)
12-15 11:16:56.742: ERROR/AndroidRuntime(893):     at
android.widget.AdapterView.setSelectedPositionInt(AdapterView.java:
1081)
12-15 11:16:56.742: ERROR/AndroidRuntime(893):     at
android.widget.AbsListView.onTouchEvent(AbsListView.java:2061)
12-15 11:16:56.742: ERROR/AndroidRuntime(893):     at
android.widget.ListView.onTouchEvent(ListView.java:3234)
12-15 11:16:56.742: ERROR/AndroidRuntime(893):     at
android.view.View.dispatchTouchEvent(View.java:3709)
12-15 11:16:56.742: ERROR/AndroidRuntime(893):     at
android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:852)
12-15 11:16:56.742: ERROR/AndroidRuntime(893):     at
android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
12-15 11:16:56.742: ERROR/AndroidRuntime(893):     at
android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
12-15 11:16:56.742: ERROR/AndroidRuntime(893):     at
android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
12-15 11:16:56.742: ERROR/AndroidRuntime(893):     at
com.android.internal.policy.impl.PhoneWindow
$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
12-15 11:16:56.742: ERROR/AndroidRuntime(893):     at
com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:
1107)
12-15 11:16:56.742: ERROR/AndroidRuntime(893):     at
android.app.Activity.dispatchTouchEvent(Activity.java:2061)
12-15 11:16:56.742: ERROR/AndroidRuntime(893):     at
com.android.internal.policy.impl.PhoneWindow
$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
12-15 11:16:56.742: ERROR/AndroidRuntime(893):     at
android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
12-15 11:16:56.742: ERROR/AndroidRuntime(893):     at
android.os.Handler.dispatchMessage(Handler.java:99)
12-15 11:16:56.742: ERROR/AndroidRuntime(893):     at
android.os.Looper.loop(Looper.java:123)
12-15 11:16:56.742: ERROR/AndroidRuntime(893):     at
android.app.ActivityThread.main(ActivityThread.java:4363)
12-15 11:16:56.742: ERROR/AndroidRuntime(893):     at
java.lang.reflect.Method.invokeNative(Native Method)
12-15 11:16:56.742: ERROR/AndroidRuntime(893):     at
java.lang.reflect.Method.invoke(Method.java:521)
12-15 11:16:56.742: ERROR/AndroidRuntime(893):     at
com.android.internal.os.ZygoteInit
$MethodAndArgsCaller.run(ZygoteInit.java:860)
12-15 11:16:56.742: ERROR/AndroidRuntime(893):     at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
12-15 11:16:56.742: ERROR/AndroidRuntime(893):     at
dalvik.system.NativeStart.main(Native Method)
On Dec 15, 9:19 am, "pramod.deore"<[email protected]>    wrote:
Hi Kostya Thanks for reply.
After changing code like
public class ExpandableList extends ExpandableListActivity
{
      private int mGroupIdColumnIndex;
      SQLiteDatabase sampleDB = null;
      String SAMPLE_DBNAME = "NewHomeAutoDataBase";
          String ROOM_TABLE_NAME = "RoomTable";
          private final String LOADTYPE_TABLE_NAME = "LoadTable";
          String SWITCH_TABLE_NAME = "SwitchTable";
      private String mPhoneNumberProjection[] = new String[] {
                  "RoomName", "SwitchFullName"
      };
      private ExpandableListAdapter mAdapter;
      @Override
      public void onCreate(Bundle savedInstanceState)
      {
          super.onCreate(savedInstanceState);
          sampleDB =  this.openOrCreateDatabase(SAMPLE_DBNAME,
MODE_PRIVATE, null);
          Cursor groupCursor = sampleDB.rawQuery ("SELECT _id, RoomName,
SwitchFullName FROM SwitchTable ORDER BY RoomName",null);
          System.out.println ("#######"+groupCursor.getCount());
          // Cache the ID column index
          mGroupIdColumnIndex =
groupCursor.getColumnIndexOrThrow("_id");
          // Set up our adapter
          mAdapter = new MyExpandableListAdapter(groupCursor,
                  this,
                  android.R.layout.simple_expandable_list_item_1,
                  android.R.layout.simple_expandable_list_item_1,
                  new String[] {"RoomName"}, // Name for group layouts
                  new int[] {android.R.id.text1},
                  new String[] {"SwitchFullName"}, // Number for child
layouts
                  new int[] {android.R.id.text1});
          setListAdapter(mAdapter);
      }
      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 groupCursor1 = sampleDB.rawQuery ("SELECT _id,
RoomName, SwitchFullName FROM SwitchTable ORDER BY RoomName",null);
                  System.out.println ("#######"+groupCursor1.getCount());
                  return groupCursor1;
          }
      }
}
It show me the list of room and when I click on any room item then
list of switches appear. But here I have 2 problems:
1. I have a room name as Hall which has 3 switches and second room as
Study room which has 2 switches. But now it shows me Hall 3 times
(because it has 3 switches) and similarly Study room 2 times. I want
it shows Hall only once, I also used DISTINCT for that but it not help
me.
2. In every room it shows all switches means total 5 switches (3
switches from Hall and 2 switches from study room.) I want after
clicking on study room it only shows 2 switches and on clicking on
Hall it shows 3 switches.
Anyone have idea how to achieve this. I think there is problem in
protected Cursor getChildrenCursor(Cursor groupCursor){} method. But I
didn't found it.
Thanks
On Dec 14, 4:32 pm, Kostya Vasilyev<[email protected]>    wrote:
Pramod,
Since your data is in a database, you may want to look at:
http://developer.android.com/reference/android/widget/CursorTreeAdapt...
or its "simple" version, which requires less code, since it's driven by
parameters passed into its constructor:
http://developer.android.com/reference/android/widget/SimpleCursorTre...
You can see an example of the latter here:
http://developer.android.com/resources/samples/ApiDemos/src/com/examp...
-- Kostya
14.12.2010 14:09, pramod.deore пишет:
For now I had just paste the code which is given at sample code.
Please give me a suggestion what should I write in this method.
On Dec 14, 4:07 pm, "pramod.deore"<[email protected]>      wrote:
Hi I want to display data from database in ExpandableList format. I
have a room table and one switch table I want to display it in
ExpandableList so when I click on room
...

read more »


--
Kostya Vasilyev -- WiFi Manager + pretty widget -- http://kmansoft.wordpress.com

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to