Skip to content

Commit

Permalink
1.19 Switch to CursorLoader
Browse files Browse the repository at this point in the history
  • Loading branch information
Beginning Android committed Aug 23, 2016
1 parent 636e6bd commit dec30b1
Showing 1 changed file with 45 additions and 38 deletions.
83 changes: 45 additions & 38 deletions app/src/main/java/com/example/android/pets/CatalogActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@
*/
package com.example.android.pets;

import android.app.LoaderManager;
import android.content.ContentValues;
import android.content.CursorLoader;
import android.content.Intent;
import android.content.Loader;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
Expand All @@ -32,7 +35,14 @@
/**
* 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> {

/** Identifier for the pet data loader */
private static final int PET_LOADER = 0;

/** Adapter for the ListView */
PetCursorAdapter mCursorAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -55,45 +65,14 @@ public void onClick(View view) {
// Find and set empty view on the ListView, so that it only shows when the list has 0 items.
View emptyView = findViewById(R.id.empty_view);
petListView.setEmptyView(emptyView);
}

@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() {
// Define a projection that specifies which columns from the database
// you will actually use after this query.
String[] projection = {
PetEntry._ID,
PetEntry.COLUMN_PET_NAME,
PetEntry.COLUMN_PET_BREED,
PetEntry.COLUMN_PET_GENDER,
PetEntry.COLUMN_PET_WEIGHT };

// Perform a query on the provider using the ContentResolver.
// Use the {@link PetEntry#CONTENT_URI} to access the pet data.
Cursor cursor = getContentResolver().query(
PetEntry.CONTENT_URI, // The content URI of the words table
projection, // The columns to return for each row
null, // Selection criteria
null, // Selection criteria
null); // The sort order for the returned rows

// Find the ListView which will be populated with the pet data
ListView petListView = (ListView) findViewById(R.id.list);

// Setup an Adapter to create a list item for each row of pet data in the Cursor.
PetCursorAdapter adapter = new PetCursorAdapter(this, cursor);
// There is no pet data yet (until the loader finishes) so pass in null for the Cursor.
mCursorAdapter = new PetCursorAdapter(this, null);
petListView.setAdapter(mCursorAdapter);

// Attach the adapter to the ListView.
petListView.setAdapter(adapter);
// Kick off the loader
getLoaderManager().initLoader(PET_LOADER, null, this);
}

/**
Expand Down Expand Up @@ -130,7 +109,6 @@ public boolean onOptionsItemSelected(MenuItem item) {
// Respond to a click on the "Insert dummy data" menu option
case R.id.action_insert_dummy_data:
insertPet();
displayDatabaseInfo();
return true;
// Respond to a click on the "Delete all entries" menu option
case R.id.action_delete_all_entries:
Expand All @@ -139,4 +117,33 @@ public boolean onOptionsItemSelected(MenuItem item) {
}
return super.onOptionsItemSelected(item);
}

@Override
public Loader<Cursor> onCreateLoader(int i, Bundle bundle) {
// Define a projection that specifies the columns from the table we care about.
String[] projection = {
PetEntry._ID,
PetEntry.COLUMN_PET_NAME,
PetEntry.COLUMN_PET_BREED };

// This loader will execute the ContentProvider's query method on a background thread
return new CursorLoader(this, // Parent activity context
PetEntry.CONTENT_URI, // Provider content URI to query
projection, // Columns to include in the resulting Cursor
null, // No selection clause
null, // No selection arguments
null); // Default sort order
}

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
// Update {@link PetCursorAdapter} with this new cursor containing updated pet data
mCursorAdapter.swapCursor(data);
}

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

1 comment on commit dec30b1

@rishav0511
Copy link

@rishav0511 rishav0511 commented on dec30b1 Jan 24, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getting error
error: method initLoader in class LoaderManager cannot be applied to given types;
getLoaderManager().initLoader(PET_LOADER, null, this);
^
required: int,Bundle,LoaderCallbacks
found: int,< null >,CatalogActivity
reason: cannot infer type-variable(s) D
(argument mismatch; CatalogActivity cannot be converted to LoaderCallbacks)
where D is a type-variable:
D extends Object declared in method initLoader(int,Bundle,LoaderCallbacks)
please help....

Please sign in to comment.