I have a listview of strings.
I want to remove a deleted item.
In the code below, the item is deleted in the db(closing and reopening confirms this).
Here where i want deleted item removed
@Override
public boolean onContextItemSelected(MenuItem item) {
// TODO Auto-generated method stub
int menuItemIndex = item.getItemId();
final AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo) item.getMenuInfo();
final int pos = menuInfo.position;
String menuItemName =menuItems[menuItemIndex];
if(menuItemName=="Delete")
{
try{
Locations l= (Locations)locations.get(pos);
int id= l.get_id();
if( db.deleteLocation(id))
{
locations.remove(pos);
arrayAdapter.notifyDataSetChanged();
}else
{
Toast.makeText(this,"Can not delet a location with apartment blocks",Toast.LENGTH_LONG).show();
}
}catch(Exception e)
{
e.printStackTrace();
}
}
return super.onContextItemSelected(item);
}
This the code for the whole activity.
NB the item is deleted in the db but remain displayed till i close and re open the list.
package com.example.metermanager;
import java.util.ArrayList;
import java.util.List;
import meter.manager.helper.DatabaseHelperClass;
import meters.model.Locations;
import android.app.Activity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.AdapterContextMenuInfo;
public class ApartmentLocations extends Activity {
private DatabaseHelperClass db;
private List<Locations> locations= new ArrayList<Locations>();
String[] menuItems = {"Delete"};
ArrayAdapter<String> arrayAdapter ;
ListView list;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.appartment_locations);
db= DatabaseHelperClass.getInstance(this);
list= (ListView) findViewById(R.id.apartments_locations);
locations=db.GetLocations();
List <String> s = new ArrayList<String>();
for (Locations l:locations){
s.add(l.toString());
}
arrayAdapter = new ArrayAdapter<String>(
this,
android.R.layout.simple_list_item_1,
s );
list.setAdapter(arrayAdapter);
registerForContextMenu(list);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
if (v.getId()==R.id.apartments_locations) {
menu.setHeaderTitle("Choose a menu item");
for (int i = 0; i<menuItems.length; i++) {
menu.add(Menu.NONE, i, i, menuItems[i]);
}
}
}
@Override
public boolean onContextItemSelected(MenuItem item) {
// TODO Auto-generated method stub
int menuItemIndex = item.getItemId();
final AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo) item.getMenuInfo();
final int pos = menuInfo.position;
String menuItemName =menuItems[menuItemIndex];
if(menuItemName=="Delete")
{
try{
Locations l= (Locations)locations.get(pos);
int id= l.get_id();
if( db.deleteLocation(id))
{
locations.remove(pos);
arrayAdapter.notifyDataSetChanged();
}else
{
Toast.makeText(this,"Can not delet a location with apartment blocks",Toast.LENGTH_LONG).show();
}
}catch(Exception e)
{
e.printStackTrace();
}
}
return super.onContextItemSelected(item);
}
}
Any clues?
Ronald