Jaison Joseph Jaison Joseph - 1 year ago 92
Android Question

How to show link and description in ListView from RSS feed?

I want to show both title and description which is parsed from an RSS feed website in the list view. now it can show only the title. i already done with the fetching of description. all i need to show the description below the title. how can i do that ?

This is my 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;
}
}


this is my 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;
}

}


and another code here

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;
}
}

Answer Source

Your rss_item should be like this

<?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>

Then in getview it should be like this

  @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;
}

Your view holder should be like this

static class ViewHolder {
    TextView itemTitle;
    TextView itemDescription;
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download