Skip to content

Commit

Permalink
After Lesson 4
Browse files Browse the repository at this point in the history
  • Loading branch information
priyanshujain2341 committed Sep 1, 2021
1 parent d9b0698 commit d307100
Show file tree
Hide file tree
Showing 9 changed files with 683 additions and 118 deletions.
1 change: 0 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
</activity>
<activity
android:name=".EditorActivity"
android:label="@string/editor_activity_title_new_pet"
android:theme="@style/EditorTheme"
android:parentActivityName=".CatalogActivity" >
<!-- Parent activity meta-com.example.android.pets.data to support 4.0 and lower -->
Expand Down
239 changes: 159 additions & 80 deletions app/src/main/java/com/example/android/pets/CatalogActivity.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,39 @@
package com.example.android.pets;

import android.app.LoaderManager;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.CursorLoader;
import android.content.Intent;
import android.content.Loader;
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;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.android.pets.data.PetContract;
import com.example.android.pets.data.PetContract.PetEntry;
import com.example.android.pets.data.PetDbHelper;

/**
* Displays list of pets that were entered and stored in the app.
*/
public class CatalogActivity extends AppCompatActivity {
public class CatalogActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<Cursor> {

private PetDbHelper mDbHelper;

private static final int PET_LOADER = 0;
PetCursorAdapter mCursorAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -39,94 +50,131 @@ public void onClick(View view) {
});

mDbHelper = new PetDbHelper(this);

ListView petListView = (ListView) findViewById(R.id.list);
View emptyView = findViewById(R.id.empty_view);
petListView.setEmptyView(emptyView);

mCursorAdapter = new PetCursorAdapter(this, null);
petListView.setAdapter(mCursorAdapter);

petListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
Intent intent = new Intent(CatalogActivity.this, EditorActivity.class);

Uri currentPetUri = ContentUris.withAppendedId(PetEntry.CONTENT_URI, id);
intent.setData(currentPetUri);

startActivity(intent);
}
});

//Start the loader
getLoaderManager().initLoader(PET_LOADER, null, this);
}

@Override
protected void onStart() {
super.onStart();
displayDatabaseInfo();

private void deleteAllPets()
{
int rowsDeleted = getContentResolver().delete(PetEntry.CONTENT_URI, null, null);
Log.v("CatalogActivity", rowsDeleted + " rows deleted from pet database");
}

// @Override
// protected void onStart() {
// super.onStart();
// displayDatabaseInfo();
// }

/**
* Temporary helper method to display information in the onscreen TextView about the state of
* the pets database.
*/
private void displayDatabaseInfo() {
// To access our database, we instantiate our subclass of SQLiteOpenHelper
// and pass the context, which is the current activity.
// PetDbHelper mDbHelper = new PetDbHelper(this);

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


String[] projections = {
PetEntry._ID,
PetEntry.COLUMN_PET_NAME,
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,
// private void displayDatabaseInfo() {
// // To access our database, we instantiate our subclass of SQLiteOpenHelper
// // and pass the context, which is the current activity.
//// PetDbHelper mDbHelper = new PetDbHelper(this);
//
// // Create and/or open a database to read from it
//// SQLiteDatabase db = mDbHelper.getReadableDatabase();
//
//
// String[] projections = {
// PetEntry._ID,
// PetEntry.COLUMN_PET_NAME,
// 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,
//// null,
//// null,
//// null,
//// null );
//
// Cursor cursor = getContentResolver().query(
// PetEntry.CONTENT_URI,
// projections,
// null,
// null,
// null,
// null,
// null );

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

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

try
{
textViewPet.setText("The pets table contains "+cursor.getCount()+" pets.\n\n");
int idIndex = cursor.getColumnIndex(PetEntry._ID);
int nameIndex = cursor.getColumnIndex(PetEntry.COLUMN_PET_NAME);
int breedIndex = cursor.getColumnIndex(PetEntry.COLUMN_PET_BREED);
int genderIndex = cursor.getColumnIndex(PetEntry.COLUMN_PET_GENDER);
int weightIndex = cursor.getColumnIndex(PetEntry.COLUMN_PET_WEIGHT);

textViewPet.append(PetEntry._ID +" - "
+PetEntry.COLUMN_PET_NAME +" - "
+PetEntry.COLUMN_PET_BREED +" - "
+PetEntry.COLUMN_PET_GENDER +" - "
+PetEntry.COLUMN_PET_WEIGHT+ "\n\n");

while(cursor.moveToNext())
{
int id = cursor.getInt(idIndex);
String name = cursor.getString(nameIndex);
String breed = cursor.getString(breedIndex);
int gender = cursor.getInt(genderIndex);
int weight = cursor.getInt(weightIndex);

textViewPet.append(
id + " - "+
name + " - " +
breed + " - " +
gender + " - "
+ weight );
textViewPet.append("\n");
}
}
finally
{
// Always close the cursor when you're done reading from it. This releases all its
// resources and makes it invalid.
cursor.close();
}
}
// null);
//
//// TextView textViewPet = (TextView) findViewById(R.id.text_view_pet);
////
//// try
//// {
//// textViewPet.setText("The pets table contains "+cursor.getCount()+" pets.\n\n");
//// int idIndex = cursor.getColumnIndex(PetEntry._ID);
//// int nameIndex = cursor.getColumnIndex(PetEntry.COLUMN_PET_NAME);
//// int breedIndex = cursor.getColumnIndex(PetEntry.COLUMN_PET_BREED);
//// int genderIndex = cursor.getColumnIndex(PetEntry.COLUMN_PET_GENDER);
//// int weightIndex = cursor.getColumnIndex(PetEntry.COLUMN_PET_WEIGHT);
////
//// textViewPet.append(PetEntry._ID +" - "
//// +PetEntry.COLUMN_PET_NAME +" - "
//// +PetEntry.COLUMN_PET_BREED +" - "
//// +PetEntry.COLUMN_PET_GENDER +" - "
//// +PetEntry.COLUMN_PET_WEIGHT+ "\n\n");
////
//// while(cursor.moveToNext())
//// {
//// int id = cursor.getInt(idIndex);
//// String name = cursor.getString(nameIndex);
//// String breed = cursor.getString(breedIndex);
//// int gender = cursor.getInt(genderIndex);
//// int weight = cursor.getInt(weightIndex);
////
//// textViewPet.append(
//// id + " - "+
//// name + " - " +
//// breed + " - " +
//// gender + " - "
//// + weight );
//// textViewPet.append("\n");
//// }
//// }
//// finally
//// {
//// // Always close the cursor when you're done reading from it. This releases all its
//// // resources and makes it invalid.
//// cursor.close();
//// }
//
//
// ListView petListView = (ListView) findViewById(R.id.list);
//
// PetCursorAdapter adapter = new PetCursorAdapter(this, cursor);
//
// petListView.setAdapter(adapter);
// }

@Override
public boolean onCreateOptionsMenu(Menu menu) {
Expand All @@ -144,11 +192,12 @@ public boolean onOptionsItemSelected(MenuItem item) {
case R.id.action_insert_dummy_data:
// Do nothing for now
insertPet();
displayDatabaseInfo();
// displayDatabaseInfo();
return true;
// Respond to a click on the "Delete all entries" menu option
case R.id.action_delete_all_entries:
// Do nothing for now
deleteAllPets();
return true;
}
return super.onOptionsItemSelected(item);
Expand All @@ -168,4 +217,34 @@ private void insertPet()

Uri newUri = getContentResolver().insert(PetEntry.CONTENT_URI, values);
}

@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args)
{
String[] projection = {
PetEntry._ID,
PetEntry.COLUMN_PET_NAME,
PetEntry.COLUMN_PET_BREED };

return new CursorLoader(
this,
PetEntry.CONTENT_URI,
projection,
null,
null,
null );
}

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data)
{
mCursorAdapter.swapCursor(data);
}

@Override
public void onLoaderReset(Loader<Cursor> loader)
{
//Callbackk called when the data needs to be deleted
mCursorAdapter.swapCursor(null);
}
}
Loading

0 comments on commit d307100

Please sign in to comment.