Doesn’t List View exclude the first time?

Asked

Viewed 56 times

0

Hello, my Listview is not deleting right, because I click on the item, open the menu, I click delete, it leaves the Listview, but it’s still in the database, so I have to exit the screen and go back to it, the item will appear again, but this second time if I click delete, it leaves 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 that and the exclusion from the bank :

public void Excluir(ListDesp pValue) {
    long id = pValue.getId();
    database.delete(CriaDespesas.TABELA, CriaDespesas.DESP_ID + " = " + id, null);
}

1 answer

0

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

Try something like that:

public void Excluir(ListDesp pValue) {
    long id = pValue.getId();
    database.beginTransaction();
    database.delete(CriaDespesas.TABELA, CriaDespesas.DESP_ID + " = " + id, null);
    database.setTransactionSuccessful();
    database.endTransaction();
}
  • didn’t work..

Browser other questions tagged

You are not signed in. Login or sign up in order to post.