如何从RSS源获取图像

时间:2012-02-29 09:35:24

标签: android rss-reader

我开发了一个从url获取RSS值的应用程序。在我的Logcat我得到了图片网址,我需要在我的ListView中显示这些图片,但它无法正常工作。我的代码:

private Drawable ImageOperations(Context context, String url, String saveFilename) {

}


//FeedTabActivity.java

public class FeedTabActivity extends Activity {


ImageView imgView;
private ListView feedListView;
private ArrayList<Item> items;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);      

    setContentView(R.layout.main);         
    feedListView = (ListView)findViewById(R.id.feedlist);


    UpdateFeedTask twitter_task = new UpdateFeedTask(); 
    URL url = null;
    try {

        url = new URL("http://news.yahoo.com/rss/");
    } catch (MalformedURLException e) {     
        e.printStackTrace();
    }
    twitter_task.execute(new URL[] {url});        
}


@Override
protected void onStart() {
    super.onStart();        
}

@Override
protected void onResume() {
    super.onResume();    
}

@Override
protected void onPause() {
    super.onPause();    
}

@Override
protected void onStop() {
    super.onStop();
}

@Override
protected void onDestroy() {
    super.onDestroy();

}

 @Override
public void onConfigurationChanged(Configuration newConfig){        
    super.onConfigurationChanged(newConfig);
}     

private class UpdateFeedTask extends AsyncTask<URL, Void, ArrayList<Item>> {    

    protected void onPreExecute() { }

    protected ArrayList<Item> doInBackground(URL...params) {     

        items = new ArrayList<Item>();          
        FeedHandler feedHandler = new FeedHandler(FeedTabActivity.this);

        for( URL url : params) {

            try {
                Feed handledFeed = feedHandler.handleFeed(url); 

                items = handledFeed.getItems();
                System.out.println(" items " +items);

            } catch (IOException ioe) {} 
              catch (SAXException se) {} 
              catch (ParserConfigurationException pce) {}            
        }
        return items;
    }

    protected void onPostExecute(ArrayList<Item> result) {  

        System.out.println(" return " +result.size());
        FeedArrayAdapter arrayAdapter = new FeedArrayAdapter(FeedTabActivity.this, R.layout.row, result);
        feedListView.setAdapter(arrayAdapter);  

    }
}

private class FeedArrayAdapter extends ArrayAdapter<Item> {

    ArrayList<Item> items;

    public FeedArrayAdapter(Context context, int textViewResourceId, ArrayList<Item> objects) {
        super(context, textViewResourceId, objects);
        this.items = objects;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {


        View row = convertView;    

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

        Item item = items.get(position);

        if(items!=null){

        TextView titleView = (TextView) row.findViewById(R.id.title);
        TextView channelView = (TextView) row.findViewById(R.id.description);

        ImageView imgView = (ImageView)row.findViewById(R.id.icon);

        Drawable img = items.getDrawable();   // Here not getten getDrawable();                              
        imgView.setImageDrawable(img);

        titleView.setText(item.getTitle());         
        channelView.setText(item.getContent());

        }



        return row;
    }


}

private Drawable ImageOperations(Context context, String url, String saveFilename) {
    try {
        InputStream is = (InputStream) this.fetch(url);
        Drawable d = Drawable.createFromStream(is, "src");
        return d;
    } catch (MalformedURLException e) {
        e.printStackTrace();
        return null;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}




public Object fetch(String address) throws MalformedURLException,IOException {
    URL url = new URL(address);
    Object content = url.getContent();
    return content;
}
}

// Item.java

public class Item {

private URL mLink;  
private String mTitle;
private String mContent;
private Drawable mimage;
private List<Enclosure> mEnclosures;

public Item() {
    mEnclosures = new ArrayList<Enclosure>();

}

public Item(URL link, String title, String description, Drawable images,String content, URL image, List<Enclosure> enclosures) {
    super();

    this.mLink = link;

    this.mTitle = title;

    this.mContent = content;
    this.mImage = image;

    this.mEnclosures = enclosures;
}



public void setLink(URL link) {
    this.mLink = link;
}

public URL getLink() {
    return this.mLink;
}


public void setTitle(String title) {
    this.mTitle = title;
}

public String getTitle() {
    return this.mTitle;
}

public void setContent(String content) {
    this.mContent = content;
}

public String getContent() {
    return mContent;
}



public void setDrawable(Drawable d) {
    this.mimage = d;
}
public Drawable getDrawable() {
    return mimage;
}


public void addEnclosure(Enclosure enclosure) {
    this.mEnclosures.add(enclosure);
}

public void setEnclosures(List<Enclosure> enclosures){
    this.mEnclosures = enclosures;
}

public List<Enclosure> getEnclosures() {
    return this.mEnclosures;
}

public void setLink(String trim) {
    // TODO Auto-generated method stub

}
}

Feed.java

public class Feed {

public static final String TYPE_RSS = "rss";

private Drawable mimage;
private URL mURL;
private URL mHomePage;
private String mTitle;
private String mType;
private Date mRefresh = null;
private boolean mEnabled = true;
private ArrayList<Item> mItems;



public Feed() {
    mItems = new ArrayList<Item>();
}

public Feed(URL url, URL homePage, String title, String type, Drawable images, Date refresh, boolean enabled, ArrayList<Item> items) {
    super();
    this.mURL = url;
    this.mHomePage = homePage;
    this.mTitle = title;
    this.mType = type;
    this.mRefresh = refresh;
    this.mEnabled = enabled;
    this.mItems = items;
    this.mimage = images;
}



public void setURL(URL url) {
    this.mURL = url;
}

public URL getURL() {
    return this.mURL;
}

public void setHomePage(URL homepage) {
    this.mHomePage = homepage;
}

public URL getHomePage() {
    return this.mHomePage;
}

public void setTitle(String title) {
    this.mTitle = title;
}

public String getTitle() {
    return this.mTitle;
}

public void setType(String type) {
    this.mType = type;
}

public String getType() {
    return mType;
}

public void setRefresh(Date refresh) {
    mRefresh = refresh;
}

public Date getRefresh() {
    return mRefresh;
}

public void enable() {
    this.mEnabled = true;
}

public void disable() {
    this.mEnabled = false;
}

public void addItem(Item item) {
    this.mItems.add(item);
}

public void setItems(ArrayList<Item> items){
    this.mItems = items;
}

public ArrayList<Item> getItems() {
    return this.mItems;
}

public Drawable getmImages() {
    return mimage;
}

public void setmImages(Drawable mImages) {
    this.mimage = mImages;
}

public String toString() {
    String s = "{URL=" + this.mURL.toString() + " homepage=" + this.mHomePage.toString() + " title=" + this.mTitle + " type=" + this.mType + " update=" + this.mRefresh.toString() + " enabled=" + this.mEnabled;
    s = s + " items={";
    Iterator<Item> iterator = this.mItems.iterator();
    while (iterator.hasNext()) {
        s = s + iterator.next().toString();
    }
    s = s + "}}";
    return s;
}

}

1 个答案:

答案 0 :(得分:0)

这篇文章:

    View row = convertView;    

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

可以大大改善。 ArrayAdapter已经知道项目布局并将完成所有工作。看here