我是在遵循atutorial但是当我到达要制作的部分时:
getLoaderManager().initLoader(0, null, this);
this
正在制造问题。它引用的是MainActivity
,但它不会接受实现。其具体信息是:
第三个参数类型错误。发现:
“com.example.plainolnotes.mainActivity”,必需:
'android.app.loadermanager.loadercallbacks的
package com.example.plainolnotes;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v4.widget.CursorAdapter;
import android.support.v4.widget.SimpleCursorAdapter;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;
public class MainActivity extends AppCompatActivity
implements LoaderManager.LoaderCallbacks<Cursor>
{
private CursorAdapter cursorAdapter;
// @TargetApi(Build.VERSION_CODES.JELLY_BEAN) //Not a good long term solution
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
insertNote("New_note");
/*
Cursor cursor = getContentResolver().query(NotesProvider.CONTENT_URI,
DBOpenHelper.ALL_COLUMNS, null, null, null, null);
*/
String[] from = {DBOpenHelper.NOTE_TEXT};
int[] to = {android.R.id.text1};
cursorAdapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1, null, from, to, 0);
ListView list = (ListView) findViewById(android.R.id.list);
list.setAdapter(cursorAdapter);
//getLoaderManager().initLoader(0, null, this);
getLoaderManager().initLoader(0, null, this);
/*
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
*/
}
private void insertNote(String noteText) {
ContentValues values = new ContentValues();
values.put(DBOpenHelper.NOTE_TEXT, noteText);
Uri noteUri = getContentResolver().insert(NotesProvider.CONTENT_URI,
values);
Log.d("MainActivity", "Inserted note " + noteUri.getLastPathSegment());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
//Loaded when data is needed for the contentProvider
return new CursorLoader(this, NotesProvider.CONTENT_URI,
null, null, null, null);
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
cursorAdapter.swapCursor(data);
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
//data needs to be wiped out
cursorAdapter.swapCursor(null);
}
}
最佳答案
import语句有问题
import android.support.v4.app.LoaderManager;
鉴于要求是
android.app.LoaderManager.LoaderCallbacks
更换它来解决你的问题