我想在列表视图中显示从RSS提要网站解析的标题和描述。现在它只能显示标题。我已经完成了描述的提取。我只需要在标题下面显示说明。我怎么能这样做?
这是我的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;
}
}
答案 0 :(得分:0)
你的rss_item应该是这样的
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<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:text="title"
android:textSize="18sp" />
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/itemDescription"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp"
android:text="discription"
android:textSize="14sp" />
</LinearLayout>
然后在getview中它应该是这样的
@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);
holder.itemDescription = (TextView) convertView.findViewById(R.id.itemDescription);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.itemTitle.setText(items.get(position).getTitle());
holder.itemDescription.setText(items.get(position).getDescription())
return convertView;
}
您的观看者应该是这样的
static class ViewHolder {
TextView itemTitle;
TextView itemDescription;
}