File tree Expand file tree Collapse file tree 5 files changed +111
-0
lines changed
app/src/main/java/com/sample Expand file tree Collapse file tree 5 files changed +111
-0
lines changed Original file line number Diff line number Diff line change 3131import com .sample .database .ContactDBHelper ;
3232import com .sample .database .ExtTestDBHelper ;
3333import com .sample .database .PersonDBHelper ;
34+ import com .sample .database .room .User ;
35+ import com .sample .database .room .UserDBHelper ;
3436import com .sample .utils .Utils ;
3537
38+ import java .util .ArrayList ;
3639import java .util .HashSet ;
40+ import java .util .List ;
3741import java .util .Set ;
3842
3943public class MainActivity extends AppCompatActivity {
@@ -108,6 +112,19 @@ protected void onCreate(Bundle savedInstanceState) {
108112 }
109113 }
110114
115+ // Room database
116+ UserDBHelper userDBHelper = new UserDBHelper (getApplicationContext ());
117+ if (userDBHelper .count () == 0 ) {
118+ List <User > userList = new ArrayList <>();
119+ for (int i = 0 ; i < 20 ; i ++) {
120+ User user = new User ();
121+ user .id = (long ) (i + 1 );
122+ user .name = "user_" + i ;
123+ userList .add (user );
124+ }
125+ userDBHelper .insertUser (userList );
126+ }
127+
111128 Utils .setCustomDatabaseFiles (getApplicationContext ());
112129 }
113130
Original file line number Diff line number Diff line change 1+ package com .sample .database .room ;
2+
3+ import android .arch .persistence .room .Database ;
4+ import android .arch .persistence .room .RoomDatabase ;
5+
6+ /**
7+ * Created by anandgaurav on 12/02/18.
8+ */
9+ @ Database (entities = {User .class }, version = 1 )
10+ public abstract class AppDatabase extends RoomDatabase {
11+
12+ public abstract UserDao userDao ();
13+
14+ }
Original file line number Diff line number Diff line change 1+ package com .sample .database .room ;
2+
3+ import android .arch .persistence .room .Entity ;
4+ import android .arch .persistence .room .PrimaryKey ;
5+
6+ /**
7+ * Created by anandgaurav on 12/02/18.
8+ */
9+ @ Entity (tableName = "users" )
10+ public class User {
11+
12+ @ PrimaryKey
13+ public Long id ;
14+
15+ public String name ;
16+
17+ }
Original file line number Diff line number Diff line change 1+ package com .sample .database .room ;
2+
3+ import android .arch .persistence .room .Room ;
4+ import android .content .Context ;
5+
6+ import java .util .List ;
7+
8+ /**
9+ * Created by anandgaurav on 12/02/18.
10+ */
11+
12+ public class UserDBHelper {
13+
14+ private final AppDatabase appDatabase ;
15+
16+ public UserDBHelper (Context context ) {
17+ appDatabase = Room .databaseBuilder (context , AppDatabase .class , "User-Database" )
18+ .allowMainThreadQueries ()
19+ .build ();
20+ }
21+
22+ public void insertUser (List <User > userList ) {
23+ appDatabase .userDao ().insertAll (userList );
24+ }
25+
26+ public int count () {
27+ return appDatabase .userDao ().loadAll ().size ();
28+ }
29+
30+ }
Original file line number Diff line number Diff line change 1+ package com .sample .database .room ;
2+
3+ import android .arch .persistence .room .Dao ;
4+ import android .arch .persistence .room .Delete ;
5+ import android .arch .persistence .room .Insert ;
6+ import android .arch .persistence .room .OnConflictStrategy ;
7+ import android .arch .persistence .room .Query ;
8+
9+ import java .util .List ;
10+
11+ /**
12+ * Created by anandgaurav on 12/02/18.
13+ */
14+
15+ @ Dao
16+ public interface UserDao {
17+
18+ @ Query ("SELECT * FROM users" )
19+ List <User > loadAll ();
20+
21+ @ Query ("SELECT * FROM users WHERE id IN (:userIds)" )
22+ List <User > loadAllByIds (List <Integer > userIds );
23+
24+ @ Insert (onConflict = OnConflictStrategy .REPLACE )
25+ void insert (User user );
26+
27+ @ Insert (onConflict = OnConflictStrategy .REPLACE )
28+ void insertAll (List <User > users );
29+
30+ @ Delete
31+ void delete (User user );
32+
33+ }
You can’t perform that action at this time.
0 commit comments