1つだけを解析するとRSSフィードが正しく実行されますが、ボタンに設定する8つの異なるURLアドレスがあり、ボタンをクリックするたびにRSSFeddを解析したいのですがSAXパーサーを使用しています。私のコードは以下です
BaseFeedParser.java
package com.example.shareslab;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import com.example.shareslab.Message;
import android.sax.Element;
import android.sax.EndElementListener;
import android.sax.EndTextElementListener;
import android.sax.RootElement;
import android.util.Xml;
public class BaseFeedParser {
static String feedUrlString = "http://www.xxxx.com/rss.xml";
// names of the XML tags
static final String RSS = "rss";
static final String CHANNEL = "channel";
static final String ITEM = "item";
static final String PUB_DATE = "pubDate";
static final String DESCRIPTION = "description";
static final String LINK = "link";
static final String TITLE = "title";
private final URL feedUrl;
protected BaseFeedParser(){
try {
this.feedUrl = new URL(feedUrlString);
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
}
protected InputStream getInputStream() {
try {
return feedUrl.openConnection().getInputStream();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public List<Message> parse() {
final Message currentMessage = new Message();
RootElement root = new RootElement(RSS);
final List<Message> messages = new ArrayList<Message>();
Element itemlist = root.getChild(CHANNEL);
Element item = itemlist.getChild(ITEM);
item.setEndElementListener(new EndElementListener(){
@Override
public void end() {
messages.add(currentMessage.copy());
}
});
item.getChild(TITLE).setEndTextElementListener(new EndTextElementListener(){
@Override
public void end(String body) {
currentMessage.setTitle(body);
}
});
item.getChild(LINK).setEndTextElementListener(new EndTextElementListener(){
@Override
public void end(String body) {
currentMessage.setLink(body);
}
});
item.getChild(DESCRIPTION).setEndTextElementListener(new EndTextElementListener(){
@Override
public void end(String body) {
currentMessage.setDescription(body);
}
});
item.getChild(PUB_DATE).setEndTextElementListener(new EndTextElementListener(){
@Override
public void end(String body) {
currentMessage.setDate(body);
}
});
try {
Xml.parse(this.getInputStream(), Xml.Encoding.UTF_8, root.getContentHandler());
} catch (Exception e) {
throw new RuntimeException(e);
}
return messages;
}
}
RSSフィードのボタンが8つとURLアドレスが8つあり、ボタンごとにすべてを解析したい
MessageList.java
package com.example.shareslab;
import java.util.ArrayList;
import java.util.Arrays;
import com.example.shareslab.R;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class MessageList extends ListActivity implements OnClickListener {
Button home,socialmedia,tech,usworld,business,fashion,people,political;
public static String singleDescription;
public static String title,URLToPost,imageURL;
public static ArrayList<String> galleryImages;
private static class EfficientAdapter extends BaseAdapter
{
private LayoutInflater mInflater;
public EfficientAdapter(Context context)
{
mInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
System.out.println("description COUNT : "+SplashActivity.description.size());
return SplashActivity.description.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent)
{
ViewHolder holder;
if (convertView == null)
{
convertView = mInflater.inflate(R.layout.inflate_list_item, null);
holder = new ViewHolder();
holder.title = (TextView) convertView.findViewById(R.id.inflate_title);
holder.des = (TextView) convertView.findViewById(R.id.inflate_description);
holder.im= (ImageView) convertView.findViewById(R.id.inflate_image);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
UrlImageViewHelper.setUrlDrawable(holder.im, SplashActivity.imageURLAmit.get(position),null);
holder.title.setText(SplashActivity.titles.get(position));
holder.des.setText(SplashActivity.description.get(position));
return convertView;
}
public static class ViewHolder {
TextView title,des;
ImageView im;
}
} // close class Efficent adapter
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
home=(Button)findViewById(R.id.buttonhome);
socialmedia=(Button)findViewById(R.id.buttonsocial);
tech=(Button)findViewById(R.id.buttontech);
usworld=(Button)findViewById(R.id.buttonusworld);
business=(Button)findViewById(R.id.buttonbusiness);
fashion=(Button)findViewById(R.id.buttonfashion);
people=(Button)findViewById(R.id.buttonpeople);
political=(Button)findViewById(R.id.buttonpolitical);
home.setOnClickListener(this);
socialmedia.setOnClickListener(this);
tech.setOnClickListener(this);
usworld.setOnClickListener(this);
business.setOnClickListener(this);
fashion.setOnClickListener(this);
people.setOnClickListener(this);
political.setOnClickListener(this);
ListView listView = getListView();
listView.setTextFilterEnabled(true);
this.setListAdapter(new EfficientAdapter(this));
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
galleryImages=new ArrayList<String>();
singleDescription=SplashActivity.description.get(position);
title=SplashActivity.titles.get(position);
URLToPost=SplashActivity.link.get(position);
imageURL=SplashActivity.imageURLAmit.get(position);
System.out.println("ON CLICK URL: "+URLToPost);
galleryImages.addAll(Arrays.asList(SplashActivity.arrays[position]));
startActivity(new Intent(MessageList.this,MessageListDetail.class));
}
});
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
}