Android, DBFlow fill object by selecting statement

1.2k Views Asked by At

this is my db:

@Database(name = GamersDatabase.NAME, version = GamersDatabase.version)
public class GamersDatabase {
        public static final String NAME = "and_roid";
        public static final int version = 1;
}

and this is my table:

@Table(database = GamersDatabase.class)
public class Gamers extends BaseModel{
        @PrimaryKey(autoincrement = true)
        int id;

        @Column@NotNull
        String name;
 }

this is my insert statement that works good (because the select statement below returns correct list size):

TransactionManager.getInstance().addTransaction(new SaveModelTransaction<>(ProcessModelInfo.withModels()));
//insert          
SQLite.insert(Gamers.class).columns(Gamers_Table.name.getDefinition() ).values("ali").execute();

this is my select statement :

 List<Gamers> w = SQLite.select().from(Gamers.class).where(Gamers_Table.fName.eq("ali")).queryList();
 for (Gamers o : w) {
        // The problem is this part that I can't get gamer's name
        Toast.makeText(MainActivity.this, ""+o.toString(), Toast.LENGTH_SHORT).show();
 }

so if DBFlow is an ORM system, how can I get my object and get it's behavior as a gamer object that we have in normal sqlite cursor method? if the question is not clear I should add comment:

in old fashion database method, we use SQLite queries like this:

Cursor c = db.getRawQuery("select * from tbl_gamers" , null);
if(c.moveToFirst()){
// fill object
}

and then use our filled object, Now how can I have access to DBFlow retrieved object's fields (like name and id)?

2

There are 2 best solutions below

0
On

I'm quire confused that you're using both a different table and field name in your select query. It works fine for me if I'm using the declared values of your Workers table.

List<Workers> workers = SQLite.select()
             .from(Workers.class)           
             .where(Workers_Table.name.eq("ali"))                    
             .queryList(); 

But I guess that's not the point. Maybe your problem is that the fields of the Workers class are package-local. Simply make them public or provide public getters to access them.

@Table(database = GamersDatabase.class)
public class Workers extends BaseModel{
        @PrimaryKey(autoincrement = true)
        public int id;

        @Column@NotNull
        public String name;
 }

Please note that's I'm also not very familiar with DBFlow but I think your TransactionManager doesn't do anything. According to the documentation at here you should pass a list of models that you want to save. After some problems with using SaveModelsTransaction using the following code worked fine for me:

 TransactionManager.transact(GamersDatabase.NAME, new Runnable() {
        @Override
        public void run() {
            Workers worker = new Workers();
            worker.name = "ali";
            worker.save();
        }
    });

Hope, this can help you.

Regards,

Alex

0
On

I struggled with this too. The list here contains a kind of pointers or location of objects stored in memory (I am not sure though) like:

Workers List: 
           [com.example.workers.receiver.schema@1a4b1622, 
           com.example.workers.receiver.schema@1302f8b3, 
           com.example.workers.receiver.schema@d7c9e70,]

Above is the output on terminal using:

Log.d(TAG, "Workers List: "+w);

You'll be able to print the data in the log using:

List<Gamers> w = SQLite.select()
                    .from(Gamers.class)
                    .where(Gamers_Table.name.eq("ali"))
                    .queryList();
for (int i=0;i<w.size();i++) {
    Log.d(TAG, ""+w.get(i).name);
}

The above code runs if your @Column elements are public. If they are private you need to make suitable getter and setter functions for them and you can use the following code to retrieve data:

List<Gamers> w = SQLite.select()
                    .from(Gamers.class)
                    .where(Gamers_Table.name.eq("ali"))
                    .queryList();
for (int i=0;i<w.size();i++) {
    Log.d(TAG, ""+w.get(i).getName());
}

Assuming getName() is a getter function. You can return the List w and have more usage.