我知道在使用LoaderCallbacks时如何使用notifyChange方法。但是我有一种情况,我需要通过URI从任何ContentProvider方法外部通知游标,该游标已更改。实际上,我正在使用微调器根据微调器选择(selectionArgs)的更改来查询数据库。选择微调器后,我单击搜索按钮。在按钮单击侦听器上,我使用getLoaderManager().initLoader()
启动加载程序。我试图在getContext().getContentResolver().notifyChange(URI, null)
方法之前添加initLoader
方法。但这给了我getContext()
方法一个错误。那么,在这种情况下我该怎么办?
这是我的Main_Activity.java代码
public class MainActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<Cursor> {
//Spinner for selecting blood group on search
private Spinner mBloodTypeSpinner;
private int mBloodType;
private Button searchBtn;
private static final int DONOR_LOADER = 0;
DonorCursorAdapter mCursorAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
searchBtn = (Button) findViewById(R.id.search_btn);
// Setup FAB to open EditorActivity
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, EditorActivity.class);
startActivity(intent);
}
});
mBloodTypeSpinner = (Spinner) findViewById(R.id.spinner_bloodType_search);
//Set the blood picker spinner
setupBloodTypeSpinner();
// Find the ListView which will be populated with the pet data
ListView donorListView = (ListView) findViewById(R.id.list);
// Setup an Adapter to create a list item for each row of donor data in the Cursor.
// There is no donor data yet (until the loader finishes) so pass in null for the Cursor.
mCursorAdapter = new DonorCursorAdapter(this, null);
donorListView.setAdapter(mCursorAdapter);
searchBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Kick off the loader in Main_Activity
getLoaderManager().initLoader(DONOR_LOADER, null,MainActivity.this);
}
});
}
/**Setup spinner for blood type pickup*/
private void setupBloodTypeSpinner() {
// Create adapter for spinner. The list options are from the String array it will use
// the spinner will use the default layout
ArrayAdapter bloodTypeSpinnerAdapter = ArrayAdapter.createFromResource(this,
R.array.array_bloodType_options, android.R.layout.simple_spinner_item);
// Specify dropdown layout style - simple list view with 1 item per line
bloodTypeSpinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
mBloodTypeSpinner.setAdapter(bloodTypeSpinnerAdapter);
// Set the integer mSelected to the constant values
mBloodTypeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String selection = (String) parent.getItemAtPosition(position);
if (!TextUtils.isEmpty(selection)) {
if (selection.equals(getString(R.string.a_positive))) {
mBloodType = BloodContract.DonorEntry.A_Positive;
} else if (selection.equals(getString(R.string.a_negative))) {
mBloodType = BloodContract.DonorEntry.A_Negative;
} else if (selection.equals(getString(R.string.b_positive))) {
mBloodType = BloodContract.DonorEntry.B_Positive;
}else if (selection.equals(getString(R.string.b_negative))) {
mBloodType = BloodContract.DonorEntry.B_Negative;
}else if (selection.equals(getString(R.string.o_positive))) {
mBloodType = BloodContract.DonorEntry.O_Positive;
}else if (selection.equals(getString(R.string.o_negative))) {
mBloodType = BloodContract.DonorEntry.O_Negative;
}else if (selection.equals(getString(R.string.ab_positive))) {
mBloodType = BloodContract.DonorEntry.AB_Positive;
}else if (selection.equals(getString(R.string.ab_negative))) {
mBloodType = BloodContract.DonorEntry.AB_Negative;
} else{
mBloodType = BloodContract.DonorEntry.TYPE_UNKNOWN;
}
}
}
// Because AdapterView is an abstract class, onNothingSelected must be defined
@Override
public void onNothingSelected(AdapterView<?> parent) {
mBloodType = BloodContract.DonorEntry.TYPE_UNKNOWN;
}
});
}
// Get value of readable blood type
public String getBloodTypeString(int bloodType) {
switch (bloodType) {
case DonorEntry.A_Positive:
return getResources().getString(R.string.a_positive);
case DonorEntry.A_Negative:
return getResources().getString(R.string.a_negative);
case DonorEntry.B_Positive:
return getResources().getString(R.string.b_positive);
case DonorEntry.B_Negative:
return getResources().getString(R.string.b_negative);
case DonorEntry.O_Positive:
return getResources().getString(R.string.o_positive);
case DonorEntry.O_Negative:
return getResources().getString(R.string.o_negative);
case DonorEntry.AB_Positive:
return getResources().getString(R.string.ab_positive);
case DonorEntry.AB_Negative:
return getResources().getString(R.string.ab_negative);
default:
return "UNKNOWN";
}
}
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
//Projection for query method
String[] projection = {
DonorEntry._ID,
DonorEntry.COLUMN_DONOR_NAME,
DonorEntry.COLUMN_DONOR_MOBILE,
DonorEntry.COLUMN_DONATE_DATE,
DonorEntry.COLUMN_BLOOD_GROUP};
String selection = DonorEntry.COLUMN_BLOOD_GROUP + "=?";
String[] selectionArgs = new String[]{String.valueOf(mBloodType)};
// This loader will execute the ContentProvider's query method on a background thread
return new CursorLoader(this,
DonorEntry.CONTENT_URI,
projection,
selection,
selectionArgs,
null);
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
mCursorAdapter.swapCursor(data);
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
mCursorAdapter.swapCursor(null);
}
}