-5

i'm trying to implement this RSS Reader: http://intel-software-academic-program.com/courses/mobile/android/IntelAcademic_AndroidEPITECH_05_Flux_RSS_FR.pdf

On my fragment:

But i get an error on this line:

adapter = new NewsAdapter(this, new ArrayList<News>());

"NewsAdapter (Android.content.context, List) in NewsAdapter cannot be applied to (package, ArrayList

Actualites.java:

import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import org.xmlpull.v1.XmlPullParserException;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class Actualites extends android.support.v4.app.Fragment{
    AsyncTask<Void, Void, List> a = null;
    NewsAdapter adapter = null;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View view = inflater.inflate(R.layout.activity_actualites, container, false);
        ListView list = (ListView) view.findViewById(R.id.newsList);
        adapter = new NewsAdapter(this, new ArrayList<News>());
        list.setAdapter(adapter);
        a = new AsyncTask<Void, Void, List>() {
            @Override
            protected List doInBackground(Void... params) {
                ArrayList<News> res = new ArrayList<News>();
                try {
                    URL url = new URL("http://feeds.lefigaro.fr/c/32266/f/438191/index.rss");
                    HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                    RssParser parser = new RssParser();
                    try {
                        return parser.parse(urlConnection.getInputStream());
                    } catch (XmlPullParserException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return res;
            }

            @Override
            protected void onPostExecute(List result) {
                adapter.update(result);
            }
        };
        a.execute();
        return view;
    }
}

RssParser.java:

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import android.util.Xml;

public class RssParser {
    private String readText(XmlPullParser parser) throws IOException, XmlPullParserException {
        String result = "";
        if (parser.next() == XmlPullParser.TEXT) {
            result = parser.getText();
            parser.nextTag();
        }
        return result;
    }
    private static final String ns = null;

    private void skip(XmlPullParser parser) throws XmlPullParserException, IOException {
        if (parser.getEventType() != XmlPullParser.START_TAG) {
            throw new IllegalStateException();
        }
        int depth = 1;
        while (depth != 0) {
            switch (parser.next()) {
                case XmlPullParser.END_TAG:
                    depth--;
                    break;
                case XmlPullParser.START_TAG:
                    depth++;
                    break;
            }
        }
    }

    public ArrayList<News> parse(InputStream in) throws XmlPullParserException, IOException {
        XmlPullParser parser = Xml.newPullParser();
        parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
        parser.setInput(in, null);
        parser.nextTag();
        ArrayList<News> entries = new ArrayList<News>();

        parser.require(XmlPullParser.START_TAG, ns, "rss");
        parser.nextTag();
        parser.require(XmlPullParser.START_TAG, ns, "channel");
        while (parser.next() != XmlPullParser.END_TAG) {
            if (parser.getEventType() != XmlPullParser.START_TAG) {
                continue;
            }
            String name = parser.getName();
            if (name.equals("item")) {
                String title = "";
                String content = "";
                while (parser.next() != XmlPullParser.END_TAG) {
                    if (parser.getEventType() != XmlPullParser.START_TAG) {
                        continue;
                    }
                    name = parser.getName();
                    if (name.equals("title")) {
                        title = readText(parser);
                    } else if (name.equals("description")) {
                        content = readText(parser);
                    } else
                        skip(parser);
                }
                entries.add(new News(title, content));
            }  else
                skip(parser);
        }
        return entries;
    }
}

NewsAdapter.java:

import java.util.List;
import android.content.Context;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class NewsAdapter extends BaseAdapter {
    List<News> news;
    Context context;

    public NewsAdapter(Context context, List<News> news) {
        this.news = news;
        this.context = context;
    }

    public void update(List<News> news) {
        this.news = news;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return news.size();
    }

    @Override
    public Object getItem(int arg0) {
        return news.get(arg0);
    }

    @Override
    public long getItemId(int arg0) {
        return arg0;
    }

    @Override
    public View getView(int arg0, View arg1, ViewGroup arg2) {
        LayoutInflater li = LayoutInflater.from(context);
        View v = li.inflate(android.R.layout.two_line_list_item, null);
        TextView tv1 = (TextView)v.findViewById(android.R.id.text1);
        TextView tv2 = (TextView)v.findViewById(android.R.id.text2);
        News n = (News)getItem(arg0);
        tv1.setText(n.getTitle());
        String content = Html.fromHtml(n.getContent()).toString().replace((char) 65532, (char) 32).trim();
        tv2.setText(content);
        return v;
    }
}

News.java:

public class News {
    private String title;
    private String content;

    public News(String title, String content) {
        this.title = title;
        this.content = content;
    }

    public String getTitle() {
        return (title);
    }

    public String getContent() {
        return (content);
    }
}

Anyone can help me ?

benjyspider
  • 245
  • 2
  • 4
  • 16
  • 3
    *Or have a better way to do an Rss Reader ?* first java's basics ... it is compile time error so it means that you don't know java. obviously Fragment class is not connected to the Context class in the way as it should, and NewsAdapter's constructor expects Context class not Fragment class – Selvin May 25 '15 at 12:45
  • insted of `this` use `getActivity()` – hrskrs May 25 '15 at 12:46
  • @Selvin Ive learned Java a little at school but its look so different to me for mobile, the error came from my extends but how to do, i have to use fragments... – benjyspider May 25 '15 at 12:47
  • @hrskrs thanks do you know why i don't thee the news on my app ? I get 0 errors on my logcat... – benjyspider May 25 '15 at 12:57

1 Answers1

0

If you want to download a xml or json file, you still need to write a network permission in the Android manifest file. (outside application tag)

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

Davy
  • 61
  • 7
  • I think it's better to run asynctask first and then set in OnPostExecuteyou adapter; but I don't know if method update or notifydatasetchanged does anything? I think now you have an empty List news before you run your asynctask, maybe that's why the list is empty. In your code you set new adapter = new NewsAdapter(this, new ArrayList()); but in your asynctask you set the variabel "res". So in OnPostExecute it should say: adapter = new NewsAdapter(this, res); list.setAdapter(adapter) – Davy May 25 '15 at 13:33
  • I cant put a.execute(); before – benjyspider May 25 '15 at 13:48
  • java.net.UnknownHostException: Unable to resolve host "feeds.lefigaro.fr": No address associated with hostname – benjyspider May 25 '15 at 13:50
  • is your device (hardware, not manifest this time) connected to Internet in it's settings ... ==> wifi on? – Davy May 25 '15 at 13:58
  • Stop ! I forgot "i" on perm"i"ssion lol ! Thank you so much ! – benjyspider May 25 '15 at 14:00
  • Do u know how i can put a link on the nows for open the article in the navigator ? – benjyspider May 25 '15 at 14:01
  • I don't quite understand, but I guess you mean to open a page when you click something in the list, so I guess you need an OnItemClick on your listview, so list.OnItemClick(new AdapterView.OnItemClick, just hit Enter in Android Studio when intellisense come's up to autogenerate full code and then come up with your own java code – Davy May 25 '15 at 14:08
  • I see what is it !! Im gonna do some search if i cant do it, thanks again ! :) – benjyspider May 25 '15 at 14:19
  • Cant do list.setOnItemClickListener in the try bloc :( because list is outside – benjyspider May 25 '15 at 14:49