从Android中的数据库填充expandableListView的数据

从Android中的数据库填充expandableListView的数据,android,sqlite,expandablelistview,Android,Sqlite,Expandablelistview,我想从数据库中填充expandableListView中的数据。实际上,我需要做的就是从一个我的数据库中检索所有数据 单表,因为第一列是groupName,其余四列是child,但这里我被卡住了。。。 下面是我正在尝试的一段代码 公共类ViewDetail扩展了活动{ private ExpandableListView mExpandableListView; private List<GroupEntity> mGroupCollection; public void onCr

我想从数据库中填充expandableListView中的数据。实际上,我需要做的就是从一个我的数据库中检索所有数据 单表,因为第一列是groupName,其余四列是child,但这里我被卡住了。。。 下面是我正在尝试的一段代码

公共类ViewDetail扩展了活动{

private ExpandableListView mExpandableListView;
private List<GroupEntity> mGroupCollection;

public void onCreate(Bundle savedInstanceState ){

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    prepareResource();
    initPage();
}

private void prepareResource() {

    mGroupCollection = new ArrayList<GroupEntity>();
    DBClass db = new DBClass(ViewDetail.this);
    SQLiteDatabase dc = db.getReadableDatabase();
    Cursor cur = dc.query(true, "db_table", new String[]{"title","_url","username","password","comment"}, null, null, null, null, null, null);
    ContentValues cv = null;
    String _title = "";
    String _url = "";
    String _usrname = "";
    String _pasword = "";
    String _comment = "";

    if(cur.getCount() <= 0){
        Toast.makeText(ViewDetail.this, "empty...Please add.", Toast.LENGTH_LONG).show();
    }else{

        for(int i=0;i<cur.getCount();i++){              

            _title = cur.getString(0);
            _url = cur.getString(1);
            _usrname = cur.getString(2);
            _pasword = cur.getString(3);
            _comment = cur.getString(4);

            GroupEntity ge = new GroupEntity();
            ge.Name = _title;
                GroupItemEntity gi = ge.new GroupItemEntity();
                gi.Name =  _url;//"Child" + j;
                gi.Name =  _usrname;
                gi.Name =  _pasword;
                gi.Name =  _comment;
            mGroupCollection.add(ge);
            cur.moveToNext();   

        }

    }

    cur.close();
    db.close();
    dc.close(); 
    }

}


private void initPage() {

    mExpandableListView = (ExpandableListView) findViewById(R.id.expandableListView);
    ExpandableListAdapter adapter = new ExpandableListAdapter(this,mExpandableListView, mGroupCollection);
    mExpandableListView.setAdapter(adapter);

}
private ExpandableListView mExpandableListView;
私人名单收集;
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
prepareResource();
initPage();
}
私有void prepareResource(){
mGroupCollection=新的ArrayList();
DBClass db=新的DBClass(ViewDetail.this);
SQLiteDatabase dc=db.getReadableDatabase();
Cursor cur=dc.query(true,“db_table”,新字符串[]{“title”,“_url”,“username”,“password”,“comment”},null,null,null,null,null);
ContentValues cv=null;
字符串_title=“”;
字符串_url=“”;
字符串_usrname=“”;
字符串_pasword=“”;
字符串_comment=“”;

如果(cur.getCount()不是存储数据的列表,请直接将其从光标中拉出

  Cursor cursor = pContext.getContentResolver().query(DEFAULT_URI, null, null, null, null);
然后是一些例子

@Override
public int getCount() {
    return cursor.getCount();
}

@Override
public Object getGroup(int position) {
    cursor.moveToPosition(position);
    String column1 = cursor.getString(COLUMN_1);
    String column2 = cursor.getString(COLUMN_2);
    GroupObject group = new Group(column1, column2);
    return group;
}

我正在尝试做一些与此非常类似的事情,您是否可以发布您使用的布局文件?这几乎是一个教程!
  Cursor cursor = pContext.getContentResolver().query(DEFAULT_URI, null, null, null, null);
@Override
public int getCount() {
    return cursor.getCount();
}

@Override
public Object getGroup(int position) {
    cursor.moveToPosition(position);
    String column1 = cursor.getString(COLUMN_1);
    String column2 = cursor.getString(COLUMN_2);
    GroupObject group = new Group(column1, column2);
    return group;
}