我想在列表視圖中顯示從RSS提要網站解析的標題和說明。現在它只能顯示標題。我已經完成了描述的提取。我需要在標題下面顯示說明。我怎樣才能做到這一點 ?如何在RSS Feed中顯示ListView中的鏈接和描述?
這是我RssAdapter.java
package com.example.jaison.newsclient;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.List;
public class RssAdapter extends BaseAdapter {
private final List<RssItem> items;
private final Context context;
public RssAdapter(Context context, List<RssItem> items) {
this.items = items;
this.context = context;
}
@Override
public int getCount() {
return items.size();
}
@Override
public Object getItem(int position) {
return items.get(position);
}
@Override
public long getItemId(int id) {
return id;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = View.inflate(context, R.layout.rss_item, null);
holder = new ViewHolder();
holder.itemTitle = (TextView) convertView.findViewById(R.id.itemTitle);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.itemTitle.setText(items.get(position).getTitle());
//need to show getdescription also
return convertView;
}
static class ViewHolder {
TextView itemTitle;
}
}
這是我rss_item.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/itemTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp"
android:textSize="18sp" />
Rssitem.java
package com.example.jaison.newsclient;
public class RssItem {
private final String title;
private final String link;
private final String description;
public RssItem(String title, String link,String description) {
this.title = title;
this.link = link;
this.description=description;
}
public String getTitle() {
return title;
}
public String getDescription() {
return description;
}
public String getLink() {
return link;
}
}
這裏另一個代碼
package com.example.jaison.newsclient;
import android.util.Xml;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
public class PcWorldRssParser {
private static final String TAG_TITLE = "title";
private static final String TAG_DESCRIPTION= "description";
private static final String TAG_LINK = "link";
private static final String TAG_RSS = "rss";
// We don't use namespaces
private final String ns = null;
public List<RssItem> parse(InputStream inputStream) throws XmlPullParserException, IOException {
try {
XmlPullParser parser = Xml.newPullParser();
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
parser.setInput(inputStream, null);
parser.nextTag();
return readFeed(parser);
} finally {
inputStream.close();
}
}
private List<RssItem> readFeed(XmlPullParser parser) throws XmlPullParserException, IOException {
parser.require(XmlPullParser.START_TAG, null, TAG_RSS);
String title = null;
String link = null;
String description=null;
List<RssItem> items = new ArrayList<RssItem>();
while (parser.next() != XmlPullParser.END_DOCUMENT) {
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
String name = parser.getName();
if (name.equals(TAG_TITLE)) {
title = readTitle(parser);
} else if (name.equals(TAG_LINK)) {
link = readLink(parser);
}
else if (name.equals(TAG_DESCRIPTION)) {
description = readDescription(parser);
}
if (title != null && link != null && description != null) {
RssItem item = new RssItem(title, link,description);
items.add(item);
title = null;
link = null;
}
}
return items;
}
private String readLink(XmlPullParser parser) throws XmlPullParserException, IOException {
parser.require(XmlPullParser.START_TAG, ns, TAG_LINK);
String link = readText(parser);
parser.require(XmlPullParser.END_TAG, ns, TAG_LINK);
return link;
}
private String readTitle(XmlPullParser parser) throws XmlPullParserException, IOException {
parser.require(XmlPullParser.START_TAG, ns, TAG_TITLE);
String title = readText(parser);
parser.require(XmlPullParser.END_TAG, ns, TAG_TITLE);
return title;
}
private String readDescription(XmlPullParser parser) throws XmlPullParserException, IOException {
parser.require(XmlPullParser.START_TAG, ns, TAG_DESCRIPTION);
String description = readText(parser);
parser.require(XmlPullParser.END_TAG, ns, TAG_DESCRIPTION);
return description;
}
// For the tags title and link, extract their text values.
private String readText(XmlPullParser parser) throws IOException, XmlPullParserException {
String result = "";
if (parser.next() == XmlPullParser.TEXT) {
result = parser.getText();
parser.nextTag();
}
return result;
}
}
並按照如何設置標題 –
包含rss_item的相同方式將描述設置爲該問題。你可以進行修改並將其寫爲答案嗎?@KingofMasses –
查看我的更新回答 –