Rss:检索到图像链接,无法显示

时间:2011-11-18 08:47:26

标签: android rss

我有一个RSS阅读器,它从网址读取新闻,网址包含图像,我能够检索图像的网址,但我无法在视图中显示它们。我正在使用以下代码阅读我的图片:

url(myRssFeed.getList().get(position).getImageLink())

我希望图像显示在列表视图中。这是我的列表活动。请查看以下代码:

public class AndroidRssReader extends ListActivity {

private RSSFeed myRssFeed = null;

TextView feedTitle;
TextView feedDescribtion;
TextView feedPubdate;
TextView feedLink;
ImageView feedImageView;

public class MyCustomAdapter extends ArrayAdapter<RSSItem> {

    public MyCustomAdapter(Context context, int textViewResourceId,
            List<RSSItem> list) {
        super(context, textViewResourceId, list);   
    }
    RSSItem item;
    RSSFeed feed;
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        //return super.getView(position, convertView, parent);

        View row = convertView;
        item = new RSSItem();
        feed = new RSSFeed();

        if(row==null){
            LayoutInflater inflater=getLayoutInflater();
            row=inflater.inflate(R.layout.row, parent, false);  
        }

     ImageView imageView = (ImageView)row.findViewById(R.id.listimage);

    /*
         try{ 

        //    String images = item.getImageLink();
              String images = myRssFeed.getThumbnail();
              System.out.println("=== IMAGE VIEW=======: "+ item.getImageLink());
              System.out.println("-------Tumbnail------: "+ feed.getThumbnail());

      //      String images = (myRssFeed.getList().get(position).getImageLink());
              Bitmap bitmap = BitmapFactory.decodeStream(new URL(images).openStream());

              System.out.println("------IAMGE--------: " + myRssFeed.getList().get(position).getImageLink());
              imageView.setImageBitmap(bitmap);
              } catch (Exception e) {
              System.out.println(e);
              e.printStackTrace();
            } */
               String image = myRssFeed.getList().get(position).getImageLink();
               System.out.println("====IMAGE=====: " +myRssFeed.getList().get(position).getImageLink());
               System.out.println("====IMAGE=====: " + item.getImageLink());
               System.out.println("====IMAGE=====: " + feed.getThumbnail());
                            Bitmap lBitmap;
                            try {
                                lBitmap = BitmapFactory.decodeStream(new URL(image).openStream());
                                 imageView.setImageBitmap(lBitmap);
                            } catch (MalformedURLException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }





        TextView listTitle=(TextView)row.findViewById(R.id.listtitle);
        listTitle.setText(myRssFeed.getList().get(position).getTitle());
        System.out.println("TITLE----: "+ myRssFeed.getTitle());
        TextView listPubdate=(TextView)row.findViewById(R.id.listpubdate);
        listPubdate.setText(myRssFeed.getList().get(position).getPubdate());

        return row;
    }
}

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.main);

    feedTitle = (TextView)findViewById(R.id.feedtitle);
    feedDescribtion = (TextView)findViewById(R.id.feeddescribtion);
    feedPubdate = (TextView)findViewById(R.id.feedpubdate);
    feedLink = (TextView)findViewById(R.id.feedlink);
    feedImageView = (ImageView)findViewById(R.id.feedimage);

    feedTitle.setVisibility(View.GONE);
    feedDescribtion.setVisibility(View.GONE);
    feedPubdate.setVisibility(View.GONE);
    feedLink.setVisibility(View.GONE);

    readRss();
}

private void readRss(){

    feedTitle.setText("--- wait ---");
    feedDescribtion.setText("");
    feedPubdate.setText("");
    feedLink.setText("");
//  feedImageView.setImage("");
    setListAdapter(null);

    Toast.makeText(this, "Reading RSS, Please wait.", Toast.LENGTH_LONG).show();

    try {
        URL rssUrl = new URL("http://www.bedfordshire-news.co.uk/News/rss.xml");
        SAXParserFactory mySAXParserFactory = SAXParserFactory.newInstance();
        SAXParser mySAXParser = mySAXParserFactory.newSAXParser();
        XMLReader myXMLReader = mySAXParser.getXMLReader();
        RSSHandler myRSSHandler = new RSSHandler();
        myXMLReader.setContentHandler(myRSSHandler);
        InputSource myInputSource = new InputSource(rssUrl.openStream());
        myXMLReader.parse(myInputSource);

        myRssFeed = myRSSHandler.getFeed();

    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ParserConfigurationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SAXException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    if (myRssFeed!=null)
    {
        Calendar c = Calendar.getInstance();
        String strCurrentTiime =  "\n(Time of Reading - "
                                + c.get(Calendar.HOUR_OF_DAY) 
                                + " : "
                                + c.get(Calendar.MINUTE) + ")\n";

        feedTitle.setText(myRssFeed.getTitle() + strCurrentTiime);
        feedDescribtion.setText(myRssFeed.getDescription());
        feedPubdate.setText(myRssFeed.getPubdate());
        feedLink.setText(myRssFeed.getLink());

         Bundle lBundle = new Bundle();
            lBundle.putString("url", myRssFeed.getThumbnail());
            System.out.println("---myRSSFeed-- THUMBNAIL--- " + myRssFeed.getThumbnail());
            Message msg = new Message();
            msg.setData(lBundle);
         //   handler.dispatchMessage(msg);


        MyCustomAdapter adapter =
            new MyCustomAdapter(this, R.layout.row, myRssFeed.getList());
        setListAdapter(adapter);

    }

}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    // TODO Auto-generated method stub
    Intent intent = new Intent(this,ShowDetails.class);
    Bundle bundle = new Bundle();
    bundle.putString("keyTitle", myRssFeed.getItem(position).getTitle());
    bundle.putString("keyDescription", myRssFeed.getItem(position).getDescription());
    bundle.putString("keyLink", myRssFeed.getItem(position).getLink());
    bundle.putString("keyPubdate", myRssFeed.getItem(position).getPubdate());
    bundle.putString("keyImageView",myRssFeed.getItem(position).getImageLink());
    intent.putExtras(bundle);
    startActivity(intent);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // TODO Auto-generated method stub
    menu.add(0, 0, 0, "Reload");
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // TODO Auto-generated method stub
    switch(item.getItemId()){
    case (0): readRss();

            break;
    default:
            break;
    }

    return true;
}


}

请让我知道如何设置图像。

1 个答案:

答案 0 :(得分:0)

Either download that image from the following url in AsyncTask and then set it to Imageview.

或者只是通过给定方法在Listview的Textview中使用rss news设置图像。

您可以使用Image Getter方法,并在Listview的文本视图中显示该图像。

Spanned spannedContent = Html.fromHtml(htmlString, new ImageGetter() {

        @Override
        public Drawable getDrawable(String source) {
          // get image from url
        }
    }, null);