List View does not delete the first time?

0
Hello, my ListView is not deleting right, because I click on the item, open the menu, I click delete, it exits the ListView , but continues on the then I have to exit the screen and return to it, the item will appear again, however the second time if I click delete, it exits the ListView and the database. >

This is the menu code:

public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {        
    try
    {
        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
        if (!blnShort)
        {
            Posicao = info.position;
        }
        blnShort = false;

        menu.setHeaderTitle("Selecione:");            

        String[] menuItems = getResources().getStringArray(R.array.menu);             
        for (int i = 0; i<menuItems.length; i++) {                
            menu.add(Menu.NONE, i, i, menuItems[i]);  

        }        
    }catch (Exception e) {
        trace("Erro : " + e.getMessage());
    }            
}  

@Override   
public boolean onContextItemSelected(MenuItem item) {
    ListDesp meuItem = null;
    try
    {   
        int menuItemIndex = item.getItemId(); 
        meuItem = (ListDesp) getListAdapter().getItem(Posicao);

        if (menuItemIndex == 0){

            Intent it = new Intent(this, CadDesp.class);
            it.putExtra("tipo", ALTERAR);
            it.putExtra("dadosLista", meuItem);
            startActivityForResult(it, ALTERAR); 

        }else if (menuItemIndex == 1){

            buscaDespesas.Excluir(meuItem);
            listas.remove(meuItem);

            adapter.notifyDataSetChanged();

        }
    }catch (Exception e) {
        trace("Erro : " + e.getMessage());
    }   
    return true;   

}   

And this is the exclude from the bank:

public void Excluir(ListDesp pValue) {
    long id = pValue.getId();
    database.delete(CriaDespesas.TABELA, CriaDespesas.DESP_ID + " = " + id, null);
}
    
asked by anonymous 27.05.2015 / 17:56

1 answer

0

You have to do the transaction control when doing some operation that modifies the database.

Try something like this:

public void Excluir(ListDesp pValue) {
    long id = pValue.getId();
    database.beginTransaction();
    database.delete(CriaDespesas.TABELA, CriaDespesas.DESP_ID + " = " + id, null);
    database.setTransactionSuccessful();
    database.endTransaction();
}
    
27.05.2015 / 18:17