如何检索图像表格RSS feed和store int List视图?

时间:2012-01-27 11:41:24

标签: android

任何人都可以告诉我如何从RSS提要中检索图像并将其显示到listview中。我的一切正常...但无法获取图像形式的URL ...我已经为图像lode写了函数..但是无法使用url字符串。即使我能够显示网址。 我试过的代码..

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