Save a list to Sqlite

Asked

Viewed 253 times

0

I have a list of products and use recycleViwer to list them. I want to take these products and save them in the database. Being honest, I can’t do it! I’m researching but I can’t find content.

I’m at my Activity where the products are and I’ll pass them to a FragmentDialog.

private void abrirFormularioDialogCompra() {
    Bundle argumentos = new Bundle();
    FormularioCompraDialog formularioCompraDialog = new FormularioCompraDialog();
    argumentos.putSerializable("valor", valorCarrinho);
    **List<Produto> produtosSelecionados = getProdutos();
    argumentos.putSerializable("listaProdutos", (Serializable)** produtosSelecionados);
    formularioCompraDialog.setArguments(argumentos);
    formularioCompraDialog.show(getSupportFragmentManager(), "dialog");
}

Receiving:

private void pegaOsProdutos() {
    assert getArguments() != null;
    **Serializable listaProdutos = getArguments().getSerializable("listaProdutos");**
}

How I get these products now and save them on Sqlite ???

1 answer

0


Friend, I did not understand right if your doubt is to do any manipulation (select, Insert and update) in the android database (sqlite) or if you know how to do this but only this with problem because the data you want to save is in a list.

Let’s go in parts then, if your problem is how to open a connection with the bank and do the manipulations, first you will need to create a class and inherit from Sqliteopenhelper:

public class minhaclasse extends SQLiteOpenHelper{
   @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {

    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

    }
}

As you can see in the example above, there will be 2 methods for you to overwrite public void onCreate() and public void onUpgrade().
This is the class that will be responsible for creating the bank and opening connections with it.
After it will be necessary to create your classes for data persistence (DAO), I will not put the code of these in the answer because it would be very extensive and each programmer/ project will do/be in a way, what is not lacking are examples of this on the internet, can search for CRUD Android, but I’ll leave this example that I believe is very easy to understand.

Now if you already know how to open connections with the database, do Insert, delete and update and your question is how to do this with the data in List, the answer is to loop this list and for each iteration you call your function of Insert in the database by passing the parameters, for example id_carrinho, id_produto, valor.

Edited
First let’s just confirm a few things:

1 - The object class you are sending has to implement Serializable, thus:

public class Produto implements Serializable{

  //Seu código da classe produto

}

2 - In the class that is sending the data, you need to use ArrayList in place of List. (you will probably have to change your getProducts() method to return Arraylist)

ArrayList<Produto> produtosSelecionados = getProdutos();  
argumentos.putSerializable("listaProdutos", (Serializable) produtosSelecionados);

3 - At the time of receiving the data you already convert to one ArrayList<Produto>.

ArrayList<Produto> listaProdutos = (ArrayList<Produto>) getArguments().getSerializable("listaProdutos");

Now that you have the data in Arraylist format again you can scroll through them with a foreach and save them like this:

for(Produto produto : listaProdutos){
  metodoInsertBD(produto);
}
  • My friend thank you very much for your message. The question is precisely to make an insertion with the data that are on the List. Attributes are description, price and quantity. About the bank I have the knowledge in the full CRUD and it is even done right in the app, but this lis that caught. You said just make a loog and call the function of Insert in the database that contains the data fields that are in the list. Could you show me an example of this, my dear? I would appreciate it, but I’m already trying something here

  • @Developd, I get it, I’ll edit my answer and include how you can do it!

  • @Developd, I think that now the example is closer to your need.

  • Perfect my friend, I believe that already opened me much the fan rsrsrs. Now I just need to save these products in the bank with the table I created, I just need to take the Id of the purchase along rsrs, because for each purchase, has its certain products. Pinching a difficulty in this you help me? May God bless you my dear, thank you very much

  • @Developd, of course, needing only to call! Just remembering that if the answer helped you with your problem don’t forget to mark it as correct to help others with the same doubt!!

  • Perfect my dear, marked =)

Show 1 more comment

Browser other questions tagged

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