我正在努力编译并运行Android开发人员网站上的示例:http://developer.android.com/guide/topics/ui/layout/listview.html
这是我输入的版本:
package com.chex.control;
import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.widget.ListAdapter;
import android.support.v4.app.LoaderManager;
import android.support.v4.widget.SimpleCursorAdapter;
import android.support.v4.content.Loader;
import android.support.v4.content.CursorLoader;
public class ListViewExample extends ListActivity implements
LoaderManager.LoaderCallbacks<Cursor> {
Cursor cursor;
// database columns that we will retreive
final String[] PROJECTION = new String[] { ContactsContract.Data._ID,
ContactsContract.Data.DISPLAY_NAME };
final String SELECTION = "((" + ContactsContract.Data.DISPLAY_NAME
+ " NOTNULL AND (" + ContactsContract.Data.DISPLAY_NAME
+ " != ''))";
SimpleCursorAdapter adapter=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] fromColumns = { ContactsContract.Data.DISPLAY_NAME };
int[] toViews = { android.R.id.text1 };
ListAdapter adapter = new SimpleCursorAdapter(this, // context
android.R.layout.simple_list_item_1, cursor, // cursor to bind
// to
fromColumns, // array of cursor
// columns to
// bind to
toViews, 0); // parallel
// array
// of
// which
// template
// objects
// to
// bind
// to
// cursor
// columns
setListAdapter(adapter);
// ******* THE FOLLOWING LINE WON'T COMPILE **************
getLoaderManager().initLoader(0, null, this);
}
@Override
public void setListAdapter(ListAdapter adapter) {
// TODO Auto-generated method stub
super.setListAdapter(adapter);
}
/**
* create and return a CursorLoader that will take care of creating a Curso
* for the data being displayed.
*/
@Override
public Loader onCreateLoader(int arg0, Bundle arg1) {
CursorLoader loader = new CursorLoader(this,
ContactsContract.Data.CONTENT_URI, PROJECTION, SELECTION, null,
null);
return loader;
}
@Override
public void onLoadFinished(Loader<Cursor> arg0, Cursor arg1) {
// TODO Auto-generated method stub
}
@Override
public void onLoaderReset(Loader<Cursor> arg0) {
// TODO Auto-generated method stub
}
}
Eclipse给出了编译错误:
The method initLoader(int, Bundle, LoaderManager.LoaderCallbacks<D>) in the type LoaderManager is not applicable for the arguments (int, null, ListViewExample).
显然,演员不想这样的例子错了吗?如果我只是从Android开发者网站剪切和粘贴,我也会遇到同样的问题。
当我在代码中将<Cursor>
更改为<D>
时,强制转换在执行期间失败。我不知道为什么 - 我认为班级在LoaderCallbacks
implements <D>
合同
答案 0 :(得分:4)
尝试使用getSupportLoaderManager
代替getLoaderManager
。
答案 1 :(得分:2)
在这里查看http://developer.android.com/tools/extras/support-library.html
他们说您需要使用getSupportLoaderManager
代替getLoaderManager
To manage your fragments and loaders, you must use the methods FragmentActivity.getSupportFragmentManager() and FragmentActivity.getSupportLoaderManager() (instead of the getFragmentManager() and getLoaderManager() methods).
但是你没有使用FragmentActivity并且你根本没有使用这个代码?它甚至需要在那里吗?
While the LoaderManager API was introduced in HONEYCOMB, a version of the API at is also available for use on older platforms through FragmentActivity.
据此我可以得出结论,您需要使用FragmentActivity
,并使用ListFragment
。
答案 2 :(得分:0)
您似乎导入了错误的LoadManager,android.support.v4.app.LoaderManager;
尝试导入此代码:android.app.LoaderManager
它可能有效。
文档:http://developer.android.com/reference/android/app/LoaderManager.html