简体   繁体   中英

How to delete an item from Listview by position?

Is there any way to delete items from listView by knowing only the position? and is it a property of the listview or the adapter ? please see the image below, which demonstrates the available properties of the ListView

在此处输入图片说明

Updat_1

The problem still persists. I populate the Listview with data retrieved from SQlite DataBase . The data retrieved is returned in an Array from String[] retrieveMPLNames() , then I convert this Array into ArrayList then bind the ArrayList to the ArrayAdapter . When I want to delete, First, I delete the item from the SQlite DataBase then go through the same procedure again, which is, calling String[] retrieveMPLNames() which will retrieve the available data from the SQlite DataBase and the returned Array will be converted into ArrayList then bind it to the ArrayAdapter . But, the item is never deleted from the ListView . Please see the Code below.

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;
    }

您需要从基础数据(即数组,光标)中删除该项目,然后再更新ListView以反映更改。

In order to remove an item from your ListView (named listview ), when the user clicks that particular item, do the following:

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();
        }});
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM