Reputation: 1165
So i'm learning android development and i started my first project a few weeks ago. Since then i've hit a few roadblocks but i've gotten past them. This one is different.
I'm trying to make it so when the user touches and holds on an item in a listview a dialogue boc pops up asking if they are sure and on Yes click it will delete the item. Simple enough right?
well here's my code:
//passwordList is a Set<String> and so is passList
final Set<String> passwordList = mPrefs.getStringSet("pass", new HashSet<String>());
if (passwordList != null) {
passList = passwordList;
listFinal = passList.toArray(new String[passList.size()]);
list2 = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listFinal);
passwordListView.setAdapter(list2);
//onClickListener for the main list
passwordListView.setLongClickable(true);
passwordListView.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View strings, final int i, long l) {
AlertDialog.Builder deleteConf = new AlertDialog.Builder(main.this);
final int positionToRemove = i;
deleteConf.setTitle("Delete Password?");
deleteConf.setMessage("Are you sure you want to delete the password '" +listFinal[i] +"'?");
deleteConf.setNegativeButton("No", null);
deleteConf.setPositiveButton("Yes", new AlertDialog.OnClickListener() {
public void onClick(DialogInterface dialog, int w) {
Log.d("dialog", "Pressed YES");
passwordList.remove(positionToRemove);
ArrayList<String> list = new ArrayList<String>(Arrays.asList(listFinal));
//Is this right?
passList.remove(positionToRemove);
//Or this one?
list.remove(positionToRemove);
list2.notifyDataSetChanged();
list2.notifyDataSetInvalidated();
}
});
deleteConf.show();
return false;
}
});
}
I feel like i'm not removing the item from the correct array. Since the Adapter source is set to listFinal I should remove the item from there right? but it won't let me call listFinal.remove(positionToRemove);
Can anyone help me figure out what's wrong here? the dialogue box shows but when I click yes nothing happens. It knows that i pressed yes (hence the log.d pressed YES) but the item is never removed.
Upvotes: 0
Views: 780
Reputation: 2840
First thing that i noticed is you have 2 objects: list and list2 and u call list.remove(); list2.notifyDataSetChanged();
I dont see the rest of the code but this might be the case.
Upvotes: 1