Android - 片段中的列表视图

时间:2016-05-11 17:30:05

标签: android listview android-fragments android-studio rss

我正在尝试解析RSS提要并通过片段中的Listview将其显示给用户。问题是列表中没有显示任何内容,我收到了一些错误:

V/Error Parsing Data: java.io.IOException: Couldn't open http://feeds.bbci.co.uk/news/england/rss.xml
W/FragmentManager: moveToState: Fragment state for Thingy{3633e4e #1 id=0x7f0c0069} not updated inline; expected state 3 found 2

My Fragments课程:

public class Thingy extends Fragment {
    ListView mList;
    ArrayAdapter<String> adapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View v = inflater.inflate(R.layout.fragment_main, container, false);
        adapter = new ArrayAdapter<>(getActivity(), R.layout.basic_list_item);
        mList = ((ListView) v.findViewById(R.id.listView));
        new GetRssFeed().execute("http://feeds.bbci.co.uk/news/england/rss.xml");

        return v;
    }

    private class GetRssFeed extends AsyncTask<String, Void, Void> {
        @Override
        protected Void doInBackground(String... params) {
            adapter.add("Pizza Steak");
            try {
                RssReader rssReader = new RssReader(params[0]);
                for (RssItem item : rssReader.getItems()) {
                    adapter.add(item.getTitle());
                }
            } catch (Exception e) {
                Log.v("Error Parsing Data", e + "");
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            adapter.notifyDataSetChanged();
            mList.setAdapter(adapter);
        }
    }
}

2 个答案:

答案 0 :(得分:1)

请在清单上添加权限

<manifest xlmns:android...>
...
<uses-permission android:name="android.permission.INTERNET" />
<application ...
</manifest>

答案 1 :(得分:0)

问题可能出在您使用的网址中,而不是有效的RSS Feed网址,

这样,尝试通过此网址运行您的代码:http://www.aljazeera.com/xml/rss/all.xml(仅用于测试)。