1

I'm creating Expandable list-view in Android Application which I want to show the all Sqlite data in Expandable List view and also show the sqlite database in child view. I'm trying show the data in expandable list view but it does not fetch all the data in expandable list view.

I got only first column name in listDataHeader in but I also want to fetch all the details in every child view.

public class My_Project extends Activity
{
    ExpandableListAdapter listAdapter;
    ExpandableListView expListView;
    List<String> listDataHeader;
    HashMap<String, List<String>> listDataChild;
    DBHelper databaseHelper;
    SQLiteDatabase db;
    Adapter adp;
    Cursor mGroupsCursor;


    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_project);
        databaseHelper = new DBHelper(this);
        databaseHelper.onOpen(db);
        expListView = (ExpandableListView) findViewById(R.id.ProjectExpandeble_ListView);
        fillData();


        // get the listview
            //expListView = (ExpandableListView) findViewById(R.id.ProjectExpandeble_ListView);

            listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);

            // setting list adapter
            //expListView.setAdapter(listAdapter);

            // Listview Group click listener
            expListView.setOnGroupClickListener(new OnGroupClickListener() {

                @Override
                public boolean onGroupClick(ExpandableListView parent, View v,
                        int groupPosition, long id) {
                    // Toast.makeText(getApplicationContext(),
                    // "Group Clicked " + listDataHeader.get(groupPosition),
                    // Toast.LENGTH_SHORT).show();
                     parent.smoothScrollToPosition(groupPosition);
                    return false;
                }
            });

            // Listview Group expanded listener
            expListView.setOnGroupExpandListener(new OnGroupExpandListener() {

                @Override
                public void onGroupExpand(int groupPosition) {
                    Toast.makeText(getApplicationContext(),
                            listDataHeader.get(groupPosition) + " Expanded",
                            Toast.LENGTH_SHORT).show();
                }
            });

            // Listview Group collasped listener
            expListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {

                @Override
                public void onGroupCollapse(int groupPosition) {
                    Toast.makeText(getApplicationContext(),
                            listDataHeader.get(groupPosition) + " Collapsed",
                            Toast.LENGTH_SHORT).show();

                }
            });

            // Listview on child click listener
            expListView.setOnChildClickListener(new OnChildClickListener() {

                @Override
                public boolean onChildClick(ExpandableListView parent, View v,
                        int groupPosition, int childPosition, long id) {
                    // TODO Auto-generated method stub
                    Toast.makeText(
                            getApplicationContext(),
                            listDataHeader.get(groupPosition)
                                    + " : "
                                    + listDataChild.get(
                                            listDataHeader.get(groupPosition)).get(
                                            childPosition), Toast.LENGTH_SHORT)
                            .show();
                    return false;
                }
            });
        }



     private void fillData() { 
            // set list adapter here

           mGroupsCursor = databaseHelper.fetchGroup();
           this.startManagingCursor(mGroupsCursor);
            mGroupsCursor.moveToFirst();

            expListView = (ExpandableListView) this.findViewById(R.id.ProjectExpandeble_ListView);

            MyExpandableListAdapter  mAdapter = new MyExpandableListAdapter(mGroupsCursor, this,
                R.layout.list_group,                     // Your row layout for a group
                R.layout.child_item,                 // Your row layout for a child
                new String[] { "project_name" },                      // Field(s) to use from group cursor
                new int[] { R.id.lblListHeader},                 // Widget ids to put group data into
                new String[] { "project_id", "project_name", "project_date_created", "project_end_date", "IsActive"},  // Field(s) to use from child cursors
                new int[] { R.id.TextView_Projectdetails, R.id.textOne, R.id.textTwo, R.id.textthree, R.id.textFour });          // Widget ids to put child data into

            expListView.setAdapter(mAdapter);                         // set the list adapter.
            }

        /*private void prepareListData() 
        {
            databaseHelper = new DBHelper(this);
            databaseHelper.onOpen(db);

            listDataHeader = new ArrayList<String>();
            listDataHeader = databaseHelper.getAllProjects();
            listDataChild = new HashMap<String, List<String>>();

            // Adding child data
            List<String> top250 = new ArrayList<String>();
            //top250 = databaseHelper.getAllProjects();
            //top250.add("The Shawshank Redemption");

            listDataChild.put(listDataHeader.get(0), top250); // Header, Child data
            listDataChild.putAll(listDataChild);
        }
         */
          public class MyExpandableListAdapter extends SimpleCursorTreeAdapter {
                // Your adapter

                 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 = databaseHelper.fetchChildren(groupCursor.getString(groupCursor.getColumnIndex("project_name")));            
                            My_Project.this.startManagingCursor(childCursor);
                            childCursor.moveToFirst();
                            return childCursor;

            }

          }

}

Log Cat info

06-13 15:23:20.193: W/dalvikvm(17071): threadid=1: thread exiting with uncaught exception (group=0x40015560)
06-13 15:23:20.203: E/AndroidRuntime(17071): FATAL EXCEPTION: main
06-13 15:23:20.203: E/AndroidRuntime(17071): java.lang.IllegalStateException: get field slot from row 0 col -1 failed
06-13 15:23:20.203: E/AndroidRuntime(17071):    at android.database.CursorWindow.getLong_native(Native Method)
06-13 15:23:20.203: E/AndroidRuntime(17071):    at android.database.CursorWindow.getLong(CursorWindow.java:380)
06-13 15:23:20.203: E/AndroidRuntime(17071):    at android.database.AbstractWindowedCursor.getLong(AbstractWindowedCursor.java:108)
06-13 15:23:20.203: E/AndroidRuntime(17071):    at android.widget.CursorTreeAdapter$MyCursorHelper.getId(CursorTreeAdapter.java:437)
06-13 15:23:20.203: E/AndroidRuntime(17071):    at android.widget.CursorTreeAdapter.getGroupId(CursorTreeAdapter.java:192)
06-13 15:23:20.203: E/AndroidRuntime(17071):    at android.widget.ExpandableListConnector.getItemId(ExpandableListConnector.java:421)
06-13 15:23:20.203: E/AndroidRuntime(17071):    at android.widget.AbsListView$PerformClick.run(AbsListView.java:1812)
06-13 15:23:20.203: E/AndroidRuntime(17071):    at android.os.Handler.handleCallback(Handler.java:587)
06-13 15:23:20.203: E/AndroidRuntime(17071):    at android.os.Handler.dispatchMessage(Handler.java:92)
06-13 15:23:20.203: E/AndroidRuntime(17071):    at android.os.Looper.loop(Looper.java:123)
06-13 15:23:20.203: E/AndroidRuntime(17071):    at android.app.ActivityThread.main(ActivityThread.java:3683)
06-13 15:23:20.203: E/AndroidRuntime(17071):    at java.lang.reflect.Method.invokeNative(Native Method)
06-13 15:23:20.203: E/AndroidRuntime(17071):    at java.lang.reflect.Method.invoke(Method.java:507)
06-13 15:23:20.203: E/AndroidRuntime(17071):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-13 15:23:20.203: E/AndroidRuntime(17071):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-13 15:23:20.203: E/AndroidRuntime(17071):    at dalvik.system.NativeStart.main(Native Method)
halfer
  • 19,824
  • 17
  • 99
  • 186
tazeenmulani
  • 600
  • 5
  • 18
  • 43
  • You should use below links for your program output. http://stackoverflow.com/questions/10644914/android-expandablelistview-and-sqlite-database & http://androidexample.com/Custom_Expandable_ListView_Tutorial_-_Android_Example/index.php?view=article_discription&aid=107&aaid=129 – Jay Jun 13 '14 at 07:13
  • Hi , I have used your code but my application is crashed at run time.And I m getting runtime exception. I post above log cat information.Please give me suggestion.Thanks for the reply. – tazeenmulani Jun 13 '14 at 08:51
  • Can someone help me to solve this.Expandable listView. – tazeenmulani Jun 13 '14 at 09:09
  • @Jay, Please give me solution I got First column name in lableHeader only but not get all child view when i click on lableheader app get crash listview does not expand.Please help me. – tazeenmulani Jun 13 '14 at 09:34
  • please post the code for your activity here ... – madteapot Jun 13 '14 at 09:39
  • @Setu , I have post my Activity code above , Please see. – tazeenmulani Jun 13 '14 at 09:52

0 Answers0