无法正确进行循环

时间:2014-08-27 11:03:13

标签: android listview for-loop

我尝试用JSOUP解析HTML页面,我的问题是我需要获得所有标题(只有10个)和图像(也有)。所以我必须把它们放在同一个地方(新闻提要)。所以我试着用这个循环

来做
@Override
protected String doInBackground(String... arg) {


    Document doc;
    try {

        doc = Jsoup.connect("http://example.com/news/").get();

        title = doc.select("h2[class=et_pt_title]");
        picture = doc.select("img");


          for (Element titles : title) {
              FeedItem item = new FeedItem();
              item.setTitle(titles.text());
              Log.w("title", "" + item.getTitle());
              for (Element img : picture) {

                //  feedList.add(item);

                  String iurl;
                  iurl = img.absUrl("src");

               //   FeedItem item = new FeedItem();
                  if (iurl.contains("http://example.com/wp-content/uploads/2014/")) {
                      item.setAttachmentUrl(iurl);
                  }
                  Log.w("imgUrl", "" + item.getAttachmentUrl());

              }

              feedList.add(item);

          }



    } catch (IOException e) {
        e.printStackTrace();
    }

    return null;
}

但标题位于右侧,ListView只包含10行,但每行包含相同的图片。我必须如何更改循环for?我想要每行的图片标题,其中不同的行包含不同的图片和标题。

我的HTML看起来像

<h2 class="et_pt_title">
    <a href="http://example.com/fotoshkola-yuurgu-otkryvaet-svoi-dveri/">&#171;Фотошкола &#187; открывает свои двери</a>
</h2>

<p class="et_pt_blogmeta">
    27.08.2014 &nbsp 
    <a href="http://example.com/category/abiturientam/" title="Просмотреть все записи в рубрике &laquo;Абитуриентам&raquo;" rel="category tag">Абитуриентам</a>, 
    <a href="http://example.com/category/aktual-noe/" title="Просмотреть все записи в рубрике &laquo;Актуальное&raquo;" rel="category tag">Актуальное</a>, 
    <a href="http://example.com/category/tvorchestvo/" title="Просмотреть все записи в рубрике &laquo;Творчество&raquo;" rel="category tag">Творчество</a>, 
    <a href="http://example.com/category/fotoshkola/" title="Просмотреть все записи в рубрике &laquo;Фотошкола&raquo;" rel="category tag">Фотошкола</a>, 
    <a href="http://example.com/category/hochu-v-gu/" title="Просмотреть все записи в рубрике&raquo;" rel="category tag">ba</a>
</p>

<div class="et_pt_thumb2 alignleft">
    <img src="http://example.com/wp-content/uploads/2014/08/3kSAbKRKwHM-282878_120x120.jpg" alt='&#171;Фотошкола &#187; открывает свои двери' width='120px' height='120px' />
    <a href="http://example.com/fotoshkola-otkryvaet-svoi-dveri/"></a>
</div> <!-- end .thumb -->  

1 个答案:

答案 0 :(得分:1)

关于图像,您基本上做的是:

  1. 循环所有标题(title)并在每次迭代中:
  2. 循环所有图片(pictures
  3. 选择符合条件(img
  4. 的图片(if(iUrl.contains...

    如果一个或多个图像满足条件(3.),那么满足条件的最后一个迭代图像将其设置为item#attachmentUrl (属性名称取自setter名称。)

    您可能想要的是h2循环中 title之后选择最近的图片:

    @Override
    protected String doInBackground(String... arg) {
    
    
        Document doc;
        try {
    
            doc = Jsoup.connect("http://example.com/news/").get();
    
            title = doc.select("h2[class=et_pt_title]");
    
            for (Element titles : title) {
                FeedItem item = new FeedItem();
                item.setTitle(titles.text());
    
                Log.w("title", "" + item.getTitle());
    
                // From Jsoup doc:
                // siblingA ~ siblingX: finds sibling X element preceded by sibling A, e.g. h1 ~ p
                // :eq(n): find elements whose sibling index is equal to n; e.g. form input:eq(1)
                Elements imgContainingDiv = doc.select("h2[class=et_pt_title] ~ div[class=et_pt_thumb2]:eq(0)") // THIS MIGHT NEED TO BE :eq(1)!
                Elements picture = imgContainingDiv.select("img")
    
                for (Element img : picture) {
    
                //  feedList.add(item);
    
                  String iurl;
                  iurl = img.absUrl("src");
    
                //   FeedItem item = new FeedItem();
                  if (iurl.contains("http://example.com/wp-content/uploads/2014/")) {
                      item.setAttachmentUrl(iurl);
                  }
                  Log.w("imgUrl", "" + item.getAttachmentUrl());
    
                }
    
                feedList.add(item);
    
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    
        return null;
    }
    
相关问题