我的程序中有一个列表视图。在那里我填充了数据库中的值并显示。我使用上下文菜单编辑,删除listview中的项目。如果我在上下文菜单中选择删除它删除数据库中的项目,但我没有从列表视图中删除该项目。我在这做错了什么。如果有人知道,请帮助我。
检索方法:
public void retrievedb() {
strquery = "SELECT * FROM api_settings";
Cursor mCursor = (MainscreenActivity.JEEMAAndroSMSDB).rawQuery(
strquery, null);
if (mCursor.getCount() != 0) {
mCursor.moveToLast();
for (int i = mCursor.getCount() - 1; i >= 0; i--) {
dbid = mCursor.getString(0);
dbapiname = mCursor.getString(1);
dbURL = mCursor.getString(2);
dbUnameVar = mCursor.getString(3);
dbUnameVal = mCursor.getString(4);
dbPwdVar = mCursor.getString(5);
dbPwdVal = mCursor.getString(6);
dbsendername = mCursor.getString(7);
dbdestinationvar = mCursor.getString(8);
dbmsgvariable = mCursor.getString(9);
dbchars = mCursor.getString(10);
}
}
if ((dbapiname == null) || (dbURL == null) || (dbUnameVar == null)
|| (dbUnameVal == null) || (dbPwdVar == null)
|| (dbPwdVal == null) || (dbsendername == null)
|| (dbdestinationvar == null) || (dbmsgvariable == null)
|| (dbchars == null)) {
tvText = (TextView) findViewById(android.R.id.text1);
tvText.setVisibility(1);
} else {
adapter = new ArrayAdapter<Settingsmodel>(this,
android.R.layout.simple_list_item_1, listItems);
tvText = (TextView) findViewById(android.R.id.text1);
tvText.setVisibility(View.GONE);
strquery = "SELECT ID,API_Name FROM api_settings";
mCursor = (MainscreenActivity.JEEMAAndroSMSDB).rawQuery(strquery,
null);
if (mCursor.getCount() != 0) {
mCursor.moveToFirst();
do {
dbid = mCursor.getString(0);
dbapiname = mCursor.getString(1);
listItems.add(new Settingsmodel(dbapiname, dbid));
adapter.notifyDataSetChanged();
} while (mCursor.moveToNext());
}
}
}
上下文菜单:
@Override
public void onCreateContextMenu(ContextMenu menu, final View v,
ContextMenuInfo menuInfo) {
if (v.getId() == android.R.id.list) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
Settingsmodel selectedValue = (Settingsmodel) getListAdapter()
.getItem(info.position);
String tempo = selectedValue.getSpinnerText();
menu.setHeaderTitle(tempo);
String[] menuItems = getResources().getStringArray(R.array.menu);
for (int i = 0; i < menuItems.length; i++) {
menu.add(Menu.NONE, i, i, menuItems[i]);
}
}
}
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item
.getMenuInfo();
int menuItemIndex = item.getItemId();
String[] menuItems = getResources().getStringArray(R.array.menu);
String menuItemName = menuItems[menuItemIndex];
Settingsmodel selectedValue = (Settingsmodel) getListAdapter().getItem(
info.position);
String temp0 = selectedValue.getSpinnerText();
String temp1 = selectedValue.getValue();
int id = Integer.parseInt(temp1);
if (menuItemName.equalsIgnoreCase("Set as Default")) {
ContentValues values = new ContentValues();
values.put(MainscreenActivity.COL_Set_Default, value);
MainscreenActivity.JEEMAAndroSMSDB.update(
MainscreenActivity.TABLE_Name, values, "ID=" + id, null);
values.put(MainscreenActivity.COL_Set_Default, 0);
Toast.makeText(getBaseContext(), temp0 + " is set as Default",
Toast.LENGTH_LONG).show();
MainscreenActivity.JEEMAAndroSMSDB.update(
MainscreenActivity.TABLE_Name, values, "ID!=" + id, null);
}
if (menuItemName.equalsIgnoreCase("Delete")) {
MainscreenActivity.JEEMAAndroSMSDB.delete(
MainscreenActivity.TABLE_Name, "ID=" + id, null);
Toast.makeText(getBaseContext(), "API Deleted Successfully",
Toast.LENGTH_SHORT).show();
retrievedb();
}
return true;
}
答案 0 :(得分:5)
您可以在删除项目后刷新列表
YourListView.setAdapter(YourAdapter); // optional
YourAdapter.notifyDataSetChanged(); // mandatory
享受
修改:
最佳方式:(第一种解决方案不伤害)
listItems.remove(position);
listViewAdapter.notifyDataSetChanged();
答案 1 :(得分:4)
尝试从listItems(List)
中删除该项目,然后调用
adapter.notifyDataSetChanged();
答案 2 :(得分:2)
这个
有两种方法1) Once after your Item from list view call the methos adapter.notifyChanged();
(or)
2) create a method that append data to the listview, after delete the record call that method again. its just another way of refreshing listview
答案 3 :(得分:1)
您是否尝试在UI线程上执行此操作:
listview.remove(position);
runOnUiThread(new Runnable() {
public void run() {
adapter.notifyDataSetChanged();
}
});
这可能有所帮助。
答案 4 :(得分:1)
首先从arraylist中删除一个项目,您可以通过
将列表填充到列表视图中arraylist.remove(position);
然后致电
listview.invalidateViews();