Skip to content

Commit

Permalink
After Lesson 3
Browse files Browse the repository at this point in the history
  • Loading branch information
priyanshujain2341 committed Aug 27, 2021
1 parent a827fcd commit d9b0698
Show file tree
Hide file tree
Showing 7 changed files with 332 additions and 18 deletions.
4 changes: 4 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@
android:name="android.support.PARENT_ACTIVITY"
android:value=".CatalogActivity" />
</activity>
<provider
android:name=".data.PetProvider"
android:authorities="com.example.android.pets"
android:exported="false" />
</application>

</manifest>
29 changes: 19 additions & 10 deletions app/src/main/java/com/example/android/pets/CatalogActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
Expand Down Expand Up @@ -56,7 +57,7 @@ private void displayDatabaseInfo() {
// PetDbHelper mDbHelper = new PetDbHelper(this);

// Create and/or open a database to read from it
SQLiteDatabase db = mDbHelper.getReadableDatabase();
// SQLiteDatabase db = mDbHelper.getReadableDatabase();


String[] projections = {
Expand All @@ -65,17 +66,25 @@ private void displayDatabaseInfo() {
PetEntry.COLUMN_PET_BREED,
PetEntry.COLUMN_PET_GENDER,
PetEntry.COLUMN_PET_WEIGHT };

// String selection = " WHERE ";
// Perform this raw SQL query "SELECT * FROM pets"
// to get a Cursor that contains all rows from the pets table.
Cursor cursor = db.query(
PetEntry.TABLE_NAME, projections,
null,
null,
// Cursor cursor = db.query(
// PetEntry.TABLE_NAME, projections,
// null,
// null,
// null,
// null,
// null,
// null );

Cursor cursor = getContentResolver().query(
PetEntry.CONTENT_URI,
projections,
null,
null,
null,
null );
null);

TextView textViewPet = (TextView) findViewById(R.id.text_view_pet);

Expand Down Expand Up @@ -148,15 +157,15 @@ public boolean onOptionsItemSelected(MenuItem item) {
private void insertPet()
{
// PetDbHelper mDbHelper = new PetDbHelper(this);
SQLiteDatabase db = mDbHelper.getWritableDatabase();
// SQLiteDatabase db = mDbHelper.getWritableDatabase();

ContentValues values = new ContentValues();
// values.put(PetEntry._ID, 1);

values.put(PetEntry.COLUMN_PET_NAME, "Toto");
values.put(PetEntry.COLUMN_PET_BREED, "Terrier");
values.put(PetEntry.COLUMN_PET_GENDER, PetEntry.GENDER_MALE);
values.put(PetEntry.COLUMN_PET_WEIGHT, 7);

long id = db.insert(PetEntry.TABLE_NAME, null, values);
Uri newUri = getContentResolver().insert(PetEntry.CONTENT_URI, values);
}
}
17 changes: 11 additions & 6 deletions app/src/main/java/com/example/android/pets/EditorActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
*/
package com.example.android.pets;

import android.content.ContentUris;
import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.support.v7.app.AppCompatActivity;
Expand Down Expand Up @@ -130,26 +132,29 @@ private void insertPet()
int weight = Integer.parseInt(weightString);


PetDbHelper mDbHelper = new PetDbHelper(this);
SQLiteDatabase db = mDbHelper.getWritableDatabase();
// PetDbHelper mDbHelper = new PetDbHelper(this);
// SQLiteDatabase db = mDbHelper.getWritableDatabase();

ContentValues values = new ContentValues();
values.put(PetEntry.COLUMN_PET_NAME, nameString);
values.put(PetEntry.COLUMN_PET_BREED, breedString);
values.put(PetEntry.COLUMN_PET_GENDER, mGender);
values.put(PetEntry.COLUMN_PET_WEIGHT, weight);

long newRowId = db.insert(PetEntry.TABLE_NAME, null, values);
Uri newUri = getContentResolver().insert(
PetEntry.CONTENT_URI,
values);
// long newRowId = db.insert(PetEntry.TABLE_NAME, null, values);

if (newRowId == -1)
if (newUri == null)
{
// If the row ID is -1, then there was an error with insertion.
Toast.makeText(this, "Error with saving pet", Toast.LENGTH_SHORT).show();
Toast.makeText(this, getString(R.string.pet_save_unsuccessful), Toast.LENGTH_SHORT).show();
}
else
{
// Otherwise, the insertion was successful and we can display a toast with the row ID.
Toast.makeText(this, "Pet saved with row id: " + newRowId, Toast.LENGTH_SHORT).show();
Toast.makeText(this, getString(R.string.pet_save_successful) , Toast.LENGTH_SHORT).show();
}

}
Expand Down
27 changes: 27 additions & 0 deletions app/src/main/java/com/example/android/pets/data/PetContract.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,31 @@
package com.example.android.pets.data;

import android.content.ContentResolver;
import android.net.Uri;
import android.provider.BaseColumns;

public final class PetContract
{

public static final String CONTENT_AUTHORITY = "com.example.android.pets";
public static final Uri BASE_CONTENT_URI = Uri.parse("content://" + CONTENT_AUTHORITY);
public static final String PATH_PETS = "pets";



public static final int PETS = 100;
public static final int PET_ID = 101;

private PetContract() {}

public static final class PetEntry implements BaseColumns
{
public static final Uri CONTENT_URI = Uri.withAppendedPath(BASE_CONTENT_URI, PATH_PETS);

public static final String CONTENT_LIST_TYPE = ContentResolver.CURSOR_DIR_BASE_TYPE + "/" + CONTENT_AUTHORITY + "/" + PATH_PETS;
public static final String CONTENT_ITEM_TYPE = ContentResolver.CURSOR_ITEM_BASE_TYPE + "/" + CONTENT_AUTHORITY + "/" + PATH_PETS;


public static final String TABLE_NAME = "pets";

public static final String _ID = BaseColumns._ID;
Expand All @@ -20,5 +38,14 @@ public static final class PetEntry implements BaseColumns
public static final int GENDER_FEMALE = 2;
public static final int GENDER_UNKNOWN = 0;

public static boolean isValidGender(int gender)
{
if(gender==GENDER_MALE || gender==GENDER_FEMALE || gender==GENDER_UNKNOWN)
{
return true;
}
return false;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ public void onCreate(SQLiteDatabase db)
db.execSQL(SQL_CREATE_ENTRIES);
}



public static final String DELETE_ALL_ENTRIES = "DROP TABLE IF EXISTS " + PetEntry.TABLE_NAME;

}
Loading

0 comments on commit d9b0698

Please sign in to comment.