Database and Listview do not work

Asked

Viewed 49 times

1

I’m trying to create an app that records data and displays it in a Listview, and then gradually sophisticate the app, but when it comes to saving the data in the database, it doesn’t save. In Debug it runs everything right, in the last line, as it is in the image below, out of nowhere it returns "false". The strange thing is that he seems to be searching for the data. And the id always returns "0" I don’t understand why.

inserir a descrição da imagem aqui

Then I also discovered that my Arraylist doesn’t seem to work either. Every time I go to the part of my app to show recorded data (The Fragment that corresponds to Listview) the app closes itself. Using Debug I discovered two details, one in Datasource and the other in Fragment where I program to show the data that is saved. Normally I would think that since there is no recorded data, then it will not work. But I think the app should not close alone, at most it should only show a blank screen.

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

The weirdest thing is that I’ve worked with another application using virtually the same code templates, so I don’t understand why so many problems.

  • Show the code of the Insert.

  • public Boolean Insert(String table, Contentvalues data) { Boolean success = true; Try { success = db.Insert(table, null, data) > 0; } catch (Exception e) { success = false; } success; }

1 answer

1

Try this code it puts the insert operation within a data transaction context that is opened with the method beginTransaction() is the data is passed to the database by the method setTransactionSuccessful() and the transaction context is closed by the method endTransaction()

public boolean insert(String tabela, ContentValues dados) { 
   boolean sucesso = true; 
   //Abre um contexto de transação.
   db.beginTransaction();
   try { 
         // >= pois a tabela pode estar vazia e rowID pode ser 0;
         sucesso = db.insert(tabela, null, dados) >= 0; 
         // Valida o contexto de transação.
         db.setTransactionSuccessful();
   } catch (Exception e) { 
         //  Seria interessante fazer um log dessa exceção 
         //para saber se está havendo erro de comunicação com o banco de dados
         sucesso = false; 
   } finally {
     // Finaliza o contexto de transação.
     db.endTransaction();
   }
   return sucesso;
}
  • 1

    The error persists. I think I’ll redo all the MVC giving Ctrl C on the other project I did that worked and + Ctrl V on this to see if it will work, changing only the gets and sets and table attributes. 'Cause there’s no point, I’m not.

Browser other questions tagged

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