//this populates the list view
@Override
protected void onResume() {
super.onResume();
main_car_list.setAdapter(null);
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 position, long l) { //tabbed automatically filled in this
String fileName = ((Cars)main_car_list.getItemAtPosition(position)).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
// this passes car name and model to CarDetails
viewCar.putExtra("Car_File", fileName); //Car_File is a key (like naming the file)
startActivity(viewCar);
}
});
}
}
//for context menu (long press menu) have to add registerForContextMenu(main_car_list) in the above list view
// @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);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId()) {
case R.id.menu_deleteItem:
// Toast.makeText(this,"clicked"+ca.getItem(info.position), Toast.LENGTH_SHORT).show();
//works!!!!!!
Utilities.deleteCar(getApplicationContext(), ((Cars)main_car_list.getItemAtPosition(info.position)).getDateTime() + Utilities.FILE_EXTENSION);
//above line deletes the file, next 2 lines removes it from the adapter (makes it go away and refreshes the list view)
cars.remove(info.position);
ca.notifyDataSetChanged();
return true;
default:
return super.onContextItemSelected(item);
}
}