我正在构建一个应用程序,在该应用程序中,我将数据库中的数据填充到我的活动列表视图中。现在,在 longItemClick 侦听器上,我想从数据库中删除该值并更新应用程序中的列表视图。
这是我在列表视图上的 longClickListener 代码:
private ListView showing_history;
private ListAdapter mHistoryListAdapter;
private ArrayList<SearchHistoryDetails> searchArrayList;
private ArrayList<SearchHistoryDetails> HistoryObjArrayList;
mHistoryListAdapter = new ArrayAdapter<String>(this,
R.layout.mylistlayout, populateList());
showing_history.setAdapter(mHistoryListAdapter);
HistoryObjArrayList = new ArrayList<SearchHistoryDetails>();
/**
* Long tap on listview to delete the selected item from the history
* list.
* */
showing_history
.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> arg0,
View arg1, int arg2, long arg3) {
final AlertDialog.Builder b = new AlertDialog.Builder(
MainActivity.this);
b.setIcon(android.R.drawable.ic_dialog_alert);
b.setMessage("Delete this from history?");
b.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
Toast.makeText(getApplicationContext(),
"Yes", Toast.LENGTH_LONG)
.show();
}
});
b.setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
dialog.cancel();
}
});
b.show();
return true;
}
});
通过这段代码,我将数据插入数据库:
/**
* Inserting the string when user searches for anything into the database.
* */
public void insertHistory(SearchHistoryDetails paraHistoryDetailsPojoObj) {
AndroidOpenDbHelper androidOpenDbHelperObj = new AndroidOpenDbHelper(
this);
SQLiteDatabase sqliteDatabase = androidOpenDbHelperObj
.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(AndroidOpenDbHelper.COLUMN_NAME_HISTORY_STRING,
paraHistoryDetailsPojoObj.getHistoryName());
@SuppressWarnings("unused")
long affectedColumnId = sqliteDatabase.insert(
AndroidOpenDbHelper.TABLE_NAME_HISTORY, null, contentValues);
sqliteDatabase.close();
}
这是我从数据库中检索数据并将其重新填充到列表视图中的代码:
public List<String> populateList() {
List<String> HistoryNamesList = new ArrayList<String>();
AndroidOpenDbHelper openHelperClass = new AndroidOpenDbHelper(this);
SQLiteDatabase sqliteDatabase = openHelperClass.getReadableDatabase();
Cursor cursor = sqliteDatabase
.query(AndroidOpenDbHelper.TABLE_NAME_HISTORY,
new String[] { AndroidOpenDbHelper.COLUMN_NAME_HISTORY_STRING },
null, null, null, null, AndroidOpenDbHelper.ID
+ " DESC");
startManagingCursor(cursor);
while (cursor.moveToNext()) {
String ugName = cursor
.getString(cursor
.getColumnIndex(AndroidOpenDbHelper.COLUMN_NAME_HISTORY_STRING));
SearchHistoryDetails histPojoClass = new SearchHistoryDetails();
histPojoClass.setHistoryName(ugName);
searchArrayList.add(histPojoClass);
HistoryNamesList.add(ugName);
}
sqliteDatabase.close();
int history_db = cursor.getCount();
if (history_db == 0) {
history_layout.setVisibility(LinearLayout.GONE);
} else {
history_layout.setVisibility(LinearLayout.VISIBLE);
}
return HistoryNamesList;
}
检索和插入数据一切都很完美,但删除特定行对我不起作用。现在我已经使用 toast 做了一些操作并且它正在工作,我应该怎么做,从列表视图中删除行后数据库数据更新。请给我任何想法来克服这个问题。