-
-
Notifications
You must be signed in to change notification settings - Fork 871
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35 from amitshekhariitbhu/Development
Editing Values and Offline Support
- Loading branch information
Showing
35 changed files
with
2,611 additions
and
497 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
|
||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.