使用SQLite数据库

时间:2017-09-10 23:25:18

标签: android sqlite listview expandablelistview

我有一个SQLite数据库,其中包含列ID,Homework,Classname,date。我还有一个可扩展的列表视图。我的代码可以工作并创建一个可扩展的列表视图,但它会复制组标题。

enter image description here

组名来自classname列,子对象被添加到具有相同类名的组中。但是,当我真正需要它时,可扩展列表视图会获取每个类名,以获取每个唯一的类名,以便它不会复制组。这是我的可扩展listview适配器:

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

}

提前致谢:)

1 个答案:

答案 0 :(得分:0)

嗨,而不是从这个构造函数传递Cursor public ExpandListAdapter(Cursor cursor,Context context){ 为什么不用用于ListView的List替换它

public ExpandListAdapter(Context context,List parent,List child){ 这样你就不必在每个视图中获取数据,以防止数据被复制