如何将解析后的XML图像源渲染为图像(而不是字符串)

时间:2013-11-17 04:29:44

标签: android gridview xml-parsing

我正在尝试从网页中提取xml数据,并使用这些信息填充带有图像的GridView布局。我已经成功地收集了这个特定网页(theoatmeal.com)上的标题,描述(包括图像链接)和每篇文章的网址。现在的问题是我只收集了字符串...到目前为止这对于链接/地址和标题是可以的...但是为了显示图像,我需要一个图像数组,指向地址的字符串图像还不够。

你可以看到我的Main runnable类SelectionGrid有大部分程序,我还创建了一个名为OatmealItems.Java的单独类,它定义了我将从theoameal.com XML feed中收集的元素,然后我存储了OatmealItems OatmealItems类型列表中的对象。

现在我只是让每篇文章的标题占据网格,但我需要在那里获取图像,任何人都可以帮助我吗?

此处是XML Feed的链接:http://feeds.feedburner.com/oatmealfeed

SelectionGrid.Java

public class SelectionGrid extends Activity implements AdapterView.OnItemClickListener 
{

    TextView selection; 
    private List<OatmealItems> oatmealItems = new ArrayList<OatmealItems>();
    private List<String> Titles = new ArrayList<String>();
    private List<String> IMAGES = new ArrayList<String>();

    static String Address = "http://feeds.feedburner.com/oatmealfeed?format=xml";

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main);

         selection=(TextView)findViewById(R.id.selection);
         GridView myGridView=(GridView) findViewById(R.id.grid);

         HttpClient client = new DefaultHttpClient();
         HttpGet getMethod = new HttpGet(Address);

         try {
            ResponseHandler<String> responseHandler=new BasicResponseHandler();
            String responseBody=client.execute(getMethod, responseHandler); 
            buildOatmealItem(responseBody);
            LoadTitleArray();
            ArrayAdapter<String> myArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, Titles);
            myGridView.setAdapter(myArrayAdapter);
            myGridView.setOnItemClickListener(this);
          }
        catch (Throwable t)
            {

            t.getMessage();
            Toast.makeText(this, "Crap.... "+t.getMessage(), Toast.LENGTH_SHORT).show();            
        }//catch

    }


void buildOatmealItem(String raw) throws Exception 
{
            DocumentBuilder builder=DocumentBuilderFactory.newInstance().newDocumentBuilder();

            Document doc=builder.parse(new InputSource(new StringReader(raw)));

            NodeList titles=doc.getElementsByTagName("title");
            NodeList link=doc.getElementsByTagName("link");
            NodeList description=doc.getElementsByTagName("description");

            for (int i=0;i<titles.getLength();i++) 
            {
                Element Atitle=(Element)titles.item(i);
                Element Alink=(Element)link.item(i);
                Element Adescription=(Element)description.item(i);
                OatmealItems oatmealitem=new OatmealItems(Atitle.getTextContent(), Alink.getTextContent(), Adescription.getTextContent());

                oatmealItems.add(oatmealitem);
            }
        }


    public void onItemClick(AdapterView<?> parent, View v, int position, long id) 
    {
        selection.setText(oatmealItems.get(position).getImageURL());
    }


    public void LoadTitleArray()
    {
        for (int i=0; i<oatmealItems.size(); i++)
        {
            String  title = oatmealItems.get(i).getTitle();
            Titles.add(title);
        }

        }

    }

}

OatmealItems.Java

    public class OatmealItems 
    {

              private String title;
              private String link;
              private String imageURL;

              public String getTitle()      { return title; }
              public String getLink()       { return link; }
              public String getDescription(){ return imageURL; }

              public OatmealItems(String _title,  String _link, String _description) 
              {

                title = _title;
                link = _link;
                imageURL =  _description;
              }

////// this method was supposed to parse the description tag and extract the image src that's embedded
              //public String ExtractImageFromDescriptionTag(String description)
              //{
                  //String y=description.replace("&lt;","<");
                  //String x;
                  //x=y.replace("&gt;",">");
                  //String[] items = x.split("><"); 
                  //String[] theLink = items[1].split("\"");; 
                  //return theLink[1];

              //}


              @Override
              public String toString() 
              {   

                return "title: " + title +"----------\n\n" + "link: "+ link+ "----------\n\n" +"imageURL: " +imageURL ;  
              }

    }

1 个答案:

答案 0 :(得分:0)

尝试在描述字符串中找到 src =“image url”的模式。 要了解它的工作原理:http://www.vogella.com/articles/JavaRegularExpressions/article.html