Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/180.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 FragmentPagerAdapter-为每个片段创建动态内容_Android_Actionbarsherlock - Fatal编程技术网

Android FragmentPagerAdapter-为每个片段创建动态内容

Android FragmentPagerAdapter-为每个片段创建动态内容,android,actionbarsherlock,Android,Actionbarsherlock,我已经在我的应用程序中实现了FragmentPagerAdapter,但每当我刷卡时,它只为每个片段显示相同的项目列表。我正在使用SherlockFragmentActivity,每当我在FragmentPager中滑动到下一个或上一个片段时,我想为每个片段显示不同的非静态页面 我怎样才能做到 我是否列出了一个联系人列表,其中没有当前片段的任何标题,这样我就可以知道这是我片段中使用此代码的联系人片段 public class ContactsFragment extends SherlockLi

我已经在我的应用程序中实现了FragmentPagerAdapter,但每当我刷卡时,它只为每个片段显示相同的项目列表。我正在使用SherlockFragmentActivity,每当我在FragmentPager中滑动到下一个或上一个片段时,我想为每个片段显示不同的非静态页面

我怎样才能做到

我是否列出了一个联系人列表,其中没有当前片段的任何标题,这样我就可以知道这是我片段中使用此代码的联系人片段

public class ContactsFragment extends SherlockListFragment implements LoaderManager.LoaderCallbacks<Cursor>{

     // This is the Adapter being used to display the list's data.
    SimpleCursorAdapter mAdapter;

    // If non-null, this is the current filter the user has provided.
    String mCurFilter;



    public static Fragment newInstance(Context context){
        ContactsFragment contactFragment = new ContactsFragment();
        return contactFragment;
    }


    @Override 
    public void onActivityCreated(Bundle savedInstanceState) {
       super.onActivityCreated(savedInstanceState);

        // Give some text to display if there is no data.  In a real
        // application this would come from a resource.
       // setEmptyText("No phone numbers");


        // Create an empty adapter we will use to display the loaded data.

        mAdapter = new SimpleCursorAdapter(getActivity(),
                android.R.layout.simple_list_item_1, null,
                new String[] {ContactsContract.Contacts.DISPLAY_NAME},
                new int[] { android.R.id.text1}, 0);


        setListAdapter(mAdapter);

        // Start out with a progress indicator.
        setListShown(true);

        // Prepare the loader.  Either re-connect with an existing one,
        // or start a new one.
        getLoaderManager().initLoader(0, null, this);

    }



    @Override public void onListItemClick(ListView l, View v, int position, long id) {
        // Insert desired behavior here.
        Log.i("FragmentComplexList", "Item clicked: " + id);
    }

    // These are the Contacts rows that we will retrieve.
    static final String[] CONTACTS_SUMMARY_PROJECTION = new String[] {
        ContactsContract.Contacts._ID,
        ContactsContract.Contacts.DISPLAY_NAME,
    };

    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
         // This is called when a new Loader needs to be created.  This
        // sample only has one Loader, so we don't care about the ID.
        // First, pick the base URI to use depending on whether we are
        // currently filtering.
        Uri baseUri;
        if (mCurFilter != null) {
            baseUri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_FILTER_URI,
                      Uri.encode(mCurFilter));
        } else {
            baseUri = ContactsContract.Contacts.CONTENT_URI;
        }

        // Now create and return a CursorLoader that will take care of
        // creating a Cursor for the data being displayed.
        String select = "((" + ContactsContract.Contacts.DISPLAY_NAME + " NOTNULL) AND ("
                + ContactsContract.Contacts.HAS_PHONE_NUMBER + "=1) AND ("
                + ContactsContract.Contacts.DISPLAY_NAME + " != '' ))";
        return new CursorLoader(getActivity(), baseUri,
                CONTACTS_SUMMARY_PROJECTION, select, null,
                ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");
    }

    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
        // Swap the new cursor in.  (The framework will take care of closing the
        // old cursor once we return.)
        mAdapter.swapCursor(data);

        // The list should now be shown.
        if (isResumed()) {
            setListShown(true);
        } else {
            setListShownNoAnimation(true);
        }
    }

    public void onLoaderReset(Loader<Cursor> loader) {
        // This is called when the last Cursor provided to onLoadFinished()
        // above is about to be closed.  We need to make sure we are no
        // longer using it.
        mAdapter.swapCursor(null);
    }


}
但当我运行我的应用程序“你的应用程序突然停止”时,这个弹出窗口出现了


没有这些方法,我的应用程序运行良好,但没有任何片段标题。有什么问题吗?

您好,您可以使用此处的查看寻呼机来实现此功能

您可以在one.xml布局中使用它

<ListView android:id="@+id/List" android:background="#00000000"
    android:scrollbars="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:scrollingCache="false"
    android:fastScrollEnabled="true"
    android:cacheColorHint="@color/white"
     />



但在LayoutOne片段中,您只是显示一个R.layout.layout\u-one xml文件,而没有将其附加到包含一些数据的适配器上。如何使用此适配器在xml文件列表中显示使用假设您的layout\u-one.xml mAdapter=new SimpleCorsorAdapter(getActivity(),android.R.layout.simple_list_item_1,null,新字符串[]{Contacts contract.Contacts.DISPLAY_NAME},新int[]{android.R.id.text1},0);我的片段类是公共类Contacts片段扩展SherlockListFragment实现LoaderManager.LoaderCallbacks
<ListView android:id="@+id/List" android:background="#00000000"
    android:scrollbars="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:scrollingCache="false"
    android:fastScrollEnabled="true"
    android:cacheColorHint="@color/white"
     />