Skip to content

Commit f4b4a95

Browse files
Merge pull request #35 from amitshekhariitbhu/Development
Editing Values and Offline Support
2 parents 6ef446c + 05d12c8 commit f4b4a95

35 files changed

+2611
-497
lines changed

app/src/main/java/com/sample/MainActivity.java

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,14 @@
2525
import android.os.Bundle;
2626
import android.preference.PreferenceManager;
2727
import android.support.v7.app.AppCompatActivity;
28+
import android.view.View;
2829

30+
import com.sample.database.CarDBHelper;
2931
import com.sample.database.ContactDBHelper;
32+
import com.sample.utils.Utils;
33+
34+
import java.util.HashSet;
35+
import java.util.Set;
3036

3137
public class MainActivity extends AppCompatActivity {
3238

@@ -38,13 +44,22 @@ protected void onCreate(Bundle savedInstanceState) {
3844

3945
setContentView(R.layout.activity_main);
4046

47+
Set<String> stringSet = new HashSet<>();
48+
stringSet.add("SetOne");
49+
stringSet.add("SetTwo");
50+
stringSet.add("SetThree");
51+
4152
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
4253

4354
SharedPreferences prefsOne = getSharedPreferences("countPrefOne", Context.MODE_PRIVATE);
4455
SharedPreferences prefsTwo = getSharedPreferences("countPrefTwo", Context.MODE_PRIVATE);
4556

4657
sharedPreferences.edit().putString("testOne", "one").commit();
47-
sharedPreferences.edit().putString("testTwo", "two").commit();
58+
sharedPreferences.edit().putInt("testTwo", 2).commit();
59+
sharedPreferences.edit().putLong("testThree", 100000L).commit();
60+
sharedPreferences.edit().putFloat("testFour", 3.01F).commit();
61+
sharedPreferences.edit().putBoolean("testFive", true).commit();
62+
sharedPreferences.edit().putStringSet("testSix", stringSet).commit();
4863

4964
prefsOne.edit().putString("testOneNew", "one").commit();
5065

@@ -58,8 +73,22 @@ protected void onCreate(Bundle savedInstanceState) {
5873
String email = "email_" + i;
5974
String street = "street_" + i;
6075
String place = "place_" + i;
61-
contactDBHelper.insertContact(name, phone, email, street, place);
76+
contactDBHelper.insertContact(name, phone, email, street, null);
77+
}
78+
}
79+
80+
CarDBHelper carDBHelper = new CarDBHelper(getApplicationContext());
81+
if (carDBHelper.count() == 0) {
82+
for (int i = 0; i < 50; i++) {
83+
String name = "name_" + i;
84+
String color = "RED";
85+
float mileage = i + 10.45f;
86+
carDBHelper.insertCar(name, color, mileage);
6287
}
6388
}
6489
}
90+
91+
public void showDebugDbAddress(View view) {
92+
Utils.showDebugDBAddressLogToast(getApplicationContext());
93+
}
6594
}
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
/*
2+
*
3+
* * Copyright (C) 2016 Amit Shekhar
4+
* * Copyright (C) 2011 Android Open Source Project
5+
* *
6+
* * Licensed under the Apache License, Version 2.0 (the "License");
7+
* * you may not use this file except in compliance with the License.
8+
* * You may obtain a copy of the License at
9+
* *
10+
* * http://www.apache.org/licenses/LICENSE-2.0
11+
* *
12+
* * Unless required by applicable law or agreed to in writing, software
13+
* * distributed under the License is distributed on an "AS IS" BASIS,
14+
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* * See the License for the specific language governing permissions and
16+
* * limitations under the License.
17+
*
18+
*/
19+
20+
package com.sample.database;
21+
22+
import android.content.ContentValues;
23+
import android.content.Context;
24+
import android.database.Cursor;
25+
import android.database.DatabaseUtils;
26+
import android.database.sqlite.SQLiteDatabase;
27+
import android.database.sqlite.SQLiteOpenHelper;
28+
29+
import java.util.ArrayList;
30+
31+
/**
32+
* Created by amitshekhar on 06/02/17.
33+
*/
34+
35+
public class CarDBHelper extends SQLiteOpenHelper {
36+
37+
public static final String DATABASE_NAME = "Car.db";
38+
public static final String CARS_TABLE_NAME = "cars";
39+
public static final String CARS_COLUMN_ID = "id";
40+
public static final String CARS_COLUMN_NAME = "name";
41+
public static final String CARS_COLUMN_COLOR = "color";
42+
public static final String CCARS_COLUMN_MILEAGE = "mileage";
43+
44+
public CarDBHelper(Context context) {
45+
super(context, DATABASE_NAME, null, 1);
46+
}
47+
48+
@Override
49+
public void onCreate(SQLiteDatabase db) {
50+
// TODO Auto-generated method stub
51+
db.execSQL(
52+
"create table cars " +
53+
"(id integer primary key, name text, color text, mileage real)"
54+
);
55+
}
56+
57+
@Override
58+
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
59+
// TODO Auto-generated method stub
60+
db.execSQL("DROP TABLE IF EXISTS cars");
61+
onCreate(db);
62+
}
63+
64+
public boolean insertCar(String name, String color, float mileage) {
65+
SQLiteDatabase db = this.getWritableDatabase();
66+
ContentValues contentValues = new ContentValues();
67+
contentValues.put("name", name);
68+
contentValues.put("color", color);
69+
contentValues.put("mileage", mileage);
70+
db.insert("cars", null, contentValues);
71+
return true;
72+
}
73+
74+
public Cursor getData(int id) {
75+
SQLiteDatabase db = this.getReadableDatabase();
76+
Cursor res = db.rawQuery("select * from cars where id=" + id + "", null);
77+
return res;
78+
}
79+
80+
public int numberOfRows() {
81+
SQLiteDatabase db = this.getReadableDatabase();
82+
int numRows = (int) DatabaseUtils.queryNumEntries(db, CARS_TABLE_NAME);
83+
return numRows;
84+
}
85+
86+
public boolean updateCar(Integer id, String name, String color, float mileage) {
87+
SQLiteDatabase db = this.getWritableDatabase();
88+
ContentValues contentValues = new ContentValues();
89+
contentValues.put("name", name);
90+
contentValues.put("color", color);
91+
contentValues.put("mileage", mileage);
92+
db.update("cars", contentValues, "id = ? ", new String[]{Integer.toString(id)});
93+
return true;
94+
}
95+
96+
public Integer deleteCar(Integer id) {
97+
SQLiteDatabase db = this.getWritableDatabase();
98+
return db.delete("cars",
99+
"id = ? ",
100+
new String[]{Integer.toString(id)});
101+
}
102+
103+
public ArrayList<String> getAllCars() {
104+
ArrayList<String> arrayList = new ArrayList<>();
105+
106+
//hp = new HashMap();
107+
SQLiteDatabase db = this.getReadableDatabase();
108+
Cursor res = db.rawQuery("select * from cars", null);
109+
res.moveToFirst();
110+
111+
while (res.isAfterLast() == false) {
112+
arrayList.add(res.getString(res.getColumnIndex(CARS_COLUMN_NAME)));
113+
res.moveToNext();
114+
}
115+
return arrayList;
116+
}
117+
118+
public int count() {
119+
SQLiteDatabase db = getReadableDatabase();
120+
Cursor cursor = db.rawQuery("select * from cars", null);
121+
if (cursor != null && cursor.getCount() > 0) {
122+
cursor.moveToFirst();
123+
return cursor.getInt(0);
124+
} else {
125+
return 0;
126+
}
127+
}
128+
}

app/src/main/java/com/sample/database/ContactDBHelper.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public void onCreate(SQLiteDatabase db) {
5353
// TODO Auto-generated method stub
5454
db.execSQL(
5555
"create table contacts " +
56-
"(id integer primary key, name text,phone text,email text, street text,place text, createdAt integer)"
56+
"(id integer primary key, name text, phone text, email text, street text, place text, createdAt integer)"
5757
);
5858
}
5959

@@ -109,18 +109,18 @@ public Integer deleteContact(Integer id) {
109109
}
110110

111111
public ArrayList<String> getAllCotacts() {
112-
ArrayList<String> array_list = new ArrayList<String>();
112+
ArrayList<String> arrayList = new ArrayList<>();
113113

114114
//hp = new HashMap();
115115
SQLiteDatabase db = this.getReadableDatabase();
116116
Cursor res = db.rawQuery("select * from contacts", null);
117117
res.moveToFirst();
118118

119119
while (res.isAfterLast() == false) {
120-
array_list.add(res.getString(res.getColumnIndex(CONTACTS_COLUMN_NAME)));
120+
arrayList.add(res.getString(res.getColumnIndex(CONTACTS_COLUMN_NAME)));
121121
res.moveToNext();
122122
}
123-
return array_list;
123+
return arrayList;
124124
}
125125

126126
public int count() {
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
*
3+
* * Copyright (C) 2016 Amit Shekhar
4+
* * Copyright (C) 2011 Android Open Source Project
5+
* *
6+
* * Licensed under the Apache License, Version 2.0 (the "License");
7+
* * you may not use this file except in compliance with the License.
8+
* * You may obtain a copy of the License at
9+
* *
10+
* * http://www.apache.org/licenses/LICENSE-2.0
11+
* *
12+
* * Unless required by applicable law or agreed to in writing, software
13+
* * distributed under the License is distributed on an "AS IS" BASIS,
14+
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* * See the License for the specific language governing permissions and
16+
* * limitations under the License.
17+
*
18+
*/
19+
20+
package com.sample.utils;
21+
22+
import android.content.Context;
23+
import android.widget.Toast;
24+
25+
import com.sample.BuildConfig;
26+
27+
import java.lang.reflect.Method;
28+
29+
/**
30+
* Created by amitshekhar on 07/02/17.
31+
*/
32+
33+
public class Utils {
34+
35+
private Utils() {
36+
37+
}
38+
39+
public static void showDebugDBAddressLogToast(Context context) {
40+
if (BuildConfig.DEBUG) {
41+
try {
42+
Class<?> debugDB = Class.forName("com.amitshekhar.DebugDB");
43+
Method getAddressLog = debugDB.getMethod("getAddressLog");
44+
Object value = getAddressLog.invoke(null);
45+
Toast.makeText(context, (String) value, Toast.LENGTH_LONG).show();
46+
} catch (Exception ignore) {
47+
48+
}
49+
}
50+
}
51+
}

app/src/main/res/layout/activity_main.xml

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
<?xml version="1.0" encoding="utf-8"?>
2-
<!--
1+
<?xml version="1.0" encoding="utf-8"?><!--
32
~ /*
43
~ * Copyright (C) 2016 Amit Shekhar
54
~ * Copyright (C) 2011 Android Open Source Project
@@ -18,7 +17,7 @@
1817
~ */
1918
-->
2019

21-
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
20+
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
2221
xmlns:tools="http://schemas.android.com/tools"
2322
android:id="@+id/activity_main"
2423
android:layout_width="match_parent"
@@ -29,8 +28,12 @@
2928
android:paddingTop="@dimen/activity_vertical_margin"
3029
tools:context="com.sample.MainActivity">
3130

32-
<TextView
31+
<Button
3332
android:layout_width="wrap_content"
3433
android:layout_height="wrap_content"
35-
android:text="Hello World!" />
36-
</RelativeLayout>
34+
android:textColor="@android:color/black"
35+
android:onClick="showDebugDbAddress"
36+
android:layout_gravity="center"
37+
android:text="@string/show_debug_db_address" />
38+
39+
</FrameLayout>

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@
1919

2020
<resources>
2121
<string name="app_name">Android-Debug-Database</string>
22+
<string name="show_debug_db_address">Show Debug Db Address</string>
2223
</resources>

debug-db/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ dependencies {
4848
exclude group: 'com.android.support', module: 'support-annotations'
4949
})
5050
testCompile 'junit:junit:4.12'
51-
compile 'com.google.code.gson:gson:2.4'
51+
compile 'com.google.code.gson:gson:2.8.0'
5252
}
5353

5454
//apply from: 'debug-db-upload.gradle'

debug-db/debug-db-upload.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def siteUrl = 'https://github.com/amitshekhariitbhu/Android-Debug-Database'
2424
def gitUrl = 'https://github.com/amitshekhariitbhu/Android-Debug-Database.git'
2525

2626
group = "com.amitshekhar.android"
27-
version = '0.5.0'
27+
version = '0.6.0-RC1'
2828

2929
install {
3030
repositories.mavenInstaller {

0 commit comments

Comments
 (0)