2

在我的应用程序中,4 个片段附加到一个活动类。在我的活动类中,我使用它设置了根内容视图

setContentView(R.layout.fragment_pager);

我的四个片段位于单独的 java 文件中。

在我的一个片段中,我正在显示我使用的联系人列表

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);
setListShown(true);

我想知道在我的情况下 setListAdapter 的用途是什么以及它将 SimpleCursorAdapter 类给出的内容列表放在哪里?以及如何设置片段的标题。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:drawable/gallery_thumb">

    <TextView android:id="@+id/text"
        android:layout_width="match_parent" android:layout_height="wrap_content"
        android:gravity="center_vertical|center_horizontal"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="@string/hello_world"/>

    <!-- The frame layout is here since we will be showing either
    the empty view or the list view.  -->
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="1" >
        <!-- Here is the list. Since we are using a ListActivity, we
             have to call it "@android:id/list" so ListActivity will
             find it -->
        <ListView android:id="@android:id/list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:drawSelectorOnTop="false"/>

        <!-- Here is the view to show if the list is emtpy -->
        <TextView android:id="@android:id/empty"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="No items."/>

    </FrameLayout>

</LinearLayout>



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

         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);
        // setListShown(true);
        // getLoaderManager().initLoader(0, null, this);
    }    

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_pager_list, container, false);
        View tv = v.findViewById(R.id.text);
        ((TextView)tv).setText("Contacts");

        return v;
    }

当我使用此方法并删除两个方法 onCreate 和 onCreateView 时,我的光标显示数据

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

       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); 
       setListShown(true);
       getLoaderManager().initLoader(0, null, this);

    }

但是当我同时使用这三种方法时,我的应用程序会意外停止。当我同时使用这三种方法时会出现什么错误?

package com.keepintouch.android;

import android.os.Bundle;

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;

import android.provider.ContactsContract;

import android.support.v4.app.Fragment;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;

import android.support.v4.widget.SimpleCursorAdapter;
import android.util.Log;

import android.view.View;


import android.widget.ListView;


import com.actionbarsherlock.app.SherlockListFragment;


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;  
    View lv;

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

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

    }    




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


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

       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); 
       setListShown(true);
       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);
    }



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



}
4

1 回答 1

2

如果您使用 Listfragement setListAdapter 设置默认列表中的 Adapter 随附 Listfragement..

如果使用操作栏 (android:targetSdkVersion="15")

ActionBar ab =  this.getActionBar();
ab.setTitle("       ");

如果不是那么

  getActivity().setTitle("     "); // after onAttach called
于 2012-06-08T08:09:29.073 回答