ID, 숙제, 클래스 이름, 날짜가있는 SQLite 데이터베이스가 있습니다. 확장 가능한 목록 뷰도 있습니다. 내 코드가 작동하고 확장 가능한 목록보기가 만들어 지지만 그룹 제목이 중복됩니다.SQLite 데이터베이스로 확장 가능한 목록 뷰 채우기
그룹 이름은 클래스 이름 열에서, 그리고 하위 개체는 동일한 클래스 이름을 가진 그룹에 추가된다. 그러나 그룹을 복제하지 않도록 각각의 고유 한 클래스 이름을 가져야 할 때 확장 가능한 listview는 각 클래스 이름을 필요로합니다. 내 SQLitehelper 클래스는 여기
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 {사전에
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);
}
}
감사합니다 :)
중복되는 것을 방지하기 위해 모든보기에서 데이터를 가져 가질 수 있도록 (컨텍스트 컨텍스트, 목록 부모, 목록 아이) { , 그래서 나는에게이 없습니다 커서를 통과해야합니까? –