片段分离后,游标加载器无法正常工作

时间:2015-05-18 13:47:54

标签: android android-fragments android-cursoradapter android-loader

使用PagerSlidingTabStrip在标签中呈现片段

@Override
        public Fragment getItem(int position) {

            if (position == 0)
                return new FlipActivity();
            else if (position == 1)
                return new CategoryFragment();
            else if (position == 2)
                return new PeopleTabFragment();
            else if (position == 3)
                return new MessageTabFragment();
            else if (position == 4)
                return new HistorytabFragment();
            return new SuperAwesomeCardFragment().newInstance(position);

        }

PeopleTabFragment使用游标适配器从手机和显示器上读取联系人。问题是,这只是第一次工作。当我选择其他选项卡并再次选择人员选项卡时,列表视图将变为空。片段在下面。

public class PeopleTabFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor> {
PeopleAdapter peopleAdapter;

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.contact_layout, container, false);
    StickyListHeadersListView listView = (StickyListHeadersListView) rootView.findViewById(R.id.list);
    getActivity().getSupportLoaderManager().initLoader(0, savedInstanceState, this);
    peopleAdapter = new PeopleAdapter(getActivity(), null, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
    listView.setAdapter(peopleAdapter);

    return rootView;
}

@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
    CursorLoader cursorLoader = new CursorLoader(getActivity(), ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, "upper(" + ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + ") ASC");
    return cursorLoader;
}

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
    peopleAdapter.load(data);
    peopleAdapter.swapCursor(data);
}

@Override
public void onLoaderReset(Loader<Cursor> loader) {
    peopleAdapter.swapCursor(null);
}
}

这是适配器

public class PeopleAdapter extends CursorAdapter implements StickyListHeadersAdapter {

ArrayList checkerList = new ArrayList();
LayoutInflater mLayoutInflater;

public PeopleAdapter(Context context, Cursor c, int flags) {
    super(context, c, flags);

    mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}


public void load(Cursor c) {
    while (c.moveToNext()) {
        String name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
        checkerList.add(name);
    }
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    return mLayoutInflater.inflate(R.layout.people_item, parent, false);
}

@Override
public void bindView(View view, Context context, Cursor cursor) {
    String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
    TextView nameTextView = (TextView) view.findViewById(R.id.name);
    ImageView imageView = (ImageView) view.findViewById(R.id.contactImage);
    String uri = cursor.getString(cursor
            .getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_URI));
    try {
        imageView.setImageURI(Uri.parse(uri));
    } catch (Exception e) {
        imageView.setImageDrawable(context.getResources().getDrawable(R.drawable.ic_launcher));
    }
    nameTextView.setText(name);
}

@Override
public View getHeaderView(int position, View convertView, ViewGroup viewGroup) {
    FunctionHolder holder;
    if (convertView == null) {
        holder = new FunctionHolder();
        convertView = mLayoutInflater.inflate(R.layout.people_header, viewGroup, false);
        holder.functionButton = (TextView) convertView.findViewById(R.id.text);
        convertView.setTag(holder);
    } else {
        holder = (FunctionHolder) convertView.getTag();
    }
    //set people_header text as first char in name
    String headerText = "" + checkerList.get(position).toString().subSequence(0, 1).charAt(0);
    holder.functionButton.setText(headerText);
    return convertView;
}

@Override
public long getHeaderId(int position) {
    return checkerList.get(position).toString().subSequence(0, 1).charAt(0);
}

1 个答案:

答案 0 :(得分:0)

不完全确定,但是你可以尝试使用

在onLoadFinished()中破坏你的装载程序
getActivity().getSupportLoaderManager().destroyLoader(int id);

这个'int id'是你在'initLoader'中传递的整数值。为了便于阅读,请使用大于0的某个值。