I have a custom arrayadapter I'm able to create my listview and add items to it. I create a context menu to delete the item that is long pressed on, the context menu launches I choose delete the item goes away (I celebrate) I restart the app and the item comes back (I scream). here's the context menu with my "delete" code
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId()){
case R.id.menu_deleteItem:
ca.remove(ca.getItem(info.position));
ca.notifyDataSetChanged();
return true;
default:
return super.onContextItemSelected(item);
}
}
at the top of the Activity I declared CarAdapter ca;
and CarAdapter is my custom array
here is the on create
//this populates the list view
@Override
protected void onResume() {
super.onResume();
main_car_list.setAdapter(null);
ArrayList<Cars> cars = Utilities.getAllSavedCars(this);
//for context menu
registerForContextMenu(main_car_list);
if(cars == null || cars.size() == 0){
Toast.makeText(this,"you have no cars added", Toast.LENGTH_SHORT).show();
return;
}else{
//removed CarAdapter ca and moved CarAdapter up top so could use to delete items
ca = new CarAdapter(this, R.layout.car_list, cars); //from caradapter
main_car_list.setAdapter(ca);
//when click item(car) opens new activity
main_car_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int postion, long l) { //tabbed automatically filled in this
String fileName = ((Cars)main_car_list.getItemAtPosition(postion)).getDateTime() + Utilities.FILE_EXTENSION; //gets filename
Intent viewCar = new Intent(getApplicationContext(),CarDetails.class); //when clicked on item in list opens deails
//this is how pass data between activities
viewCar.putExtra("Car_File", fileName); //Car_File is a key (like naming the file)
startActivity(viewCar);
}
});
}
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
}
my delete car method from my Utilities Activity
//deletes car
public static boolean deleteCar(Context context, String fileName){
File dir = context.getFilesDir();
File filename = new File(dir,fileName);
if(filename.exists()){
filename.delete();
}
return true;
}
Your cars
list is loaded here
ArrayList<Cars> cars = Utilities.getAllSavedCars(this);
But when you delete, you delete only from the adapter.
You have to delete from the ArrayList of Cars that was created.
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId()){
case R.id.menu_deleteItem:
ca.remove(ca.getItem(info.position));
ca.notifyDataSetChanged();
//delete from my storage
Utilities.deleteCar(ca.getItem(info.position));
return true;
default:
return super.onContextItemSelected(item);
}
}