我正在使用CursorLoader在我的Fragment中填充ListView。我想要做的是在ListView填充数据后以编程方式选择第一个项目。
我已经创建了一个showFirstItem()方法,我在ListView上调用performItemClick()。
然后我将showFirstItem()放入onLoadFinished()回调中,希望它会选择列表中的第一个项目,但结果我得到了这个错误:“java.lang.IllegalStateException:无法执行此操作onLoadFinished”。
我不明白为什么不能在这里完成。我应该在哪里调用performItemClick()?
public class CustomersFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor> {
private static final int URL_LOADER = 0;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mRootView = inflater.inflate(R.layout.fragment_customers, container, false);
customersListView = (ListView) mRootView.findViewById(id.list);
loadCustomersList();
return mRootView;
}
public void showFirstItem() {
customersListView.performItemClick(
mAdapter.getView(0, null, null), 0, mAdapter.getItemId(0)
);
}
private void loadCustomersList() {
// Initialize loader
getLoaderManager().initLoader(URL_LOADER, null, this);
String[] fromColumns = {};
int[] toViews = {};
mAdapter = new CustomersCursorAdapter(
getActivity(),
R.layout.list_item_customers,
null,
fromColumns,
toViews
);
customersListView.setAdapter(mAdapter);
}
@Override
public Loader<Cursor> onCreateLoader(int loaderID, Bundle bundle) {
CursorLoader cursorLoader = new CursorLoader(
getActivity(),
CustomersProvider.CONTENT_URI,
null,
null,
null,
null
);
return cursorLoader;
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
mAdapter.swapCursor(data);
showFirstItem(); // <------------ java.lang.IllegalStateException: Can not perform this action inside of onLoadFinished
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
mAdapter.swapCursor(null);
}
--------------编辑:错误日志
02-24 08:42:34.803: E/AndroidRuntime(986): FATAL EXCEPTION: main
02-24 08:42:34.803: E/AndroidRuntime(986): java.lang.IllegalStateException: Can not perform this action inside of onLoadFinished
02-24 08:42:34.803: E/AndroidRuntime(986): at android.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1322)
02-24 08:42:34.803: E/AndroidRuntime(986): at android.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1329)
02-24 08:42:34.803: E/AndroidRuntime(986): at android.app.BackStackRecord.commitInternal(BackStackRecord.java:595)
02-24 08:42:34.803: E/AndroidRuntime(986): at android.app.BackStackRecord.commit(BackStackRecord.java:574)
02-24 08:42:34.803: E/AndroidRuntime(986): at com.myapp.MainActivity.onCustomerSelected(MainActivity.java:360)
02-24 08:42:34.803: E/AndroidRuntime(986): at com.myapp.customers.CustomersFragment.onItemClickCallback(CustomersFragment.java:95)
02-24 08:42:34.803: E/AndroidRuntime(986): at com.myapp.customers.CustomersItemClickListener.onItemClick(CustomersItemClickListener.java:56)
02-24 08:42:34.803: E/AndroidRuntime(986): at android.widget.AdapterView.performItemClick(AdapterView.java:298)
02-24 08:42:34.803: E/AndroidRuntime(986): at android.widget.AbsListView.performItemClick(AbsListView.java:1100)
02-24 08:42:34.803: E/AndroidRuntime(986): at com.myapp.customers.CustomersFragment.showFirstItem(CustomersFragment.java:133)
02-24 08:42:34.803: E/AndroidRuntime(986): at com.myapp.customers.CustomersFragment.onLoadFinished(CustomersFragment.java:203)
02-24 08:42:34.803: E/AndroidRuntime(986): at com.myapp.customers.CustomersFragment.onLoadFinished(CustomersFragment.java:1)
02-24 08:42:34.803: E/AndroidRuntime(986): at android.app.LoaderManagerImpl$LoaderInfo.callOnLoadFinished(LoaderManager.java:483)
02-24 08:42:34.803: E/AndroidRuntime(986): at android.app.LoaderManagerImpl$LoaderInfo.onLoadComplete(LoaderManager.java:451)
02-24 08:42:34.803: E/AndroidRuntime(986): at android.content.Loader.deliverResult(Loader.java:143)
02-24 08:42:34.803: E/AndroidRuntime(986): at android.content.CursorLoader.deliverResult(CursorLoader.java:113)
02-24 08:42:34.803: E/AndroidRuntime(986): at android.content.CursorLoader.deliverResult(CursorLoader.java:43)
02-24 08:42:34.803: E/AndroidRuntime(986): at android.content.AsyncTaskLoader.dispatchOnLoadComplete(AsyncTaskLoader.java:254)
02-24 08:42:34.803: E/AndroidRuntime(986): at android.content.AsyncTaskLoader$LoadTask.onPostExecute(AsyncTaskLoader.java:91)
02-24 08:42:34.803: E/AndroidRuntime(986): at android.os.AsyncTask.finish(AsyncTask.java:631)
02-24 08:42:34.803: E/AndroidRuntime(986): at android.os.AsyncTask.access$600(AsyncTask.java:177)
02-24 08:42:34.803: E/AndroidRuntime(986): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
02-24 08:42:34.803: E/AndroidRuntime(986): at android.os.Handler.dispatchMessage(Handler.java:99)
02-24 08:42:34.803: E/AndroidRuntime(986): at android.os.Looper.loop(Looper.java:137)
02-24 08:42:34.803: E/AndroidRuntime(986): at android.app.ActivityThread.main(ActivityThread.java:5041)
02-24 08:42:34.803: E/AndroidRuntime(986): at java.lang.reflect.Method.invokeNative(Native Method)
02-24 08:42:34.803: E/AndroidRuntime(986): at java.lang.reflect.Method.invoke(Method.java:511)
02-24 08:42:34.803: E/AndroidRuntime(986): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-24 08:42:34.803: E/AndroidRuntime(986): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-24 08:42:34.803: E/AndroidRuntime(986): at dalvik.system.NativeStart.main(Native Method)
-----------编辑:找到解决方法
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
mAdapter.swapCursor(data);
patientsListView.post(new Runnable() {
@Override
public void run() {
showFirstItem();
}
});
}
答案 0 :(得分:1)
您要做的就是先拨打showFirstItem();
,然后拨打mAdapter.swapCursor(data);
。