我正在尝试实施此RSS阅读器:http://intel-software-academic-program.com/courses/mobile/android/IntelAcademic_AndroidEPITECH_05_Flux_RSS_FR.pdf
在我的片段上:
但我在这一行上收到错误:
adapter = new NewsAdapter(this, new ArrayList<News>());
NewsAdapter中的&#34; NewsAdapter(Android.content.context,List)无法应用于(包,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);
}
}
任何人都可以帮助我?
答案 0 :(得分:0)
如果要下载xml或json文件,仍需要在Android清单文件中写入网络权限。 (外部申请标签)
&LT; uses-permission android:name =&#34; android.permission.INTERNET&#34; /&GT;