Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/182.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 片段分离后游标加载程序不工作_Android_Android Fragments_Android Cursoradapter_Android Loader - Fatal编程技术网

Android 片段分离后游标加载程序不工作

Android 片段分离后游标加载程序不工作,android,android-fragments,android-cursoradapter,android-loader,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();

使用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使用光标适配器从手机和显示器读取联系人。问题是,这只在第一次起作用。当我选择其他选项卡并再次返回选择人员选项卡时,listview变为空。下面是这个片段

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);
}
}

不完全确定,但请尝试使用销毁onLoadFinished()中的加载程序

此“int id”是在“initLoader”中传递的整数值。为了可读性,请使用大于0的值

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);
}
getActivity().getSupportLoaderManager().destroyLoader(int id);