Save an Arraylist<> to an SQLITE table

Asked

Viewed 818 times

-1

Speaks Galera, I’m wanting to save an array list in a table in SQLITE, can I do it? If yes, how?

This is my table:

db.execSQL("create table amc (_id integer primary key autoincrement,tipo text not null, resultado float not null,data date not null, id_usuario integer not null, FOREIGN KEY (id_usuario) REFERENCES usuarios (_id);");

I want to create a field "answers" to save (Insert) this Arraylist: boolean[] radioButtonValues = new boolean[3];

Someone to help me? Please?

  • 1

    If you have date this is not Sqlite. Just like the date, you will have to serialize in string and record undo.

  • Take a look at the new Android data persistence architecture, it might help you. Try using Room. If I want to put an example there, hug!! https://developer.android.com/topic/libraries/architecture/room.html

  • @Arthurcordovastapassoli would like you to give an example, please?

  • Sure, I’ll ride here and post

  • When one starts badly the tendency is for problems to multiply. If one were to implement the suggestion given in this reply would only have to keep a value.

  • @ramaral but anyway I will have to use this method. I have a listview with 3 Radiobutton in each position of listview. And I need to save in the bank the selection of each Listview Radiobutton.

  • Are you no longer saving the other information? Just save this one more (another field in the table).

  • @ramaral yes yes, but the others are a unique field, as name, type and hired. In this table field I need to save the answers.

  • You can’t understand it. It’s better to ask another question in full detail. What is an answer? It makes no sense to say "they are a unique field, as name, type and hired" and "In this field of the table I need to save the answers".

Show 4 more comments

1 answer

2


Using the new Android persistence file, Room : First you need to import dependencies:

build.Gradle

allprojects {
    repositories {
        jcenter()
        maven { url 'https://maven.google.com' }
    }
}


compile "android.arch.persistence.room:runtime:1.0.0-alpha3"
annotationProcessor "android.arch.persistence.room:compiler:1.0.0-alpha3"

Java response.

@Entity
public class Resposta {
    @PrimaryKey
    private int id;

    @ColumnInfo(name = "questao")
    private String questao

    @ColumnInfo(name = "is_check")
    private Boolean isCheck;

    // Getters and setters are ignored for brevity,
    // but they're required for Room to work.
}

Java response.

@Dao
public interface RespostaDao {    
    @Query("SELECT * FROM resposta")
    List<User> getAll();

    @Insert
    void insert(Resposta resposta);

    @Delete
    void delete(Resposta resposta);
}

Appdatabase.java

@Database(entities = {Resposta.class}, version = 1)
public abstract class AppDatabase extends RoomDatabase {
    public abstract RespostaDao respostaDao();
}

Java activity.

...
    AppDatabase db = Room.databaseBuilder(getApplicationContext(),
    AppDatabase.class, "database-name").build();

    List<Resposta> respostas = metodoQuePovoaSuaLista();
    for(Resposta r : respostas){
        db.respostaDao().insert(r);
    }
...

Any doubt we talked, but the documentation is very cool Here

Browser other questions tagged

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