Expandablelistview延伸simplecursoradapter从源码来填充 [英] Expandablelistview extends simplecursoradapter to populate from sqlite

查看:190
本文介绍了Expandablelistview延伸simplecursoradapter从源码来填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何片段中实现的Andr​​oid SQLite的,而不是将其扩展到任何其他比片段也将这些条目中的扩展列表视图的孩子。

这是我所实现的:<一href="http://wptrafficanalyzer.in/blog/implementing-horizontal-view-swiping-using-viewpager-and-fragmentpageradapter-in-android/">http://wptrafficanalyzer.in/blog/implementing-horizontal-view-swiping-using-viewpager-and-fragmentpageradapter-in-android/

我实施视图寻呼机在我mainactivity因此我什么都做,我有在做的事。我不知道如何使用SQLite与片段和定制Expandablelist适配器,请帮助我。

下面我基本上走的是用户和年龄,我希望显示在expandablelistview的儿童用户条目的名称。

发行的日期表,因此无法打开该条目回吐活动,即活动2

更新:: SQLiteAdapter:

 公共类SQLiteDBAdapter {

私有静态最后字符串变量=DBAdapter; //用于记录数据库版本的变化
//数据库的信息:
公共静态最后弦乐DATABASE_NAME =进入;
公共静态最后弦乐DATABASE_TABLE2 =entryTable;
公共静态最后弦乐DATABASE_TABLE1 =dateTable;
公共静态最终诠释DATABASE_VERSION = 7; //版本号必须在每次更改数据库结构发生时间递增。

//字段名(日期表):
公共静态最后弦乐KEY_ROWID =_id;
公共静态最后弦乐KEY_UDATE =日期; //公共字段

//字段名(项表):
公共静态最后弦乐KEY_EROWID =_id;
公共静态最后弦乐KEY_CID =熙德; //微调范畴
公共静态最后弦乐KEY_TITLE =称号;
公共静态最后弦乐KEY_COST =成本;
公共静态最后弦乐KEY_NOTE =注意;
公共静态最后弦乐KEY_DATE =日期;
公共静态最后弦乐KEY_DAY =一天;
私有静态最后弦乐KEY_MONTH =月;
私有静态最后弦乐KEY_YEAR =一年;
公共静态最后弦乐KEY_TIME =时间;

公共静态最后的String [] ALL_KEYS_DATE =新的String [] {KEY_UDATE,KEY_ROWID};
公共静态最后的String [] ALL_KEYS_ENTRY =新的String [] {KEY_EROWID,KEY_CID,KEY_TITLE,KEY_COST,KEY_NOTE,KEY_DATE,KEY_DAY,KEY_MONTH,KEY_YEAR,KEY_TIME};



// SQL语句来创建数据库
私有静态最后弦乐DATABASE_CREATE_SQL_DATE =
        CREATE TABLE+ DATABASE_TABLE1
        +(+ KEY_ROWID +INTEGER PRIMARY KEY AUTOINCREMENT,
        + KEY_UDATE +INTEGER UNIQUE NOT NULL
        +);;
私有静态最后弦乐DATABASE_CREATE_SQL_ENTRY =
        CREATE TABLE+ DATABASE_TABLE2
        +(+ KEY_DATE +TEXT NOT NULL,
        + KEY_EROWID +INTEGER PRIMARY KEY AUTOINCREMENT,
        + KEY_CID +INTEGER
        + KEY_TITLE +TEXT NOT NULL,
        + KEY_COST +INTEGER
        + KEY_NOTE +TEXT NOT NULL,
        + KEY_DAY +INTEGER NOT NULL,
        + KEY_MONTH +TEXT NOT NULL,
        + KEY_YEAR +INTEGER NOT NULL,
        + KEY_TIME +TEXT NOT NULL
                +外键(+ KEY_DATE +)参考+ DATABASE_TABLE1 +(+ KEY_UDATE +)//外键
        +);;


私人最终上下文的背景下;
私人DatabaseHelper myDBHelper;
私人SQLiteDatabase分贝;


公共SQLiteDBAdapter(上下文CTX){
    this.context = CTX;
    myDBHelper =新DatabaseHelper(上下文);
}

//打开数据库连接。
公共SQLiteDBAdapter的open(){
    DB = myDBHelper.getWritableDatabase();
    回到这一点;
}

//关闭数据库连接。
公共无效的close(){
    myDBHelper.close();
}

//添加一组新值的将被插入到数据库中。
//操作的日期表
众长insertRowDate(字符串datestring){
    ContentValues​​值=新ContentValues​​();
    values​​.put(KEY_UDATE,datestring);
    CharSequence的文字=日期已经插入;
    INT持续时间= Toast.LENGTH_SHORT;
    吐司面包= Toast.makeText(背景,文本,持续时间);
    toast.show();
    返回db.insert(DATABASE_TABLE1,空,价值观);
}
//操作的入门表
众长insertRowEntry(CID字符串,字符串名称,字符串成本,串音符,字符串日期,字符串一天,字符串monthDb,年整型,字符串时间){
    ContentValues​​ initialValues​​ =新ContentValues​​();
    initialValues​​.put(KEY_CID,CID);
    initialValues​​.put(KEY_TITLE,职称);
    initialValues​​.put(KEY_COST,成本);
    initialValues​​.put(KEY_NOTE,请注意);
    initialValues​​.put(KEY_DATE,日期);
    initialValues​​.put(KEY_DAY,日);
    initialValues​​.put(KEY_MONTH,monthDb);
    initialValues​​.put(KEY_YEAR,一年);
    initialValues​​.put(KEY_TIME,时间);
    CharSequence的文本=行已经插入;
    INT持续时间= Toast.LENGTH_SHORT;
    吐司面包= Toast.makeText(背景,文本,持续时间);
    toast.show();
    //将数据存入数据库。
    返回db.insert(DATABASE_TABLE2,空,initialValues​​);
}

//从数据库中删除一行,用ROWID(主键)
公共布尔deleteRowDate(长ROWID){
    字符串,其中= KEY_ROWID +=+ ROWID;
    返回db.delete(DATABASE_TABLE1,其中,空)!= 0;
}
公共布尔deleteRowEntry(长ROWID){
    字符串,其中= KEY_EROWID +=+ ROWID;
    返回db.delete(DATABASE_TABLE2,其中,空)!= 0;
}
公共无效deleteAllDate(){
    光标C = getAllRowsDate();
    长ROWID = c.getColumnIndexOrThrow(KEY_ROWID);
    如果(c.moveToFirst()){
        做 {
            deleteRowDate(c.getLong((int)的ROWID));
        }而(c.moveToNext());
    }
    c.close();
}
公共无效deleteAllEntry(){
    光标C = getAllRowsEntry();
    长ROWID = c.getColumnIndexOrThrow(KEY_ROWID);
    如果(c.moveToFirst()){
        做 {
            deleteRowEntry(c.getLong((int)的ROWID));
        }而(c.moveToNext());
    }
    c.close();
}

//返回所有数据库中的数据。
公共光标getAllRowsDate(){
    字符串,其中= NULL;
    光标C = db.query(真,DATABASE_TABLE1,ALL_KEYS_DATE,其中,NULL,NULL,NULL,NULL,NULL);
    如果(C!= NULL){
        c.moveToFirst();
    }
    返回℃;
}
公共光标getAllRowsEntry(){
    字符串,其中= NULL;
    光标C = db.query(真,DATABASE_TABLE2,ALL_KEYS_ENTRY,其中,NULL,NULL,NULL,NULL,NULL);
    如果(C!= NULL){
        c.moveToFirst();
    }
    返回℃;
}

//获取特定行(通过ROWID)
公共光标getRowDate(长ROWID){
    字符串,其中= KEY_ROWID +=+ ROWID;
    光标C = db.query(真,DATABASE_TABLE1,ALL_KEYS_DATE,
                    在这里,NULL,NULL,NULL,NULL,NULL);
    如果(C!= NULL){
        c.moveToFirst();
    }
    返回℃;
}
公共光标getRowEntry(长ROWID){
    字符串,其中= KEY_EROWID +=+ ROWID;
    光标C = db.query(真,DATABASE_TABLE2,ALL_KEYS_ENTRY,
            在这里,NULL,NULL,NULL,NULL,NULL);
    如果(C!= NULL){
        c.moveToFirst();
    }
    返回℃;
}

//改变现有的行等于新的数据。
公共布尔updateRowDate(长ROWID,字符串日期){
    字符串,其中= KEY_ROWID +=+ ROWID;
    ContentValues​​ newValues​​ =新ContentValues​​();
    newValues​​.put(KEY_UDATE,日期);
    //把它插入到数据库中。
    返回db.update(DATABASE_TABLE2,newValues​​,其中,空)!= 0;
}
公共布尔updateRowEntry(长ROWID,CID字符串,字符串名称,字符串成本,串音符,字符串日期,字符串一天,串月,年整型,字符串时间){
    字符串,其中= KEY_EROWID +=+ ROWID;
    ContentValues​​ newValues​​ =新ContentValues​​();
    newValues​​.put(KEY_CID,CID);
    newValues​​.put(KEY_TITLE,职称);
    newValues​​.put(KEY_COST,成本);
    newValues​​.put(KEY_NOTE,请注意);
    newValues​​.put(KEY_DATE,日期);
    newValues​​.put(KEY_DAY,日);
    newValues​​.put(KEY_MONTH,月);
    newValues​​.put(KEY_YEAR,一年);
    newValues​​.put(KEY_TIME,时间);
    //把它插入到数据库中。
    返回db.update(DATABASE_TABLE1,newValues​​,其中,空)!= 0;
}
//取出组从数据库分贝
公共光标fetchgroup(){
    查询字符串=SELECT * FROM datetable;
            返回db.rawQuery(查询,NULL);
}
//从数据库分贝取儿童
公共光标fetchChildren(字符串日期){
    查询字符串=SELECT * FROM entrytable其中date ='+日期+';
    返回db.rawQuery(查询,NULL);
}
私有静态类DatabaseHelper扩展SQLiteOpenHelper
{
    DatabaseHelper(上下文的背景下){
        超(背景下,DATABASE_NAME,空,DATABASE_VERSION);
    }

    @覆盖
    公共无效的onCreate(SQLiteDatabase _db){
        _db.execSQL(DATABASE_CREATE_SQL_ENTRY);
        _db.execSQL(DATABASE_CREATE_SQL_DATE);
    }

    @覆盖
    公共无效onUpgrade(SQLiteDatabase _db,诠​​释oldVersion,诠释静态网页){
        Log.w(TAG,从版本升级应用程序的数据库+ oldVersion
                +到+动态网页+,这将销毁所有旧的数据!);

        //销毁旧的数据库:
        _db.execSQL(DROP TABLE IF EXISTS+ DATABASE_CREATE_SQL_DATE);
        _db.execSQL(DROP TABLE IF EXISTS+ DATABASE_CREATE_SQL_ENTRY);

        //重新创建新的数据库:
        的onCreate(_db);
    }
}
}
 

修订:: 从活性2我是存储数据为

  entryDb.insertRowDate(日期);
                entryDb.insertRowEntry(一,标题,费用,注意事项,日期,Daym,monthDb,一年,时间);
 

和这里是我的expandableListview

  LV =(ExpandableListView)v.findViewById(R.id.expandable_list);
        ExpandableListAdapter expandableListAdapter =新ExpandableListAdapter(平日,子女);
        lv.setAdapter(expandableListAdapter);


 公共类ExpandableListAdapter扩展BaseExpandableListAdapter {

    私人LayoutInflater INF;
    私有String []组;
    私有String [] []儿童;

    公共ExpandableListAdapter(字符串[]组,字符串[] []儿童){
        this.groups =组;
        this.children =儿童;
        的inf = LayoutInflater.from(getActivity());
    }

    @覆盖
    公众诠释getGroupCount(){
        返回groups.length;
    }

    @覆盖
    公众诠释getChildrenCount(INT groupPosition){

        返回儿[groupPosition] .length;
    }

    @覆盖
    公共对象getGroup(INT groupPosition){
        返回组[groupPosition]
    }

    @覆盖
    公共对象getChild(INT groupPosition,诠释childPosition){
        返回儿[groupPosition] [childPosition]
    }

    @覆盖
    众长getGroupId(INT groupPosition){
        返回groupPosition;
    }

    @覆盖
    众长getChildId(INT groupPosition,诠释childPosition){
        返回childPosition;
    }

    @覆盖
    公共布尔hasStableIds(){
        返回true;
    }

    @覆盖
    公共查看getChildView(INT groupPosition,最终诠释childPosition,布尔isLastChild,查看convertView,ViewGroup中父){
        ViewHolder持有人;

        如果(convertView == NULL){
            convertView = inf.inflate(R.layout.list_item,父母,假);
            持有人=新ViewHolder();

            holder.text =(TextView中)convertView.findViewById(R.id.lblListItem);
            convertView.setTag(保持器);
        } 其他 {
            支架=(ViewHolder)convertView.getTag();
        }
holder.text.setText(+ myDataFromActivity);
        返回convertView;
    }

    @覆盖
    公共查看getGroupView(INT groupPosition,布尔isExpanded,查看convertView,ViewGroup中父){
        ViewHolder持有人;
        的TextView TextView的;
        如果(convertView == NULL){
            convertView = inf.inflate(R.layout.list_group,父母,假);

            持有人=新ViewHolder();
            holder.text =(TextView中)convertView.findViewById(R.id.lblListHeader);
            convertView.setTag(保持器);
        } 其他 {
            支架=(ViewHolder)convertView.getTag();
        }


      // holder.text.setText(getGroup(groupPosition)的ToString()); //测试
        //设置组的位置,因此日期
        TextView的=(的TextView)convertView.findViewById(R.id.date);
        textView.setTextSize(30);
        如果((groupPosition + 1) - ; 10)
        textView.setText(0+(groupPosition + 1));
        其他
        textView.setText(+(groupPosition + 1));
        //去做
        / *
        *想想一个潜台词,使布局更加互动
        * * /
        返回convertView;
    }

    @覆盖
    公共布尔isChildSelectable(INT groupPosition,诠释childPosition){
        返回true;
    }

    私有类ViewHolder {
        TextView的文字;
    }
}
 

解决方案

我不能添加评论,所以我在这里试着理解你的问题。 我可以在你的SQLiteAdapter看:你没有一个方法拿在数据库中填充您Expandablelistview回所有记录

也许你需要这样的方法:

  //获取所有记录
 公开名单&LT;记录&GT; getAllRecords(){
 名单&LT;记录&GT; recordList =新的ArrayList&LT;记录&GT;();
 //选择所有查询
 字符串selectQuery =SELECT * FROM+ DATABASE_TABLE;

SQLiteDatabase DB = this.getWritableDatabase();
光标光标= db.rawQuery(selectQuery,NULL);

//遍历所有行和添加到列表
如果(cursor.moveToFirst()){
//字符串名称,字符串成本,串音符,字符串日期,字符串月,年整型,字符串时间
    做 {
        Records唱片公司=新的记录();
        records.setTitle(cursor.getString(0));
        records.setCost(cursor.getString(1));
        records.setNote(cursor.getString(2));
        records.setDate(cursor.getString(3));
        records.setMounth(cursor.getString(4));
        records.setYear(的Integer.parseInt(cursor.getString(5)));
        records.setTime(cursor.getString(6));
        //添加记录清单
        recordsList.add(记录);
    }而(cursor.moveToNext());
}

//返回联系人列表
返回recordsList;
}
 

您需要一个类Record.java是管理的setTitle,setCost ....你可以采取一切记录了像这样

这code必须是所著到收回的数据从sqlite的活动。

  MySQLiteHelper DB =新MySQLiteHelper(本);
名单&LT;记录&GT;记录= db.getAllRecords();

    对于(RC的记录:记录){
        //做一些事情记录
    }
 

该人士是在这里: DatabaseSqlite与方法

那是什么问题?

How to implement android SQLite within fragments and not extending it to any other than fragments also putting those entries in the child of the expandable listview.

This is what i have implemented : http://wptrafficanalyzer.in/blog/implementing-horizontal-view-swiping-using-viewpager-and-fragmentpageradapter-in-android/

I'm implementing view pager in my mainactivity hence what ever i do i have to do within it. I dont know how to use SQLite with fragment and custom Expandablelist adapter please help me out.

Here i am basically taking the name of the user and age as user entries which i want to be displayed in the child of expandablelistview.

Issue with the date table hence can't open the entry taking activity i.e activity 2

Updated:: SQLiteAdapter:

public class SQLiteDBAdapter {

private static final String TAG = "DBAdapter"; //used for logging database version changes
// DataBase info:
public static final String DATABASE_NAME = "entry";
public static final String DATABASE_TABLE2 = "entryTable";
public static final String DATABASE_TABLE1= "dateTable";
public static final int DATABASE_VERSION = 7; // The version number must be incremented each time a change to DB structure occurs.

//Field Names(date table):
public static final String KEY_ROWID = "_id";
public static final String KEY_UDATE ="date"; //common field

// Field Names(entry table):
public static final String KEY_EROWID = "_id";
public static final String KEY_CID = "Cid"; //spinner category
public static final String KEY_TITLE = "title";
public static final String KEY_COST = "cost";
public static final String KEY_NOTE = "note";
public static final String KEY_DATE = "date";
public static final String KEY_DAY ="day";
private static final String KEY_MONTH = "month";
private static final String KEY_YEAR = "year";
public static final String KEY_TIME = "time";

public static final String[] ALL_KEYS_DATE = new String[] { KEY_UDATE,KEY_ROWID};
public static final String[] ALL_KEYS_ENTRY = new String[] {KEY_EROWID,KEY_CID, KEY_TITLE,KEY_COST,KEY_NOTE,KEY_DATE,KEY_DAY,KEY_MONTH,KEY_YEAR,KEY_TIME};



//SQL statement to create database
private static final String DATABASE_CREATE_SQL_DATE=
        "CREATE TABLE" + DATABASE_TABLE1
        +"("+ KEY_ROWID +"INTEGER PRIMARY KEY AUTOINCREMENT, "
        + KEY_UDATE +"INTEGER UNIQUE NOT NULL"
        +");";
private static final String DATABASE_CREATE_SQL_ENTRY =
        "CREATE TABLE " + DATABASE_TABLE2
        + " (" + KEY_DATE + " TEXT NOT NULL,"
        + KEY_EROWID +"INTEGER PRIMARY KEY AUTOINCREMENT, "
        + KEY_CID + " INTEGER,"
        + KEY_TITLE + " TEXT NOT NULL, "
        + KEY_COST + " INTEGER,"
        + KEY_NOTE+ " TEXT NOT NULL,"
        + KEY_DAY + "INTEGER NOT NULL, "
        + KEY_MONTH + " TEXT NOT NULL,"
        + KEY_YEAR + " INTEGER NOT NULL,"
        + KEY_TIME + " TEXT NOT NULL"
                +"FOREIGN KEY ("+KEY_DATE+")REFERENCES "+DATABASE_TABLE1+"("+KEY_UDATE+")" //FOREIGN KEY
        + ");";


private final Context context;
private DatabaseHelper myDBHelper;
private SQLiteDatabase db;


public SQLiteDBAdapter(Context ctx) {
    this.context = ctx;
    myDBHelper = new DatabaseHelper(context);
}

// Open the database connection.
public SQLiteDBAdapter open() {
    db = myDBHelper.getWritableDatabase();
    return this;
}

// Close the database connection.
public void close() {
    myDBHelper.close();
}

// Add a new set of values to be inserted into the database.
//operations for the date table
public long insertRowDate(String datestring){
    ContentValues values= new ContentValues();
    values.put(KEY_UDATE,datestring);
    CharSequence text = " Date has been inserted";
    int duration = Toast.LENGTH_SHORT;
    Toast toast = Toast.makeText(context, text, duration);
    toast.show();
    return db.insert(DATABASE_TABLE1,null,values);
}
//operations for the entry table
public long insertRowEntry(String cid, String title, String cost, String note,String date, String day, String monthDb, int year, String time) {
    ContentValues initialValues = new ContentValues();
    initialValues.put(KEY_CID, cid);
    initialValues.put(KEY_TITLE, title);
    initialValues.put(KEY_COST,cost);
    initialValues.put(KEY_NOTE,note);
    initialValues.put(KEY_DATE, date);
    initialValues.put(KEY_DAY, day);
    initialValues.put(KEY_MONTH, monthDb);
    initialValues.put(KEY_YEAR, year);
    initialValues.put(KEY_TIME,time);
    CharSequence text = " Row has been inserted";
    int duration = Toast.LENGTH_SHORT;
    Toast toast = Toast.makeText(context, text, duration);
    toast.show();
    // Insert the data into the database.
    return db.insert(DATABASE_TABLE2, null, initialValues);
}

// Delete a row from the database, by rowId (primary key)
public boolean deleteRowDate(long rowId) {
    String where = KEY_ROWID + "=" + rowId;
    return db.delete(DATABASE_TABLE1, where, null) != 0;
}
public boolean deleteRowEntry(long rowId) {
    String where = KEY_EROWID + "=" + rowId;
    return db.delete(DATABASE_TABLE2, where, null) != 0;
}
public void deleteAllDate() {
    Cursor c = getAllRowsDate();
    long rowId = c.getColumnIndexOrThrow(KEY_ROWID);
    if (c.moveToFirst()) {
        do {
            deleteRowDate(c.getLong((int) rowId));
        } while (c.moveToNext());
    }
    c.close();
}
public void deleteAllEntry() {
    Cursor c = getAllRowsEntry();
    long rowId = c.getColumnIndexOrThrow(KEY_ROWID);
    if (c.moveToFirst()) {
        do {
            deleteRowEntry(c.getLong((int) rowId));
        } while (c.moveToNext());
    }
    c.close();
}

// Return all data in the database.
public Cursor getAllRowsDate() {
    String where = null;
    Cursor c =  db.query(true, DATABASE_TABLE1, ALL_KEYS_DATE, where, null, null, null, null, null);
    if (c != null) {
        c.moveToFirst();
    }
    return c;
}
public Cursor getAllRowsEntry() {
    String where = null;
    Cursor c =  db.query(true, DATABASE_TABLE2, ALL_KEYS_ENTRY, where, null, null, null, null, null);
    if (c != null) {
        c.moveToFirst();
    }
    return c;
}

// Get a specific row (by rowId)
public Cursor getRowDate(long rowId) {
    String where = KEY_ROWID + "=" + rowId;
    Cursor c =  db.query(true, DATABASE_TABLE1, ALL_KEYS_DATE,
                    where, null, null, null, null, null);
    if (c != null) {
        c.moveToFirst();
    }
    return c;
}
public Cursor getRowEntry(long rowId) {
    String where = KEY_EROWID + "=" + rowId;
    Cursor c =  db.query(true, DATABASE_TABLE2, ALL_KEYS_ENTRY,
            where, null, null, null, null, null);
    if (c != null) {
        c.moveToFirst();
    }
    return c;
}

// Change an existing row to be equal to new data.
public boolean updateRowDate(long rowId,String date) {
    String where = KEY_ROWID + "=" + rowId;
    ContentValues newValues = new ContentValues();
    newValues.put(KEY_UDATE, date);
    // Insert it into the database.
    return db.update(DATABASE_TABLE2, newValues, where, null) != 0;
}
public boolean updateRowEntry(long rowId,String cid, String title, String cost, String note,String date,String day, String month, int year, String time) {
    String where = KEY_EROWID + "=" + rowId;
    ContentValues newValues = new ContentValues();
    newValues.put(KEY_CID, cid);
    newValues.put(KEY_TITLE, title);
    newValues.put(KEY_COST, cost);
    newValues.put(KEY_NOTE, note);
    newValues.put(KEY_DATE, date);
    newValues.put(KEY_DAY, day);
    newValues.put(KEY_MONTH, month);
    newValues.put(KEY_YEAR, year);
    newValues.put(KEY_TIME, time);
    // Insert it into the database.
    return db.update(DATABASE_TABLE1, newValues, where, null) != 0;
}
//fetching groups from the database db
public Cursor fetchgroup(){
    String query="SELECT * FROM datetable";
            return db.rawQuery(query,null);
}
//fetching children from the database db
public Cursor fetchChildren(String date) {
    String query = "SELECT * FROM entrytable WHERE date = '" + date + "'";
    return db.rawQuery(query, null);
}
private static class DatabaseHelper extends SQLiteOpenHelper
{
    DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase _db) {
        _db.execSQL(DATABASE_CREATE_SQL_ENTRY);
        _db.execSQL(DATABASE_CREATE_SQL_DATE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase _db, int oldVersion, int newVersion) {
        Log.w(TAG, "Upgrading application's database from version " + oldVersion
                + " to " + newVersion + ", which will destroy all old data!");

        // Destroy old database:
        _db.execSQL("DROP TABLE IF EXISTS " + DATABASE_CREATE_SQL_DATE);
        _db.execSQL("DROP TABLE IF EXISTS " + DATABASE_CREATE_SQL_ENTRY);

        // Recreate new database:
        onCreate(_db);
    }
}
}

UPDATED:: From the Activity2 i am the storing the data as

entryDb.insertRowDate(date);
                entryDb.insertRowEntry(a, title, cost, note,date, Daym,monthDb, year, time);

and here is my expandableListview

lv = (ExpandableListView) v.findViewById(R.id.expandable_list);
        ExpandableListAdapter expandableListAdapter = new ExpandableListAdapter(weekdays, children);
        lv.setAdapter(expandableListAdapter);


 public class ExpandableListAdapter extends BaseExpandableListAdapter {

    private  LayoutInflater inf;
    private String[] groups;
    private String[][] children;

    public ExpandableListAdapter(String[] groups, String[][] children) {
        this.groups = groups;
        this.children = children;
        inf = LayoutInflater.from(getActivity());
    }

    @Override
    public int getGroupCount() {
        return groups.length;
    }

    @Override
    public int getChildrenCount(int groupPosition) {

        return children[groupPosition].length;
    }

    @Override
    public Object getGroup(int groupPosition) {
        return groups[groupPosition];
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return children[groupPosition][childPosition];
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getChildView(int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ViewHolder holder;

        if (convertView == null) {
            convertView = inf.inflate(R.layout.list_item, parent, false);
            holder = new ViewHolder();

            holder.text = (TextView) convertView.findViewById(R.id.lblListItem);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
holder.text.setText(""+ myDataFromActivity);
        return convertView;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        ViewHolder holder;
        TextView textView;
        if (convertView == null) {
            convertView = inf.inflate(R.layout.list_group, parent, false);

            holder = new ViewHolder();
            holder.text = (TextView) convertView.findViewById(R.id.lblListHeader);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }


      //  holder.text.setText(getGroup(groupPosition).toString());//testing
        //Sets the group position hence the Date
        textView=(TextView)convertView.findViewById(R.id.date);
        textView.setTextSize(30);
        if((groupPosition+1)<10)
        textView.setText("0"+(groupPosition+1));
        else
        textView.setText(""+(groupPosition+1));
        //TODO
        /*
        * Think of a subtext to make the layout much more interactive
        * */
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    private class ViewHolder {
        TextView text;
    }
}

解决方案

I can't add comment so i try to understand your problem here. As i can see on your SQLiteAdapter: you don't have a method for take back all records in your database for populate your Expandablelistview.

Maybe you need a method like:

 // Getting All records
 public List<Records> getAllRecords() {
 List<Records> recordList = new ArrayList<Records>();
 // Select All Query
 String selectQuery = "SELECT  * FROM " + DATABASE_TABLE;

SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);

// looping through all rows and adding to list
if (cursor.moveToFirst()) {
//String title, String cost, String note,String date, String month, int year, String time
    do {
        Records records = new Record();
        records.setTitle(cursor.getString(0));
        records.setCost(cursor.getString(1));
        records.setNote(cursor.getString(2));
        records.setDate(cursor.getString(3));
        records.setMounth(cursor.getString(4));
        records.setYear(Integer.parseInt(cursor.getString(5)));
        records.setTime(cursor.getString(6));
        // Adding records to list
        recordsList.add(record);
    } while (cursor.moveToNext());
}

// return contact list
return recordsList;
}

You need a class Record.java that manage the setTitle,setCost .... and you can take all record with a for like this

This code must be writed to the Activity that take back data from sqlite.

MySQLiteHelper db = new MySQLiteHelper(this);
List<Records> records = db.getAllRecords();       

    for (Records rc : records) {
        //Do something with the records 
    }

The source is here: DatabaseSqlite with method

Is that the problem?

这篇关于Expandablelistview延伸simplecursoradapter从源码来填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆