简体   繁体   中英

Unable to delete item from List View using context menu in Android

I have custom list view which contains name and username of employees. I am trying to delete any employee. I have use the context menu to delete the employee. But I didn't get Id of selected Item in list View. here is my EmployeeDatabaseHelper class which contains deleteEmployee Method

EmployeeDatabaseHelper .java

public void deleteEmployee(String id){

    SQLiteDatabase db = dbhelper.getReadableDatabase();
    int delId = db.delete(TABLE_NAME_EMPLOYEE, id+" =? ", 
            new String[] {String.valueOf(id)});
    db.close();
}

Here I am using context menu to delete employee

EmployeeFragment.java

public void onCreateContextMenu(ContextMenu menu, View v,
        ContextMenuInfo menuInfo) {

    menu.setHeaderTitle("Select Action");
    menu.add(0,v.getId(),0,"Edit");
    menu.add(0,v.getId(),0,"Delete");
}

public boolean onContextItemSelected(MenuItem item) {

    if (item.getTitle() == "Edit"){
        //ToDo edit employee Code
    } else if (item.getTitle() == "Delete") {

        new AlertDialog.Builder(getActivity())
        .setTitle("Delete")
        .setMessage("Are you sure you want to delete this Employee ?")
        .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {

                dbHelper.deleteEmployee(id);
                //id is not getting here. (My issue)
            }
        })
        .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        }).show();

    } else {
        return false;
    }
    return true;
}

Here is my model class Employee

Employee.java

public class Employee {
    public String username;
    public String name;
    public String password;     
}

如果要删除ListView Item,则应从适配器中删除该ListView所使用的Item。

See if you want to delete item from ListView using onCreateContextMenu you can do like this:

int currentposition;

listview.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
            ContextMenuInfo menuInfo) {
        // TODO Auto-generated method stub
        AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
        currentposition = info.position;
        menu.setHeaderTitle("Choose");
        menu.add(0, v.getId(), 0, "Delete ");

    }

});

Now use this:

public boolean onContextItemSelected(MenuItem item) {
    AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
            .getMenuInfo();
    if (item.getTitle() == "Delete ") {
        mArray.remove(currentposition);
        ca.notifyDataSetChanged();
        Toast.makeText(this, "Deleted ", Toast.LENGTH_SHORT)
                .show();
    } 
    return true;
}

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