Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing RSS on Android

Tags:

java

android

rss

I've got a couple RSS feeds I need to parse for my app and I followed the excellent tutorial here: http://w2davids.wordpress.com/android-rssatom-feeds-parsing-with-rome/. I modified the sample a bit and got it to do what I needed. So, I went to integrate it into my app and consistently get a Force Quit every time I try to display the list of posts. My modified version of the code is below. It always seems to fail when attaching the adapter to the ListView, so I assume my adapter is setup incorrectly. What's strange us that in the debugger, I can look at the adapter and see the data I'm trying to fetch.

Thanks in advance:

public class View1 extends Activity
{
    /** Called when the activity is first created. */
    private final ArrayList<String> list = new ArrayList<String>();
    private ListView listView;
    private ArrayAdapter<String> adapter = null;
    private SyndContent desc;
    private ArrayList<String> d = new ArrayList<String>() ;

    @Override
    public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            adapter = new ArrayAdapter<String>(this, R.layout.dataview, R.id.ListItemView);             
            getRSS("http://www.example.com/wp/?feed=gigpress&artist=1");
            listView.setAdapter(adapter);


            listView = (ListView) this.findViewById(R.id.ListView);

            listView.setOnItemClickListener(new OnItemClickListener()
                {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long duration)
                        {                               
                            Intent intent = new Intent();
                            intent.setClassName("com.example.idtb", "com.example.idtb.ViewRssDescription");
                            intent.putExtra("desc", d.get(position));
                            startActivity(intent);
                        }
                });             

        }

    private void getRSS(String rss)
        {

            URL feedUrl;
            try
                {
                    Log.d("DEBUG", "Entered:" + rss);
                    feedUrl = new URL(rss);

                    SyndFeedInput input = new SyndFeedInput();
                    SyndFeed feed = input.build(new XmlReader(feedUrl));
                    List entries = feed.getEntries();

                    Iterator iterator = entries.listIterator();
                    while (iterator.hasNext())
                        {
                            SyndEntry ent = (SyndEntry) iterator.next();
                            String title = ent.getTitle();
                            desc = ent.getDescription();
                            d.add(desc.getValue());
                            adapter.add(title);
                        }
                    adapter.notifyDataSetChanged();

                }
            catch (MalformedURLException e)
                {
                    e.printStackTrace();
                }
            catch (IllegalArgumentException e)
                {
                    e.printStackTrace();
                }
            catch (FeedException e)
                {
                    e.printStackTrace();
                }
            catch (IOException e)
                {
                    e.printStackTrace();
                }
        }
}
like image 357
voodoobilly Avatar asked Nov 15 '22 04:11

voodoobilly


1 Answers

I think adapter should look something like this:

final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, lst); 
like image 171
ninjasense Avatar answered Dec 05 '22 00:12

ninjasense