ListFragment点击被错误的片段回答

时间:2012-12-10 09:08:34

标签: android android-fragments listadapter android-pageradapter

我有一系列ListFragments,每个显示不同的SQLite表或视图。我用可跳过的标签布局在它们之间切换。

我有两个不同的可转换布局加载到同一个活动中,具体取决于调用的Intent参数。 alertdialog和操作栏上的按钮可切换可切换布局。可转换选项卡设置描述为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

Exception显示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。我在整个项目中保持兼容模式。

1 个答案:

答案 0 :(得分:1)

此声明:

private static Uri logView, logTable;

表示您的LogFragment始终会将Uris Uris传递给由FragmentPagerAdapter构建的最后一个片段,因为这些静态变量将由您共享LogFragment个实例。将这些字段设为实例变量:

private Uri logView, logTable;