有没有办法通过只知道位置来删除listView
中的项目?它是listview
还是adapter
的属性?请参阅下面的图片,该图片演示了ListView
Updat_1
问题仍然存在。我使用从Listview
检索到的数据填充SQlite DataBase
。检索到的数据将从Array
String[] retrieveMPLNames()
返回,然后我将此Array
转换为ArrayList
,然后将ArrayList
绑定到ArrayAdapter
。当我想要删除时,首先,我从SQlite DataBase
删除该项目,然后再次执行相同的过程,即调用String[] retrieveMPLNames()
,它将从SQlite DataBase
中检索可用数据,返回的Array
将转换为ArrayList
,然后将其绑定到ArrayAdapter
。但是,该项永远不会从ListView
中删除。请参阅下面的代码。
Java_Code:
if (isDBEmpty()) {
Log.i(TAG, "@onCreate(): The DataBase Is Empty");
}else {
Log.i(TAG, "@onCreate(): The DataBase Is Not Empty");
bindArrayListToArrayAdapter(arrayToArrayList(retrieveMPLNames()));
....
....
protected void deleteItemFromDataBase(int itemClickedPos) {
// TODO Auto-generated method stub
Log.i(TAG, "@deleteItemFromDataBase():");
if(! sqliteDB.isOpen()) {
//mplOpenHelperDB = new MPLDataBase(getApplicationContext());
sqliteDB = mplOpenHelperDB.getWritableDatabase();
Log.i(TAG, "@deleteItemFromDataBase(): The Database was closed And Now It Is Opened");
}
String locName = mplOpenHelperDB.getLocationName((itemClickedPos+1));
int []x = mplOpenHelperDB.getIDs();
Log.i(TAG, "@deleteItemFromDataBase(): total rows in the database = "+mplOpenHelperDB.getCurrentRowNumber());
Log.i(TAG, "@deleteItemFromDataBase(): name to delete from database = "+locName);
Log.i(TAG, "@deleteItemFromDataBase(): ID of the item to delete from the database= "+x[itemClickedPos+1]);
mplOpenHelperDB.deleteRow((itemClickedPos+1));
Log.i(TAG, "@deleteItemFromDataBase(): total rows in the database after delete = "+mplOpenHelperDB.getCurrentRowNumber());
notifyArrayAdapter();
}
private void notifyArrayAdapter() {
// TODO Auto-generated method stub
Log.i(TAG, "@notifyArrayAdapter(): ");
mArrayAdapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.savedlocationlist_layout,
R.id.savedLocationName, arrayToArrayList(retrieveMPLNames()));
mListView.setAdapter(mArrayAdapter);
mArrayAdapter.notifyDataSetChanged();
}
private String [] retrieveMPLNames() {
// TODO Auto-generated method stub
Log.d(TAG, "@retrieveMPLNames(): Retrieving MPLNames");
if (! sqliteDB.isOpen()) {
Log.d(TAG, "@retrieveMPLNames(): Your DataBase Was Closed And It Will Be Opened Now");
sqliteDB = mplOpenHelperDB.getWritableDatabase();
}
Log.d(TAG, "@retrieveMPLNames(): Your DataBase Is Opened");
int []dbIDs = mplOpenHelperDB.getIDs();
String []str = new String[dbIDs.length];
for(int i=0; i<dbIDs.length; i++) {
str[i] = mplOpenHelperDB.getLocationName(dbIDs[i]);
Log.d(TAG, "@retrieveMPLNames(): "+str[i]);
Log.d(TAG, "@retrieveMPLNames(): Total Rows Retrieved = "+str.length);
}
return str;
}
答案 0 :(得分:1)
您需要从基础数据中删除该项,即数组,游标,无论它是什么,然后更新ListView以反映更改。
答案 1 :(得分:1)
要从ListView
(名为listview
)中删除项目,当用户点击该特定项目时,请执行以下操作:
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
final int positionToRemove = position;
public void onClick(DialogInterface dialog, int which) {
listview.remove(positionToRemove);
adapter.notifyDataSetChanged();
}});
}
});