Can any one tell me how to retrieve image from RSS feed and display it into listview. My everything is working…but unable to fetch the image form url.. I’ve written function for image lode also..but unable to take the url string for use. Even though I am able to display the url.
The code I tried..
public class RssFeedExampleTestImageActivity extends ListActivity
{
private ArrayList<RSSIteam> itemlist = null;
private RSSListAdaptor rssadaptor = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
itemlist = new ArrayList<RSSIteam>();
new RetrieveRSSFeeds().execute();
RSSListAdaptor adapter = new RSSListAdaptor(this, R.layout.main, itemlist);
setListAdapter(adapter);
}
//Function for loading image from web
public static Drawable LoadImageFromWeb(String uri)
{
try
{
InputStream is = (InputStream) new URL(uri).getContent();
Drawable d = Drawable.createFromStream(is, "src name");
return d;
}
catch (Exception e)
{
return null;
}
}
private void retrieveRSSFeed(String urlToRssFeed,ArrayList<RSSIteam> list)
{
try
{
URL url = new URL(urlToRssFeed);
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
XMLReader xmlreader = parser.getXMLReader();
RSSParser theRssHandler = new RSSParser(list);
xmlreader.setContentHandler(theRssHandler);
InputSource is = new InputSource(url.openStream());
xmlreader.parse(is);
}
catch (Exception e)
{
e.printStackTrace();
}
}
private class RetrieveRSSFeeds extends AsyncTask<Void, Void, Void>
{
private ProgressDialog progress = null;
@Override
protected Void doInBackground(Void... params)
{
/*RSSIteam data = null;
Uri custuri = Uri.parse(data.imageurl);
String str = custuri.toString();
LoadImageFromWeb(str);
*/
retrieveRSSFeed("http://some_url",itemlist);
rssadaptor = new RSSListAdaptor(RssFeedExampleTestImageActivity.this, R.layout.customlist, itemlist);
return null;
}
@Override
protected void onCancelled()
{
super.onCancelled();
}
@Override
protected void onPostExecute(Void result)
{
setListAdapter(rssadaptor);
progress.dismiss();
super.onPostExecute(result);
}
@Override
protected void onPreExecute()
{
progress = ProgressDialog.show(RssFeedExampleTestImageActivity.this, null, "Loading RSS Feeds...");
super.onPreExecute();
}
@Override
protected void onProgressUpdate(Void... values)
{
super.onProgressUpdate(values);
}
}
private class RSSListAdaptor extends ArrayAdapter<RSSIteam>
{
private List<RSSIteam> objects = null;
public RSSListAdaptor(Context context, int textViewResourceId, List<RSSIteam> objects)
{
super(context, textViewResourceId, objects);
this.objects = objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View view = convertView;
if(null == view)
{
LayoutInflater vi = (LayoutInflater)RssFeedExampleTestImageActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(R.layout.customlist, null);
}
RSSIteam data = objects.get(position);
if(null != data)
{
Drawable imgLoad = null;
TextView title = (TextView)view.findViewById(R.id.txtViewTitle);
TextView description = (TextView)view.findViewById(R.id.txtViewDescription);
TextView imagetxt = (TextView)view.findViewById(R.id.txtViewImageUrl);
ImageView imageLoge = (ImageView)view.findViewById(R.id.imgViewLogo);
title.setText(data.title);
description.setText(data.description);
imagetxt.setText(data.imageurl);
/*imageLoge.setVisibility(View.VISIBLE);
imageLoge.setBackgroundDrawable(imgLoad);*/
}
return view;
}
}
}
Check this
http://developer.android.com/resources/samples/XmlAdapters/index.html
https://github.com/thest1/LazyList