Skip to content

Commit 75e5536

Browse files
authored
room library added for SQLite (#13)
1 parent 0fd7b7e commit 75e5536

9 files changed

Lines changed: 227 additions & 11 deletions

File tree

app/build.gradle

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,11 @@ dependencies {
2828
androidTestImplementation 'com.android.support.test:runner:1.0.1'
2929
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
3030

31+
32+
//Room dependencies
33+
implementation 'android.arch.persistence.room:runtime:1.0.0'
34+
annotationProcessor 'android.arch.persistence.room:compiler:1.0.0'
3135
// butter knife
32-
compile 'com.jakewharton:butterknife:8.8.1'
36+
implementation 'com.jakewharton:butterknife:8.8.1'
3337
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
3438
}

app/src/main/java/com/example/androidtechies/majorproject/HomeScreen.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,16 @@
66
import android.view.View;
77
import android.widget.Button;
88

9+
import com.example.androidtechies.majorproject.RoomSample.AppDatabase;
10+
import com.example.androidtechies.majorproject.RoomSample.DatabaseInitializer;
11+
912
public class HomeScreen extends AppCompatActivity {
1013
public static final String HomeScreenTag = "HomeScreen";
1114
public static final Integer cseValue = 0;
1215
public static final Integer it = 0;
1316
public static final Integer ece = 0;
1417
public static final Integer eee = 0;
18+
AppDatabase appDatabase;
1519

1620
@Override
1721
protected void onCreate(Bundle savedInstanceState) {
@@ -24,12 +28,17 @@ protected void onCreate(Bundle savedInstanceState) {
2428
cse.setOnClickListener(new View.OnClickListener() {
2529
@Override
2630
public void onClick(View view) {
27-
Intent cseIntent = new Intent(HomeScreen.this, ListPage.class);
28-
cseIntent.putExtra(HomeScreenTag, cseValue);
29-
startActivity(cseIntent);
31+
// Intent cseIntent = new Intent(HomeScreen.this, ListPage.class);
32+
// cseIntent.putExtra(HomeScreenTag, cseValue);
33+
// startActivity(cseIntent);
34+
populateDatabase();
3035

3136
}
3237
});
38+
39+
40+
41+
3342
// ece.setOnClickListener(new View.OnClickListener() {
3443
// @Override
3544
// public void onClick(View view) {
@@ -55,5 +64,9 @@ public void onClick(View view) {
5564
// }
5665
// });
5766
}
67+
68+
private void populateDatabase() {
69+
DatabaseInitializer.populateAsync(AppDatabase.getAppDatabase(this));
5870
}
71+
}
5972

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.example.androidtechies.majorproject.RoomSample;
2+
3+
import android.arch.persistence.room.Database;
4+
import android.arch.persistence.room.Room;
5+
import android.arch.persistence.room.RoomDatabase;
6+
import android.content.Context;
7+
8+
9+
@Database(entities = {Project.class}, version = 1)
10+
public abstract class AppDatabase extends RoomDatabase {
11+
12+
private static AppDatabase INSTANCE;
13+
14+
public abstract ProjectDao projectDao();
15+
16+
17+
public static AppDatabase getAppDatabase(Context context) {
18+
if (INSTANCE == null) {
19+
INSTANCE =
20+
Room.databaseBuilder(context.getApplicationContext(), AppDatabase.class, "user-database")
21+
// allow queries on the main thread.
22+
// Not suggested to do on real application!
23+
.allowMainThreadQueries()
24+
.build();
25+
}
26+
return INSTANCE;
27+
}
28+
29+
public static void destroyInstance() {
30+
INSTANCE = null;
31+
}
32+
33+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.example.androidtechies.majorproject.RoomSample;
2+
3+
import android.content.Context;
4+
import android.os.AsyncTask;
5+
import android.support.annotation.NonNull;
6+
import android.util.Log;
7+
8+
import java.util.List;
9+
10+
public class DatabaseInitializer {
11+
private static final String TAG = DatabaseInitializer.class.getName();
12+
13+
14+
public static void populateAsync(@NonNull final AppDatabase db) {
15+
PopulateDbAsync task = new PopulateDbAsync(db);
16+
task.execute();
17+
}
18+
19+
public static void populateSync(@NonNull final AppDatabase db) {
20+
populateWithTestData(db);
21+
}
22+
23+
private static Project addProject (final AppDatabase db, Project project) {
24+
db.projectDao().insertAll(project);
25+
return project;
26+
}
27+
28+
private static void populateWithTestData(AppDatabase db) {
29+
30+
Project project = new Project();
31+
project.setTitleOfProject("Hinton");
32+
project.setIntroOfProject("Hinton is a fake news generator (video) platform that aims to create\n" +
33+
" awareness among the society ");
34+
project.setModulesOfProject("news");
35+
project.setProjectBranch("IT");
36+
project.setTechnologyUsed("Machine learning");
37+
addProject(db, project);
38+
39+
List<Project> projectsList = db.projectDao().getAll();
40+
41+
int count = db.projectDao().countProjects();
42+
// Log.d(DatabaseInitializer.TAG,);
43+
Log.d(DatabaseInitializer.TAG, "Branch name: " + projectsList.get(0).getProjectBranch()+ "\nNumber of rows: "+count);
44+
}
45+
46+
private static class PopulateDbAsync extends AsyncTask<Void, Void, Void> {
47+
48+
private final AppDatabase mDb;
49+
50+
PopulateDbAsync(AppDatabase db) {
51+
mDb = db;
52+
}
53+
54+
@Override
55+
protected Void doInBackground(final Void... params) {
56+
populateWithTestData(mDb);
57+
return null;
58+
}
59+
60+
}
61+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package com.example.androidtechies.majorproject.RoomSample;
2+
3+
import android.arch.persistence.room.ColumnInfo;
4+
import android.arch.persistence.room.Entity;
5+
import android.arch.persistence.room.PrimaryKey;
6+
7+
@Entity(tableName = "project_table")
8+
public class Project {
9+
10+
@PrimaryKey(autoGenerate = true)
11+
private int id;
12+
13+
@ColumnInfo(name = "branch")
14+
private String projectBranch;
15+
16+
@ColumnInfo(name = "title_project")
17+
private String titleOfProject;
18+
19+
@ColumnInfo(name = "intro_project")
20+
private String introOfProject;
21+
22+
@ColumnInfo(name = "technology_used")
23+
private String technologyUsed;
24+
25+
@ColumnInfo(name = "modules")
26+
private String modulesOfProject;
27+
28+
public int getId() {
29+
return id;
30+
}
31+
32+
public void setId(int id) {
33+
this.id = id;
34+
}
35+
36+
public String getProjectBranch() {
37+
return projectBranch;
38+
}
39+
40+
public void setProjectBranch(String projectBranch) {
41+
this.projectBranch = projectBranch;
42+
}
43+
44+
public String getTitleOfProject() {
45+
return titleOfProject;
46+
}
47+
48+
public void setTitleOfProject(String titleOfProject) {
49+
this.titleOfProject = titleOfProject;
50+
}
51+
52+
public String getIntroOfProject() {
53+
return introOfProject;
54+
}
55+
56+
public void setIntroOfProject(String introOfProject) {
57+
this.introOfProject = introOfProject;
58+
}
59+
60+
public String getTechnologyUsed() {
61+
return technologyUsed;
62+
}
63+
64+
public void setTechnologyUsed(String technologyUsed) {
65+
this.technologyUsed = technologyUsed;
66+
}
67+
68+
public String getModulesOfProject() {
69+
return modulesOfProject;
70+
}
71+
72+
public void setModulesOfProject(String modulesOfProject) {
73+
this.modulesOfProject = modulesOfProject;
74+
}
75+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.example.androidtechies.majorproject.RoomSample;
2+
3+
4+
import android.arch.persistence.room.Dao;
5+
import android.arch.persistence.room.Delete;
6+
import android.arch.persistence.room.Insert;
7+
import android.arch.persistence.room.Query;
8+
9+
import java.util.List;
10+
11+
@Dao
12+
public interface ProjectDao {
13+
14+
@Query("SELECT * FROM project_table")
15+
List<Project> getAll();
16+
17+
18+
@Query("SELECT COUNT(*) from project_table")
19+
int countProjects();
20+
21+
@Insert
22+
void insertAll(Project... users);
23+
24+
@Delete
25+
void delete(Project user);
26+
27+
}

app/src/main/res/values/colors.xml

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<resources>
3-
<color name="colorPrimary">#9E9E9E</color>
4-
<color name="colorPrimaryDark">#616161</color>
5-
<color name="colorAccent">#FF4081</color>
3+
<color name="colorPrimary">#673AB7</color>
4+
<color name="colorPrimaryDark">#512DA8</color>
5+
<color name="colorAccent">#607D8B</color>
66
<color name="black">#000000</color>
7-
7+
<color name="primary_text">#212121</color>
8+
<color name="secondary_text">#757575</color>
9+
<color name="icons">#FFFFFF</color>
10+
<color name="divider">#BDBDBD</color>
811
</resources>

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ buildscript {
77
jcenter()
88
}
99
dependencies {
10-
classpath 'com.android.tools.build:gradle:3.0.1'
10+
classpath 'com.android.tools.build:gradle:3.1.0'
1111

1212

1313
// NOTE: Do not place your application dependencies here; they belong
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
#Tue Mar 13 13:19:59 IST 2018
1+
#Tue Mar 27 20:08:43 IST 2018
22
distributionBase=GRADLE_USER_HOME
33
distributionPath=wrapper/dists
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
6-
distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip

0 commit comments

Comments
 (0)