我的应用程序类似于todo应用程序,其中待办事项列表与标记相关联 例如: tag1:“星期天” 待办事项列表:任务:“唤醒迟到”信息:“task1” 任务:“快速突破”信息:“task2”
tag2:“星期一” 待办事项清单:任务:“6点起床”信息:“早醒” 任务:“阅读新闻报纸”信息:“task2” 任务:“吃快点”信息:“task3”
当用户选择一个标签(比如星期日)时,我正在制作该标签的待办事项列表,并在我的片段中将其显示为列表视图。列表视图中的每个条目都显示任务和信息详细信息。
现在我在每个列表条目上实施长按,并显示选项“删除条目”和“更新信息”。
对于“删除”操作,我已从数据库中删除该条目并显示列表更新列表视图。
对于“更新信息”,读取用户输入并更新“信息”详细信息并刷新列表视图。
我能够实现除数据库操作之外的所有操作,例如,如果用户选择删除并使用给定信息更新特定行,则删除相应的行。
我正在接受帮助 http://www.androidhive.info/2013/09/android-sqlite-database-with-multiple-tables/
对名称进行一些修改(不是逻辑)
但是有些删除和更新如何使用KEY_ID。
以下是创建表格的代码:
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String CREATE_TABLE_ROWDATA = "CREATE TABLE IF NOT EXISTS "
+ TABLE_ROWDATA + "(" + KEY_ID + " INTEGER PRIMARY KEY," + KEY_ROWDATA
+ " TEXT," + KEY_INFOPATH + " TEXT," + KEY_STATUS + " INTEGER," + KEY_CREATED_AT
+ " DATETIME" + ")";
// CategoryTag table create statement
private static final String CREATE_TABLE_TAG = "CREATE TABLE IF NOT EXISTS " + TABLE_TAG
+ "(" + KEY_ID + " INTEGER PRIMARY KEY," + KEY_TAG_NAME + " TEXT,"
+ KEY_CREATED_AT + " DATETIME" + ")";
// rowdata_tag table create statement
private static final String CREATE_TABLE_ROWDATA_TAG = "CREATE TABLE IF NOT EXISTS "
+ TABLE_ROWDATA_TAG + "(" + KEY_ID + " INTEGER PRIMARY KEY,"
+ KEY_ROWDATA_ID + " INTEGER," + KEY_TAG_ID + " INTEGER,"
+ KEY_CREATED_AT + " DATETIME" + ")";
public static DatabaseHelper sInstance;
public static synchronized DatabaseHelper getInstance(Context context) {
if (sInstance == null) {
sInstance = new DatabaseHelper(context.getApplicationContext());
}
return sInstance;
}
@Override
public void onCreate(SQLiteDatabase db) {
// creating required tables
db.execSQL(CREATE_TABLE_ROWDATA);
db.execSQL(CREATE_TABLE_TAG);
db.execSQL(CREATE_TABLE_ROWDATA_TAG);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// on upgrade drop older tables
db.execSQL("DROP TABLE IF EXISTS " + TABLE_ROWDATA);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_TAG);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_ROWDATA_TAG);
// create new tables
onCreate(db);
}
// ------------------------ "rowdatas" table methods ----------------//
/**
* Creating a rowdata
*/
public long createRowData(RowData rowdata, long[] tag_ids) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_ROWDATA, rowdata.getPath());
values.put(KEY_INFOPATH, rowdata.getInfoPath());
values.put(KEY_STATUS, rowdata.getStatus());
values.put(KEY_CREATED_AT, getDateTime());
// insert row
long rowdata_id = db.insert(TABLE_ROWDATA, null, values);
Log.e(LOG, "New rowdata id for: " + rowdata.getPath() + "is: " + rowdata_id + "tag id: " + tag_ids[0] + " to table: " + TABLE_ROWDATA);
// insert tag_ids
for (long tag_id : tag_ids) {
createRowDataTag(rowdata_id, tag_id);
}
return rowdata_id;
}
/**
* get single rowdata
*/
public RowData getRowData(long rowdata_id) {
SQLiteDatabase db = this.getReadableDatabase();
String selectQuery = "SELECT * FROM " + TABLE_ROWDATA + " WHERE "
+ KEY_ID + " = " + rowdata_id;
Log.e(LOG, selectQuery);
Cursor c = db.rawQuery(selectQuery, null);
if (c != null)
c.moveToFirst();
RowData td = new RowData();
td.setId(c.getInt(c.getColumnIndex(KEY_ID)));
td.setPath((c.getString(c.getColumnIndex(KEY_ROWDATA))));
td.setInfoNote((c.getString(c.getColumnIndex(KEY_INFOPATH))));
td.setCreatedAt(c.getString(c.getColumnIndex(KEY_CREATED_AT)));
return td;
}
/**
* getting all rowdatas
* */
public List<RowData> getAllRowDatas() {
List<RowData> rowdatas = new ArrayList<RowData>();
String selectQuery = "SELECT * FROM " + TABLE_ROWDATA;
Log.e(LOG, selectQuery);
SQLiteDatabase db = this.getReadableDatabase();
Cursor c = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (c.moveToFirst()) {
do {
RowData td = new RowData();
td.setId(c.getInt((c.getColumnIndex(KEY_ID))));
td.setPath((c.getString(c.getColumnIndex(KEY_ROWDATA))));
td.setInfoNote((c.getString(c.getColumnIndex(KEY_INFOPATH))));
td.setCreatedAt(c.getString(c.getColumnIndex(KEY_CREATED_AT)));
// adding to rowdata list
rowdatas.add(td);
} while (c.moveToNext());
}
return rowdatas;
}
/**
* getting all rowdatas under single tag
* */
public List<RowData> getAllRowDatasByCategoryTag(String tag_name) {
List<RowData> rowdatas = new ArrayList<RowData>();
String selectQuery = "SELECT * FROM " + TABLE_ROWDATA + " td, "
+ TABLE_TAG + " tg, " + TABLE_ROWDATA_TAG + " tt WHERE tg."
+ KEY_TAG_NAME + " = '" + tag_name + "'" + " AND tg." + KEY_ID
+ " = " + "tt." + KEY_TAG_ID + " AND td." + KEY_ID + " = "
+ "tt." + KEY_ROWDATA_ID;
Log.e(LOG, selectQuery);
SQLiteDatabase db = this.getReadableDatabase();
Cursor c = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (c.moveToFirst()) {
do {
RowData td = new RowData();
td.setId(c.getInt((c.getColumnIndex(KEY_ID))));
td.setPath((c.getString(c.getColumnIndex(KEY_ROWDATA))));
td.setInfoNote((c.getString(c.getColumnIndex(KEY_INFOPATH))));
td.setCreatedAt(c.getString(c.getColumnIndex(KEY_CREATED_AT)));
// adding to rowdata list
rowdatas.add(td);
} while (c.moveToNext());
}
return rowdatas;
}
public RowData getRowDataId(String tag_name, RowData rowdata) {
String selectQuery = "SELECT * FROM " + TABLE_ROWDATA + " td, "
+ TABLE_TAG + " tg, " + TABLE_ROWDATA_TAG + " tt WHERE tg."
+ KEY_TAG_NAME + " = '" + tag_name + "'" + " AND tg." + KEY_ID
+ " = " + "tt." + KEY_TAG_ID + " AND td." + KEY_ID + " = "
+ "tt." + KEY_ROWDATA_ID;
SQLiteDatabase db = this.getReadableDatabase();
Cursor c = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (c.moveToFirst()) {
do {
if(rowdata.getPath().equals(c.getString(c.getColumnIndex(KEY_ROWDATA)))) {
rowdata.setId(c.getColumnIndex(KEY_ID));
rowdata.setInfoNote(c.getString(c.getColumnIndex(KEY_INFOPATH)));
rowdata.setCreatedAt(c.getString(c.getColumnIndex(KEY_CREATED_AT)));
break;
}
} while (c.moveToNext());
}
Log.e(LOG, rowdata.getPath() + " got id: " + rowdata.getId());
return rowdata;
}
/**
* getting rowdata count
*/
public int getRowDataCount() {
String countQuery = "SELECT * FROM " + TABLE_ROWDATA;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
int count = cursor.getCount();
cursor.close();
// return count
return count;
}
/**
* Updating a rowdata
*/
public int updateRowData(RowData rowdata) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_ROWDATA, rowdata.getPath());
values.put(KEY_INFOPATH, rowdata.getInfoPath());
values.put(KEY_STATUS, rowdata.getStatus());
// updating row
return db.update(TABLE_ROWDATA, values, KEY_ID + " = ?",
new String[] { String.valueOf(rowdata.getId()) });
}
public int getRowDataCountByCategoryTag(String tag_name) {
String selectQuery = "SELECT * FROM " + TABLE_ROWDATA + " td, "
+ TABLE_TAG + " tg, " + TABLE_ROWDATA_TAG + " tt WHERE tg."
+ KEY_TAG_NAME + " = '" + tag_name + "'" + " AND tg." + KEY_ID
+ " = " + "tt." + KEY_TAG_ID + " AND td." + KEY_ID + " = "
+ "tt." + KEY_ROWDATA_ID;
SQLiteDatabase db = this.getReadableDatabase();
Cursor c = db.rawQuery(selectQuery, null);
int count = c.getCount();
return count;
}
/**
* Deleting a rowdata
*/
public void deleteRowData(long todo_id) {
SQLiteDatabase db = this.getWritableDatabase();
Log.e(LOG, "Deleting rowdata id: " + todo_id + " from table: " + TABLE_ROWDATA);
int ret = db.delete(TABLE_ROWDATA, KEY_ID + " = ?",
new String[] { String.valueOf(todo_id) });
}
// ------------------------ "tags" table methods ----------------//
/**
* Creating tag
*/
public long createCategoryTag(CategoryTag tag) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_TAG_NAME, tag.getTagName());
values.put(KEY_CREATED_AT, getDateTime());
// insert row
long tag_id = db.insert(TABLE_TAG, null, values);
return tag_id;
}
public String[] getAllCategoryTagNames() {
String[] tagNames = null;
int count = 0;
String selectQuery = "SELECT * FROM " + TABLE_TAG;
Log.e(LOG, selectQuery);
SQLiteDatabase db = this.getReadableDatabase();
Cursor c = db.rawQuery(selectQuery, null);
tagNames = new String[c.getCount()];
// looping through all rows and adding to list
if (c.moveToFirst()) {
do {
tagNames[count] = c.getString(c.getColumnIndex(KEY_TAG_NAME));
count++;
} while (c.moveToNext());
}
return tagNames;
}
/**
* getting all tags
* */
public List<CategoryTag> getAllCategoryTags() {
List<CategoryTag> tags = new ArrayList<CategoryTag>();
String selectQuery = "SELECT * FROM " + TABLE_TAG;
Log.e(LOG, selectQuery);
SQLiteDatabase db = this.getReadableDatabase();
Cursor c = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (c.moveToFirst()) {
do {
CategoryTag t = new CategoryTag();
t.setId(c.getInt((c.getColumnIndex(KEY_ID))));
t.setTagName(c.getString(c.getColumnIndex(KEY_TAG_NAME)));
// adding to tags list
tags.add(t);
} while (c.moveToNext());
}
return tags;
}
public CategoryTag getCategoryTagId(CategoryTag tag) {
String selectQuery = "SELECT * FROM " + TABLE_TAG;
boolean found = false;
Log.e(LOG, selectQuery);
SQLiteDatabase db = this.getReadableDatabase();
Cursor c = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (c.moveToFirst()) {
do {
if(c.getString(c.getColumnIndex(KEY_TAG_NAME)).equals(tag.getTagName())) {
tag.setId(c.getInt((c.getColumnIndex(KEY_ID))));
found = true;
break;
}
} while (c.moveToNext());
}
if(found) {
return tag;
} else {
tag.setId(-1);
return tag;
}
}
/**
* Updating a tag
*/
public int updateCategoryTag(CategoryTag tag) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_TAG_NAME, tag.getTagName());
// updating row
return db.update(TABLE_TAG, values, KEY_ID + " = ?",
new String[] { String.valueOf(tag.getId()) });
}
/**
* Deleting a tag
*/
public void deleteCategoryTag(CategoryTag tag, boolean should_delete_all_tag_rowdatas) {
SQLiteDatabase db = this.getWritableDatabase();
// before deleting tag
// check if rowdatas under this tag should also be deleted
if (should_delete_all_tag_rowdatas) {
// get all rowdatas under this tag
List<RowData> allTagToDos = getAllRowDatasByCategoryTag(tag.getTagName());
// delete all rowdatas
for (RowData rowdata : allTagToDos) {
// delete rowdata
deleteRowData(rowdata.getId());
}
}
// now delete the tag
db.delete(TABLE_TAG, KEY_ID + " = ?",
new String[] { String.valueOf(tag.getId()) });
}
// ------------------------ "rowdata_tags" table methods ----------------//
/**
* Creating rowdata_tag
*/
public long createRowDataTag(long rowdata_id, long tag_id) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_ROWDATA_ID, rowdata_id);
values.put(KEY_TAG_ID, tag_id);
values.put(KEY_CREATED_AT, getDateTime());
Log.e(LOG, "Inserting rowdata_id: " + rowdata_id + " tag id: " + tag_id + " to table: " + TABLE_ROWDATA_TAG);
long id = db.insert(TABLE_ROWDATA_TAG, null, values);
Log.e(LOG, "inserted id: " + id);
return id;
}
public void printRowData_Tag() {
String selectQuery = "SELECT * FROM " + TABLE_ROWDATA_TAG;
Log.e(LOG, selectQuery);
SQLiteDatabase db = this.getReadableDatabase();
Cursor c = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (c.moveToFirst()) {
do {
Log.e(LOG, TABLE_ROWDATA_TAG + "tag id: " + c.getInt(c.getColumnIndex(KEY_TAG_ID)) + "rowdata id: " + c.getInt(c.getColumnIndex(KEY_ROWDATA_ID)));
}while (c.moveToNext());
}
}
/**
* Updating a rowdata tag
*/
public int updateRowData_Tag(long id, long tag_id) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_TAG_ID, tag_id);
// updating row
return db.update(TABLE_ROWDATA_TAG, values, KEY_ID + " = ?",
new String[] { String.valueOf(id) });
}
/**
* Deleting a rowdata tag
*/
public void deleteRowData_Tag(long id) {
SQLiteDatabase db = this.getWritableDatabase();
Log.e(LOG, "Deleting id: " + id + " from : " + TABLE_ROWDATA_TAG);
db.delete(TABLE_ROWDATA_TAG, KEY_ID + " = ?",
new String[] { String.valueOf(id) });
}
// closing database
public void closeDB() {
SQLiteDatabase db = this.getReadableDatabase();
if (db != null && db.isOpen())
db.close();
}
/**
* get datetime
* */
private String getDateTime() {
SimpleDateFormat dateFormat = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss", Locale.getDefault());
Date date = new Date();
return dateFormat.format(date);
}
}
我可以在TABLE_ROWDATA中创建条目 我无法删除TABLE_ROWDATA中的条目 下面调用删除条目和更新列表视图的代码:
public void deleteListEntry(int position) {
CategoryTag tag = new CategoryTag(title);
tag = db.getCategoryTagId(tag);
RowData rowdata = new RowData(pathList[position], 0);
List<RowData> rowdatas = db.getAllRowDatasByCategoryTag(title);
for (RowData rd : rowdatas) {
if (rd.getPath().equals(pathList[position])) {
db.deleteRowData(rd.getId());
break;
}
}
Log.e("tagIt", "Count: " + db.getRowDataCountByCategoryTag(title));
}
public void updateRowDataList(){
List<RowData> rowdatas = db.getAllRowDatasByCategoryTag(title);
int i = 0;
if(rowdatas.size() == 0) {
Log.e("tagIt", "No list found ");
}
pathList = new String[rowdatas.size()];
pathInfoList = new String[rowdatas.size()];
for (RowData rowdata : rowdatas) {
pathList[i] = rowdata.getPath();
pathInfoList[i] = rowdata.getInfoPath();
Log.e("tagIt", "rowdata-id: " + rowdata.getId());
i++;
}
}
public View updateFragmentWithData(){
updateRowDataList();
lpadapter = new ListPathsAdapter(getActivity(), pathList, pathInfoList);
lv = (ListView) rootView.findViewById(R.id.pathsList);
lv.setAdapter(lpadapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
openFile(position);
return;
}
});
registerForContextMenu(lv);
return rootView;
}
public void updateInfo(final int position) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Update info");
final EditText input = new EditText(getActivity());
builder.setView(input);
builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
String value = input.getText().toString();
CategoryTag tag = new CategoryTag(value);
List<RowData> rowdatas = db.getAllRowDatasByCategoryTag(title);
int i = 0;
for(RowData rd : rowdatas){
if (i == position){
rd.setInfoNote(value);
db.updateRowData(rd);
break;
}
}
updateFragmentWithData();
}
});
以上删除调用来自onContextItemSelected(MenuItem item);中的以下选择。其中info.position位于public String [] list pathList的列表条目中。
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId()) {
case R.id.send:
openFile(info.position);
return true;
case R.id.delete:
deleteListEntry(info.position);
updateFragmentWithData();
return true;
case R.id.updateinfo:
updateInfo(info.position);
return true;
default:
return super.onContextItemSelected(item);
}
}
我不确定DB中的问题在哪里。 db.delete返回0但删除后重新创建列表再次显示相同的列表。