I've tried to use Loader to fetch the data from the database. I've tried getsupportloaderManager and all the other stuff still it's showing error. In getloaderManager().initLoader(PET_LOADER, null, this); Last parameter i.e this isnot getting accepted.
The code is:
package com.example.pets;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.loader.app.LoaderManager;
import androidx.loader.content.CursorLoader;
import androidx.loader.content.Loader;
import com.example.pets.Data.PetContract;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
public class MainActivity extends AppCompatActivity implements
LoaderManager.LoaderCallbacks<Cursor> {
public static final int PET_LOADER = 10;
PetCursorAdapter petCursorAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(view -> {
Intent intent = new Intent(MainActivity.this, EditorActivity.class);
startActivity(intent);
});
ListView listView = (ListView) findViewById(R.id.listview);
View empty_view = findViewById(R.id.empty_view);
listView.setEmptyView(empty_view);
PetCursorAdapter petCursorAdapter = new PetCursorAdapter(this, null);
listView.setAdapter(petCursorAdapter);
**getLoaderManager().initLoader(PET_LOADER, null, this);**
}
private void insertPet() {
// Create a ContentValues object where column names are the keys,
// and Toto's pet attributes are the values.
ContentValues values = new ContentValues();
values.put(PetContract.PetEntry.PET_NAME, "Toto");
values.put(PetContract.PetEntry.BREED, "Terrier");
values.put(PetContract.PetEntry.GENDER, PetContract.PetEntry.GENDER_MALE);
values.put(PetContract.PetEntry.WEIGHT, 7);
// Insert a new row for Toto into the provider using the ContentResolver.
// Use the {@link PetEntry#CONTENT_URI} to indicate that we want to insert
// into the pets database table.
// Receive the new content URI that will allow us to access Toto's data in the future.
Uri newUri = getContentResolver().insert(PetContract.CONTENT_URI, values);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu options from the res/menu/menu_catalog.xml file.
// This adds menu items to the app bar.
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// User clicked on a menu option in the app bar overflow menu
switch (item.getItemId()) {
// Respond to a click on the "Insert dummy data" menu option
case R.id.action_insert_dummy_data:
insertPet();
return true;
// Respond to a click on the "Delete all entries" menu option
case R.id.action_delete_all_entries:
// Do nothing for now
return true;
}
return super.onOptionsItemSelected(item);
}
@NonNull
@Override
public Loader<Cursor> onCreateLoader(int id, @Nullable Bundle args) {
String[] projection = {PetContract.PetEntry._ID, PetContract.PetEntry.PET_NAME, PetContract.PetEntry.BREED};
return new CursorLoader(this, PetContract.CONTENT_URI, projection, null, null, null);
}
@Override
public void onLoadFinished(@NonNull Loader<Cursor> loader, Cursor data) {
petCursorAdapter.swapCursor(data);
}
@Override
public void onLoaderReset(@NonNull Loader<Cursor> loader) {
petCursorAdapter.swapCursor(null);
}
}
and the problem i'm getting is:
'initLoader(int, android.os.Bundle, android.app.LoaderManager.LoaderCallbacks<D>)' in 'android.app.LoaderManager' cannot be applied to '(int, null, com.example.pets.MainActivity)'
How to solve this error?