Question

[UPDATE]

The first problem is solved but there is no data printed yet.

If i uncomment the declaration of values variable, and i replace setAdapter with values , it works fine, but with data's received from rss feed,i doesn't work.

Here is the updated code :

public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        //      String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
        //              "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
        //              "Linux", "OS/2" };

        Thread thread = new Thread(new Runnable(){
            @Override
            public void run() {
                try {
                    URL rssUrl = new URL("http://feeds.feedburner.com/Android-er?format=xml");
                    SAXParserFactory mySAXParserFactory = SAXParserFactory.newInstance();
                    SAXParser mySAXParser = mySAXParserFactory.newSAXParser();
                    XMLReader myXMLReader = mySAXParser.getXMLReader();
                    RSSHandler myRSSHandler = new RSSHandler();
                    myXMLReader.setContentHandler(myRSSHandler);
                    InputSource myInputSource = new InputSource(rssUrl.openStream());
                    myXMLReader.parse(myInputSource);

                } catch (MalformedURLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (ParserConfigurationException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (SAXException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });

        thread.start();

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

I try to load rss feed in a listview of ListFragment.

I am getting this error :

The constructor ArrayAdapter(DernieresNewsFragment, int, List) is undefined

Could you help me? Is it a good way to retrieve xml data like this ?

Here is the code of my dernieres_news_fragment.xml :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:drawable/dialog_holo_light_frame"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/listview_dernieres_news"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
</RelativeLayout>

And my item_list.xml :

<?xml version="1.0" encoding="utf-8"?>
<TextView
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/rowtext"
 android:layout_width="fill_parent"
 android:layout_height="25px"
 android:textSize="10sp" />

And finally my DernieresNewsFragment.java :

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;

import android.app.Fragment;
import android.app.ListFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;


public class DernieresNewsFragment extends ListFragment {

    private List<String> item = new ArrayList<String>();

    public DernieresNewsFragment() {
        // Empty constructor required for fragment subclasses
    }

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

        View rootView = inflater.inflate(R.layout.dernieres_news_fragment, container, false);

        try {
            URL rssUrl = new URL("http://feeds.feedburner.com/Android-er?format=xml");
            SAXParserFactory mySAXParserFactory = SAXParserFactory.newInstance();
            SAXParser mySAXParser = mySAXParserFactory.newSAXParser();
            XMLReader myXMLReader = mySAXParser.getXMLReader();
            RSSHandler myRSSHandler = new RSSHandler();
            myXMLReader.setContentHandler(myRSSHandler);
            InputSource myInputSource = new InputSource(rssUrl.openStream());
            myXMLReader.parse(myInputSource);

        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ParserConfigurationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SAXException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        ArrayAdapter<String> itemList = new ArrayAdapter<String>(this, R.layout.item_list, item);
        setListAdapter(itemList);

        return rootView;
    }

    private class RSSHandler extends DefaultHandler {
        final int stateUnknown = 0;
        final int stateTitle = 1;
        int state = stateUnknown;

        @Override
        public void startDocument() throws SAXException {
            // TODO Auto-generated method stub
        }

        @Override
        public void endDocument() throws SAXException {
            // TODO Auto-generated method stub
        }

        @Override
        public void startElement(String uri, String localName, String qName,
                Attributes attributes) throws SAXException {
            // TODO Auto-generated method stub
            if (localName.equalsIgnoreCase("title"))
            {
                state = stateTitle;
            }
            else
            {
                state = stateUnknown;
            }
        }

        @Override
        public void endElement(String uri, String localName, String qName)
                throws SAXException {
            // TODO Auto-generated method stub
            state = stateUnknown;
        }

        @Override
        public void characters(char[] ch, int start, int length)
                throws SAXException {
            // TODO Auto-generated method stub
            String strCharacters = new String(ch, start, length);
            if (state == stateTitle)
            {
                item.add(strCharacters);

            }
        }
    }
}
Was it helpful?

Solution

Use this:

 ArrayAdapter<String> itemList = new ArrayAdapter<String>(getActivity(), R.layout.item_list, item);

instead of this:

ArrayAdapter<String> itemList = new ArrayAdapter<String>(this, R.layout.item_list, item);

You have to pass in a Context.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top