如何将我的字符串资源文件中的字符串数组添加到ListFragment的ListView中?

时间:2022-12-04 00:34:26

It's my understanding that a ListFragment is just a Fragment that automatically has a ListView in it's layout. I have an array of Strings in my XML string resource file, and I've got a class that extends ListFragment called UrlListFragment.

我的理解是ListFragment只是一个片段,它自动在其布局中有一个ListView。我的XML字符串资源文件中有一个字符串数组,我有一个扩展名为UrlListFragment的ListFragment的类。

Here's the array:

这是阵列:

<string-array name="list_array">
    <item>http://www.microsoft.com</item>
    <item>http://www.xbox.com</item>
    <item>http://www.windowsphone.com</item>
</string-array>

And here's my extened ListFragment:

这是我扩展的ListFragment:

public class UrlListFragment extends ListFragment {

private static String[] urlList;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
        Bundle savedInstanceState) {
        View view;
        Resources res = getActivity().getResources();
        urlList = res.getStringArray(R.array.list_array);
        view = super.onCreateView(inflater, container, savedInstanceState);
        return view;

    }
}

How can I add the array of strings to the ListFragment so that every entry in the array is an entry in the list?

如何将字符串数组添加到ListFragment中,以便数组中的每个条目都是列表中的条目?

1 个解决方案

#1


0  

A list fragment has a setAdapter() method.

列表片段具有setAdapter()方法。

You can create a simple adapter with:

您可以创建一个简单的适配器:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity().getApplicationContext(), android.R.layout.simple_list_item_1, urlList);

And then, you just have to do:

然后,你只需要这样做:

setListAdapter(adapter);

#1


0  

A list fragment has a setAdapter() method.

列表片段具有setAdapter()方法。

You can create a simple adapter with:

您可以创建一个简单的适配器:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity().getApplicationContext(), android.R.layout.simple_list_item_1, urlList);

And then, you just have to do:

然后,你只需要这样做:

setListAdapter(adapter);