mardi 21 juillet 2015

creating a expandable list view with data in the sqlite?

I Want to populate the Expandable Listview from the data stored in the sqlite database.

Database Helper

public class databasehelperhome extends SQLiteOpenHelper
{
    static String DATABASE_NAME="SYSTECH_home.DB";
    public static final String TABLE_NAME="Sites";
    public static final String TABLE_NAME1="Days";
    public static final String KEY_ID="KEY_ID";
    public static final String SITE_NAME="Site_Name";
    public static final String DAY_NAME="Day_Name";
    private SQLiteDatabase mDb;

    public databasehelperhome(Context context)
    {
        super(context, DATABASE_NAME, null,1);


    }
    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        String CREATE_TABLE="CREATE TABLE "+TABLE_NAME+" ("+KEY_ID+" INTEGER PRIMARY KEY, "+SITE_NAME+" TEXT, "+DAY_NAME+" TEXT)";
        db.execSQL(CREATE_TABLE);
    }
    @Override
    public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
        // TODO Auto-generated method stub

    }
    public Cursor fetchGroup() {
        String query = "SELECT * FROM Sites";
        return mDb.rawQuery(query, null);
    }

    public Cursor fetchChildren(String Site_Name) {
        String query = "SELECT * FROM Sites WHERE KEY_ID = '" + Site_Name + "'";
        return mDb.rawQuery(query, null);
    }
}

My Activity

private void fillData() {
        Cursor mGroupsCursor;
        mGroupsCursor = mHelper.fetchGroup();
        startManagingCursor(mGroupsCursor);
        mGroupsCursor.moveToFirst();

        ExpandableListView elv = (ExpandableListView) findViewById(android.R.id.list);

        madapter = new MyExpandableListAdapter(mGroupsCursor, Home.this,
            R.layout.list_group,                     // Your row layout for a group
            R.layout.list_item,                 // Your row layout for a child
            new String[] { "Site_Name" },                      // Field(s) to use from group cursor
            new int[] { R.id.lblListHeader },                 // Widget ids to put group data into
            new String[] { "Day_Name" },          // Field(s) to use from child cursors
            new int[] { R.id.lblListItem });          // Widget ids to put child data into

        elv.setAdapter(madapter);                         // set the list adapter.
        }

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

        }

the Error what iam getting is null pointer exception over here return mDb.rawQuery(query, null);

I Followed this link Android ExpandableListView and SQLite Database

please help me.

Aucun commentaire:

Enregistrer un commentaire