Skip to content

Commit

Permalink
Merge pull request #35 from amitshekhariitbhu/Development
Browse files Browse the repository at this point in the history
Editing Values and Offline Support
  • Loading branch information
amitshekhariitbhu authored Feb 8, 2017
2 parents 6ef446c + 05d12c8 commit f4b4a95
Show file tree
Hide file tree
Showing 35 changed files with 2,611 additions and 497 deletions.
33 changes: 31 additions & 2 deletions app/src/main/java/com/sample/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,14 @@
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.sample.database.CarDBHelper;
import com.sample.database.ContactDBHelper;
import com.sample.utils.Utils;

import java.util.HashSet;
import java.util.Set;

public class MainActivity extends AppCompatActivity {

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

setContentView(R.layout.activity_main);

Set<String> stringSet = new HashSet<>();
stringSet.add("SetOne");
stringSet.add("SetTwo");
stringSet.add("SetThree");

SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

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

sharedPreferences.edit().putString("testOne", "one").commit();
sharedPreferences.edit().putString("testTwo", "two").commit();
sharedPreferences.edit().putInt("testTwo", 2).commit();
sharedPreferences.edit().putLong("testThree", 100000L).commit();
sharedPreferences.edit().putFloat("testFour", 3.01F).commit();
sharedPreferences.edit().putBoolean("testFive", true).commit();
sharedPreferences.edit().putStringSet("testSix", stringSet).commit();

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

Expand All @@ -58,8 +73,22 @@ protected void onCreate(Bundle savedInstanceState) {
String email = "email_" + i;
String street = "street_" + i;
String place = "place_" + i;
contactDBHelper.insertContact(name, phone, email, street, place);
contactDBHelper.insertContact(name, phone, email, street, null);
}
}

CarDBHelper carDBHelper = new CarDBHelper(getApplicationContext());
if (carDBHelper.count() == 0) {
for (int i = 0; i < 50; i++) {
String name = "name_" + i;
String color = "RED";
float mileage = i + 10.45f;
carDBHelper.insertCar(name, color, mileage);
}
}
}

public void showDebugDbAddress(View view) {
Utils.showDebugDBAddressLogToast(getApplicationContext());
}
}
128 changes: 128 additions & 0 deletions app/src/main/java/com/sample/database/CarDBHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/*
*
* * Copyright (C) 2016 Amit Shekhar
* * Copyright (C) 2011 Android Open Source Project
* *
* * Licensed under the Apache License, Version 2.0 (the "License");
* * you may not use this file except in compliance with the License.
* * You may obtain a copy of the License at
* *
* * http://www.apache.org/licenses/LICENSE-2.0
* *
* * Unless required by applicable law or agreed to in writing, software
* * distributed under the License is distributed on an "AS IS" BASIS,
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* * See the License for the specific language governing permissions and
* * limitations under the License.
*
*/

package com.sample.database;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import java.util.ArrayList;

/**
* Created by amitshekhar on 06/02/17.
*/

public class CarDBHelper extends SQLiteOpenHelper {

public static final String DATABASE_NAME = "Car.db";
public static final String CARS_TABLE_NAME = "cars";
public static final String CARS_COLUMN_ID = "id";
public static final String CARS_COLUMN_NAME = "name";
public static final String CARS_COLUMN_COLOR = "color";
public static final String CCARS_COLUMN_MILEAGE = "mileage";

public CarDBHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}

@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(
"create table cars " +
"(id integer primary key, name text, color text, mileage real)"
);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS cars");
onCreate(db);
}

public boolean insertCar(String name, String color, float mileage) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("name", name);
contentValues.put("color", color);
contentValues.put("mileage", mileage);
db.insert("cars", null, contentValues);
return true;
}

public Cursor getData(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery("select * from cars where id=" + id + "", null);
return res;
}

public int numberOfRows() {
SQLiteDatabase db = this.getReadableDatabase();
int numRows = (int) DatabaseUtils.queryNumEntries(db, CARS_TABLE_NAME);
return numRows;
}

public boolean updateCar(Integer id, String name, String color, float mileage) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("name", name);
contentValues.put("color", color);
contentValues.put("mileage", mileage);
db.update("cars", contentValues, "id = ? ", new String[]{Integer.toString(id)});
return true;
}

public Integer deleteCar(Integer id) {
SQLiteDatabase db = this.getWritableDatabase();
return db.delete("cars",
"id = ? ",
new String[]{Integer.toString(id)});
}

public ArrayList<String> getAllCars() {
ArrayList<String> arrayList = new ArrayList<>();

//hp = new HashMap();
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery("select * from cars", null);
res.moveToFirst();

while (res.isAfterLast() == false) {
arrayList.add(res.getString(res.getColumnIndex(CARS_COLUMN_NAME)));
res.moveToNext();
}
return arrayList;
}

public int count() {
SQLiteDatabase db = getReadableDatabase();
Cursor cursor = db.rawQuery("select * from cars", null);
if (cursor != null && cursor.getCount() > 0) {
cursor.moveToFirst();
return cursor.getInt(0);
} else {
return 0;
}
}
}
8 changes: 4 additions & 4 deletions app/src/main/java/com/sample/database/ContactDBHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(
"create table contacts " +
"(id integer primary key, name text,phone text,email text, street text,place text, createdAt integer)"
"(id integer primary key, name text, phone text, email text, street text, place text, createdAt integer)"
);
}

Expand Down Expand Up @@ -109,18 +109,18 @@ public Integer deleteContact(Integer id) {
}

public ArrayList<String> getAllCotacts() {
ArrayList<String> array_list = new ArrayList<String>();
ArrayList<String> arrayList = new ArrayList<>();

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

while (res.isAfterLast() == false) {
array_list.add(res.getString(res.getColumnIndex(CONTACTS_COLUMN_NAME)));
arrayList.add(res.getString(res.getColumnIndex(CONTACTS_COLUMN_NAME)));
res.moveToNext();
}
return array_list;
return arrayList;
}

public int count() {
Expand Down
51 changes: 51 additions & 0 deletions app/src/main/java/com/sample/utils/Utils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
*
* * Copyright (C) 2016 Amit Shekhar
* * Copyright (C) 2011 Android Open Source Project
* *
* * Licensed under the Apache License, Version 2.0 (the "License");
* * you may not use this file except in compliance with the License.
* * You may obtain a copy of the License at
* *
* * http://www.apache.org/licenses/LICENSE-2.0
* *
* * Unless required by applicable law or agreed to in writing, software
* * distributed under the License is distributed on an "AS IS" BASIS,
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* * See the License for the specific language governing permissions and
* * limitations under the License.
*
*/

package com.sample.utils;

import android.content.Context;
import android.widget.Toast;

import com.sample.BuildConfig;

import java.lang.reflect.Method;

/**
* Created by amitshekhar on 07/02/17.
*/

public class Utils {

private Utils() {

}

public static void showDebugDBAddressLogToast(Context context) {
if (BuildConfig.DEBUG) {
try {
Class<?> debugDB = Class.forName("com.amitshekhar.DebugDB");
Method getAddressLog = debugDB.getMethod("getAddressLog");
Object value = getAddressLog.invoke(null);
Toast.makeText(context, (String) value, Toast.LENGTH_LONG).show();
} catch (Exception ignore) {

}
}
}
}
15 changes: 9 additions & 6 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
<?xml version="1.0" encoding="utf-8"?><!--
~ /*
~ * Copyright (C) 2016 Amit Shekhar
~ * Copyright (C) 2011 Android Open Source Project
Expand All @@ -18,7 +17,7 @@
~ */
-->

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
Expand All @@ -29,8 +28,12 @@
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.sample.MainActivity">

<TextView
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
</RelativeLayout>
android:textColor="@android:color/black"
android:onClick="showDebugDbAddress"
android:layout_gravity="center"
android:text="@string/show_debug_db_address" />

</FrameLayout>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@

<resources>
<string name="app_name">Android-Debug-Database</string>
<string name="show_debug_db_address">Show Debug Db Address</string>
</resources>
2 changes: 1 addition & 1 deletion debug-db/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ dependencies {
exclude group: 'com.android.support', module: 'support-annotations'
})
testCompile 'junit:junit:4.12'
compile 'com.google.code.gson:gson:2.4'
compile 'com.google.code.gson:gson:2.8.0'
}

//apply from: 'debug-db-upload.gradle'
2 changes: 1 addition & 1 deletion debug-db/debug-db-upload.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def siteUrl = 'https://github.com/amitshekhariitbhu/Android-Debug-Database'
def gitUrl = 'https://github.com/amitshekhariitbhu/Android-Debug-Database.git'

group = "com.amitshekhar.android"
version = '0.5.0'
version = '0.6.0-RC1'

install {
repositories.mavenInstaller {
Expand Down
Loading

0 comments on commit f4b4a95

Please sign in to comment.