Save an ArrayList to an SQLITE table

-1

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 "replies" field to save (insert) this ArrayList: boolean[] radioButtonValues = new boolean[3];

Someone to help me? please?

    
asked by anonymous 06.07.2017 / 20:13

1 answer

2

Using the new Android persistence architecture, 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"

Answer.java

@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.
}

Answer.java

@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();
}

Activity.java

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

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

Any questions, let's talk, but the documentation is pretty cool Here

    
06.07.2017 / 21:30