0

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

user3079559
  • 417
  • 5
  • 16
  • 1
    possible duplicate of [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Nabin Oct 24 '14 at 08:55
  • Calling `arrayAdapter.notifyDataSerChanged()` assumes that you update the list with data by yourself. The adapter just draws the views again. In other words, reload the list from the database after you deleted an item. – Leon Joosse Oct 24 '14 at 08:55
  • 1
    if(menuItemName=="Delete") to if(menuItemName.equals("Delete")) – Nabin Oct 24 '14 at 08:55

3 Answers3

1

You can use two things for this to work :

  1. When you use arrayAdapter.notifyDataSetChanged(); then it only works when the data(list) that is passed in the adapter is being chnaged. so before using this you shoud remove the data from passed arraylist in your case that s(list).

So You should declare the list variable s globally and then remove the item when needed like :

     locations.remove(pos);
     s.remove(pos);
     arrayAdapter.notifyDataSetChanged();

OR 2. You can use the SimpleCursorAdapter that will suite in your context
http://developer.android.com/reference/android/widget/SimpleCursorAdapter.html

And also , I found one more mistake :
if(menuItemName=="Delete")
we can not compare Strings in java like this So plz change it to as follow :
if(menuItemName.equals("Delete"))

Nitesh Singh
  • 328
  • 3
  • 13
0

Use a CursorAdapter instead of ArrayAdapter. More info: http://developer.android.com/reference/android/widget/CursorAdapter.html

Federico Ponzi
  • 2,682
  • 4
  • 34
  • 60
0

just add locations.clear() after notifydatasetchanged() in ur activity where delete command is executed

Hirak Chhatbar
  • 3,159
  • 1
  • 27
  • 36