我有一系列ListFragments
每个显示不同的 SQLite 表或视图。我使用可滑动的标签布局在它们之间切换。
我有两个不同的可滑动布局加载到同一个活动中,具体取决于调用的 Intent 参数。警报对话框和操作栏上的按钮切换可滑动布局。可滑动标签设置在http://www.pushing-pixels.org/2011/08/11/android-tips-and-tricks-swipey-tabs.html中进行了描述。
单击项目后会出现问题。
android.database.sqlite.SQLiteException: no such column: authorHash: ,
while compiling: SELECT name, shared, authorHash, primaryMatch
FROM contacts_view
异常表明 SELECT 子句来自下面的第一个声明,而 VIEW 来自第二个声明。
LogFragment.newInstance(PeerDataProvider.URI_RESPONSES_VIEW,
PeerDataProvider.URI_PEERDATA_TABLE, R.loader.responseslogfragloader,
PeerCreatedDataColumns.NAME,
new String[] { PeerCreatedDataColumns.NAME,
PeerCreatedDataColumns.SHARED, PeerCreatedDataColumns.AUTHORHASH,
ResponsesColumns.PRIMARYMATCH });
LogFragment.newInstance(UserDataProvider.URI_CONTACTS_VIEW,
UserDataProvider.URI_USERDATA_TABLE, R.loader.contactslogfragloader,
UserCreatedDataColumns.NAME,
new String[] { UserCreatedDataColumns.LONGSUMMARY });
这是被调用的片段:
public class LogFragment extends ListFragment implements LoaderManager.LoaderCallbacks<Cursor> {
private static int LIST_LOADER;
protected int layout = R.layout.log_fragment;
int entryLayout = R.layout.list_item;
private static Uri logView, logTable;
String[] listItemProjection;
int[] listItemFields = { R.id.title };
String itemClickProjection[];
String[] createProjection;
int mCurCheckPosition = 0;
Button clearLogButton;
protected SimpleCursorAdapter listAdapter;
public static Fragment newInstance(Uri logview, Uri logtable, int listLoader, String displayColumn,
String[] clickProjection) {
LogFragment f = new LogFragment();
Bundle args = new Bundle();
args.putString("listItemProjection", "" + displayColumn);
args.putStringArray("itemClickProjection", clickProjection);
args.putString("logView", "" + logview);
args.putString("logTable", "" + logtable);
args.putString("LIST_LOADER", "" + listLoader);
Log.d(DEBUG_TAG, "listLoader" + listLoader);
f.setArguments(args);
return f;
}
public LogFragment() {
super();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(layout, container, false);
String listItemProjection_String = getArguments().getString("listItemProjection");
String[] itemClickProjection_StringArray = getArguments().getStringArray("itemClickProjection");
String logView_String = getArguments().getString("logView");
String logTable_String = getArguments().getString("logTable");
String LIST_LOADER_String = getArguments().getString("LIST_LOADER");
listItemProjection = new String[] { listItemProjection_String };
createProjection = new String[] { UserDatabase._ID, listItemProjection_String };
itemClickProjection = itemClickProjection_StringArray;
logView = Uri.parse(logView_String);
logTable = Uri.parse(logTable_String);
LIST_LOADER = Integer.parseInt(LIST_LOADER_String);
getLoaderManager().initLoader(LIST_LOADER, null, this);
listAdapter = new SimpleCursorAdapter(getActivity().getApplicationContext(), entryLayout, null,
listItemProjection, listItemFields, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
setListAdapter(listAdapter);
return view;
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
Cursor c = getActivity().getContentResolver().query(logView, itemClickProjection, null, null, null);
String fields = "";
c.moveToPosition(position);
for (String column : itemClickProjection) {
fields += column + " " + c.getString(c.getColumnIndex(column)) + "\n";
Toast.makeText(getActivity(), fields, Toast.LENGTH_SHORT).show();
}
}
}
这是适配器
private class SearchSwipePagerAdapter extends FragmentPagerAdapter implements SwipeyTabsAdapter {
private final String DEBUG_TAG = "SearchSwipePagerAdapter";
private final Context mContext;
public SearchSwipePagerAdapter(Context context, FragmentManager fm) {
super(fm);
this.mContext = context;
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return SearchFragment.newInstance();
case 1:
return ResponsesFragment.newInstance();
case 2:
return LogFragment.newInstance(PeerDataProvider.URI_RESPONSES_VIEW,
PeerDataProvider.URI_PEERDATA_TABLE, R.loader.responseslogfragloader,
PeerCreatedDataColumns.NAME, new String[] { PeerCreatedDataColumns.NAME,
PeerCreatedDataColumns.SHARED, PeerCreatedDataColumns.AUTHORHASH,
ResponsesColumns.PRIMARYMATCH });
case 3:
return LogFragment.newInstance(UserDataProvider.URI_CONTACTS_VIEW, UserDataProvider.URI_USERDATA_TABLE,
R.loader.contactslogfragloader, UserCreatedDataColumns.NAME,
new String[] { UserCreatedDataColumns.LONGSUMMARY });
case 4:
return LogFragment.newInstance(PeerDataProvider.URI_METHODS_VIEW, PeerDataProvider.URI_PEERDATA_TABLE,
R.loader.methodslogfragloader, PeerCreatedDataColumns.NAME, new String[] {
PeerCreatedDataColumns.NAME, PeerCreatedDataColumns.SHARED,
PeerCreatedDataColumns.AUTHORHASH, MethodsColumns.PRIMARYMATCH });
default:
return SwipeyTabFragment.newInstance(WORKFLOW[position]);
}
}
每个片段都有一个唯一的加载器 ID。我在整个项目中保持兼容模式。