使用sqlite中的数据创建可扩展列表视图?

时间:2015-07-22 05:05:59

标签: android sqlite expandablelistview

我想从存储在sqlite数据库中的数据填充Expandable Listview。

数据库助手

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

我的活动

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

        }

我得到的错误是这里的空指针异常返回mDb.rawQuery(query,null);

我关注此链接Android ExpandableListView and SQLite Database

请帮帮我。

2 个答案:

答案 0 :(得分:2)

使用以下代码。确保关闭活动上的mHelper或片段OnDestroy。

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";

    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 getReadableDatabase().rawQuery(query, null);
    }

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

答案 1 :(得分:0)

在fillData()

中获取Group之前,您需要编写以下代码
Map<String, String>