1

我是android开发的初学者。对于一个项目,我想知道如何将列表视图实现到片段中?

在我的项目中,我有 3 个片段。

我必须检索手机中的所有联系人。(我正在使用这个例子=> http://samir-mangroliya.blogspot.fr/p/android-read-contact-and-display-in.html)。然后,我将它们放入一个片段中,其中包含一个列表视图。

我正在搜索许多示例,但它只是列表视图到 ACTIVITY 示例。

你能帮我么 ?

此致,

豆腐

PS:对不起我的英语不好,我是法国人:/

编辑

这是我的代码:

public class PhoneFrag extends ListFragment
{
private List<Contact>listecontact=new ArrayList<Contact>();

public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
}

public void onActivityCreated(Bundle savedInstanceState, Context context)
{
    super.onActivityCreated(savedInstanceState);

    String[]values=new String[]{};

    Cursor phones=context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,null,null,null);

    while(phones.moveToNext())
    {
        String name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
        String num=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

        Contact contact=new Contact();
        contact.setName(name);
        contact.setNum(num);

        listecontact.add(contact);
    }
    phones.close();

    ArrayAdapter<String>adapter=new ArrayAdapter<String>(getActivity(), android.R.layout.simple_expandable_list_item_2, listecontact);
    setListAdapter(adapter);
}

我在倒数第三行有一个错误:

构造函数 ArrayAdapter(FragmentActivity, int, List) 未定义

你能帮我吗 ?

豆腐

4

1 回答 1

3

让您的 Fragment 扩展ListFragment而不是 .Fragment如果您已经使用或查看了示例,ListActivity则不再需要将此片段与 xml 布局文件相关联setContentView(layout),Android 会在其中创建一个现成Fragment的,以方便您使用。而不是像这样的ListView方便方法setListAdapter(adapter)可供您使用。但是,如果您想访问ListView实例,请使用.The getListView()Fragment 似乎用于onActivityCreated执行此操作:

我猜你对片段的了解足以理解你this的.ListActivitygetActivity()ListFragment

这是我在Android 片段文档中找到的代码:

 public static class TitlesFragment extends ListFragment {
boolean mDualPane;
int mCurCheckPosition = 0;

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

    // Populate list with our static array of titles.
    setListAdapter(new ArrayAdapter<String>(getActivity(),
            android.R.layout.simple_list_item_activated_1, Shakespeare.TITLES));

    // Check to see if we have a frame in which to embed the details
    // fragment directly in the containing UI.
    View detailsFrame = getActivity().findViewById(R.id.details);
    mDualPane = detailsFrame != null && detailsFrame.getVisibility() == View.VISIBLE;

    if (savedInstanceState != null) {
        // Restore last state for checked position.
        mCurCheckPosition = savedInstanceState.getInt("curChoice", 0);
    }

    if (mDualPane) {
        // In dual-pane mode, the list view highlights the selected item.
        getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        // Make sure our UI is in the correct state.
        showDetails(mCurCheckPosition);
    }
}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putInt("curChoice", mCurCheckPosition);
}

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    showDetails(position);
}

/**
 * Helper function to show the details of a selected item, either by
 * displaying a fragment in-place in the current UI, or starting a
 * whole new activity in which it is displayed.
 */
void showDetails(int index) {
    mCurCheckPosition = index;

    if (mDualPane) {
        // We can display everything in-place with fragments, so update
        // the list to highlight the selected item and show the data.
        getListView().setItemChecked(index, true);

        // Check what fragment is currently shown, replace if needed.
        DetailsFragment details = (DetailsFragment)
                getFragmentManager().findFragmentById(R.id.details);
        if (details == null || details.getShownIndex() != index) {
            // Make new fragment to show this selection.
            details = DetailsFragment.newInstance(index);

            // Execute a transaction, replacing any existing fragment
            // with this one inside the frame.
            FragmentTransaction ft = getFragmentManager().beginTransaction();
            if (index == 0) {
                ft.replace(R.id.details, details);
            } else {
                ft.replace(R.id.a_item, details);
            }
            ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
            ft.commit();
        }

    } else {
        // Otherwise we need to launch a new activity to display
        // the dialog fragment with selected text.
        Intent intent = new Intent();
        intent.setClass(getActivity(), DetailsActivity.class);
        intent.putExtra("index", index);
        startActivity(intent);
    }
}

}

您还可以使用 和 之类的方法setEmptyTextsetListShown显示一条消息,以防您的光标没有行并分别切换您的 listView 的可见性。此外,Android 的 Loader Framework 仅支持 Fragment 而不是支持库中的 Activity。

于 2013-11-14T11:24:03.597 回答