我有一个使用 CursorLoader 填充的 ListFragment。填充 ListFragment 后,我有一个 OnItemClickListener 方法,我想在该方法中识别用户选择的 List Fragment 中的哪个项目。我该怎么做呢?我试过了:
String item = getListAdapter().getItem(position);
String item = getListAdapter().getItem(position).toString();
和
String item = (String) ((Fragment) getListAdapter().getItem(position)).getString(0);
其中 position 是传递给 onClickItemListener 方法的整数,如下所示:
public void onListItemClick(ListView l, View v, int position, long id)
所有这些都会抛出一种或另一种类型的强制转换异常。我难住了。这似乎是一件简单的事情。供您参考,以下是列表片段的填充方式:
private SimpleCursorAdapter mAdapter;
private static final String[] PROJECTION = new String[] { "_id", "stitchname" };
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Intent myData = getActivity().getIntent();
Bundle info = myData.getExtras();
String[] dataColumns = { "stitchname" };
int[] viewIDs = { R.id.stitchlist1 };
mAdapter = new SimpleCursorAdapter(getActivity(), R.layout.stitchlist, null, dataColumns, viewIDs, 0);
setListAdapter(mAdapter);
getLoaderManager().initLoader(0, info, (LoaderCallbacks<Cursor>) this);
}
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
String selection = "stitchlevel=?";
String[] selectionArgs = new String[] {args.getString("Level")};
return (Loader<Cursor>) new CursorLoader(getActivity(), STITCHES_URI, PROJECTION, selection, selectionArgs, null);
}
任何建议都将受到欢迎,谢谢!