gpt4 book ai didi

android - performItemClick onLoadFinished 后的第一个 ListView 项

转载 作者:行者123 更新时间:2023-11-29 01:45:00 24 4
gpt4 key购买 nike

我正在使用 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();
}
});
}

最佳答案

您要做的是先调用 showFirstItem();,然后再调用 mAdapter.swapCursor(data);

关于android - performItemClick onLoadFinished 后的第一个 ListView 项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21983163/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com