【问题标题】:Populating Expandable Listview with SQLite database使用 SQLite 数据库填充可扩展列表视图
【发布时间】:2018-02-19 03:34:22
【问题描述】:

我有一个 SQLite 数据库,其中包含 ID、作业、类名、日期等列。我还有一个可扩展的列表视图。我的代码工作并创建了一个可扩展的列表视图,但是它复制了组标题。

组名来自类名列,子对象添加到具有相同类名的组中。然而,当我真的需要它来获取每个唯一的类名时,可扩展的列表视图会获取每个类名,这样它就不会重复组。这是我的可扩展列表视图适配器:

public class ExpandListAdapter extends CursorTreeAdapter {

SQLiteHelper values;
private LayoutInflater mInflator;
Context context;
TextView groupHeader;

public ExpandListAdapter(Cursor cursor, Context context){
    super(cursor, context);
    this.context = context;
    values = new SQLiteHelper(context);
    mInflator = LayoutInflater.from(context);
}

@Override
public View newGroupView(Context context, Cursor cursor, boolean isExpanded,
                         ViewGroup parent){
    final View view = mInflator.inflate(R.layout.group_header, parent, false);

    return view;
}

public void bindGroupView(View view, Context context, Cursor cursor, boolean isExpanded){
    groupHeader = (TextView) view.findViewById(R.id.group_name);
    if(groupHeader != null){
        groupHeader.setText(cursor.getString(cursor.getColumnIndex("class")));
    }
}

@Override
public View newChildView(Context context, Cursor cursor,
                         boolean isLastChild, ViewGroup parent){
    final View view = mInflator.inflate(R.layout.row_expandable, parent, false);

    return view;
}

@Override
public void bindChildView(View view, final Context context, final Cursor cursor,
                          boolean isLastChild){

    TextView homework = (TextView) view.findViewById(R.id.homeworkDisplay1);
    if(homework != null){
        homework.setText(cursor.getString(cursor.getColumnIndex(values.KEY_HOMEWORK)));
    }
    TextView date = (TextView) view.findViewById(R.id.dueDisplay1);
    if(date != null){
        date.setText(cursor.getString(cursor.getColumnIndex(values.KEY_DATE)));
    }
    TextView classes = (TextView) view.findViewById(R.id.classDisplay1);
    if(classes != null){
        classes.setText(cursor.getString(cursor.getColumnIndex(values.KEY_CLASS)));
    }
}

@Override
protected  Cursor getChildrenCursor(Cursor groupCursor){
    values.open();
    Cursor childCursor = values.fetchChildren(groupCursor.getString(groupCursor.getColumnIndex("class")));
    childCursor.moveToFirst();
    values.close();
    return childCursor;
}

@Override
public void onGroupCollapsed(int groupPosition){
}

@Override
public void onGroupExpanded(int groupPosition){

}

}

这是我的 SQLitehelper 类: 公共类 SQLiteHelper{

public static final int DATABASE_VERSION = 1;

public static final String DATABASE_NAME = "homeworkManager";

public static final String TABLE_NAME = "homeworkList";

public static final String KEY_ID = "_id";
public static final String KEY_HOMEWORK = "homework";
public static final String KEY_DATE = "date";
public static final String KEY_CLASS = "class";
private SQLiteDatabase db;
private DblistHelper myHelper;
Cursor DB_Cursor;

Context context;


class DblistHelper extends SQLiteOpenHelper {


public DblistHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);

}

@Override
public void onCreate(SQLiteDatabase db) {
    String CREATE_TABLE = "Create table " + TABLE_NAME + " ("
            + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
            + KEY_HOMEWORK + " TEXT ,"
            + KEY_DATE + " TEXT ,"
            + KEY_CLASS + " TEXT)";

    db.execSQL(CREATE_TABLE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
    onCreate(db);
}
}

public SQLiteHelper(Context context){
    this.context = context;
}

public SQLiteHelper open() throws SQLException{
    myHelper = new DblistHelper(context);
    db = myHelper.getReadableDatabase();
    return this;
}

public void close(){
    if(myHelper != null){
        myHelper.close();
    }
}

public long insertData(String homework, String data, String classes){
    ContentValues values = new ContentValues();
    values.put(KEY_HOMEWORK, homework);
    values.put(KEY_DATE, data);
    values.put(KEY_CLASS, classes);

    return db.insert(TABLE_NAME, null, values);
}

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

public Cursor fetchChildren(String class_name){

    String query = "SELECT * FROM homeworkList WHERE class = '" + class_name + "'";
    Cursor cursor = db.rawQuery(query, null);

    return cursor;
}

public void remove(long id){
    String whereClause = "_id=?";
    String[] whereArgs = new String[]{String.valueOf(id)};
    db.delete(TABLE_NAME, whereClause, whereArgs);
}

}

提前致谢:)

【问题讨论】:

    标签: android sqlite listview expandablelistview


    【解决方案1】:

    你好,而不是从这个构造函数传递光标 public ExpandListAdapter(光标光标,上下文上下文){ 为什么不将其替换为用于您的 ListView 的列表

    public ExpandListAdapter(上下文上下文,列出父级,列出子级){ 这样您就不必在每个视图中都获取数据以防止数据重复

    【讨论】:

    • 不过,我是在扩展 cursortreeadapter,所以我不必通过 Cursor 吗?
    猜你喜欢
    • 1970-01-01
    • 2019-12-12
    • 2023-03-13
    • 2023-03-12
    • 1970-01-01
    • 2016-06-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多