0

我通过使用 JSoup 执行 2 个 for 循环来解析来自网站的 2 个元素 TD 标签。我希望它们在 Listview 中彼此相邻显示,如下所示:

naam: waarde 
naam: waarde 

但它们在 Listview 中显示为这样,由 CustomBaseAdapter 填充。

naam: 
naam: 
naam: 
waarde 
waarde 
waarde 

有人可以帮我完成这件事吗?这让我现在忙着戒烟一段时间。

也欢迎任何其他建议,谢谢。

用于填充列表“naam”和“waarde”的 2 个 for 循环:

 Document doc = Jsoup.parse(kpn);  
   Elements tdsFromSecondColumn = doc.select("table.personaltable td:eq(0)"); 
   Elements tdsFromSecondColumn1 = doc.select("table.personaltable td:eq(1)"); 
   SearchResults sr1 = new SearchResults(); 

      for (Element tdFromSecondColumn : tdsFromSecondColumn) {                            
           sr1 = new SearchResults(); 
           sr1.setNaam(tdFromSecondColumn.text()); 
           results.add(sr1); 
      } 

      for (Element tdFromSecondColumn1 : tdsFromSecondColumn1) {                              
           sr1 = new SearchResults(); 
           sr1.setWaarde(tdFromSecondColumn1.text()); 
           results.add(sr1); 
      } 

我知道它应该是这样的,但是如何?:

for (Element tdFromSecondColumn : tdsFromSecondColumn) {                             
     sr1 = new SearchResults();  
     sr1.setNaam(tdFromSecondColumn.text());  
     sr1.setWaarde(tdFromSecondColumn1.text());
     results.add(sr1);  

}

这是我的CustomeBaseAdapter:

public class MyCustomBaseAdapter extends BaseAdapter { 

private static ArrayList<SearchResults> searchArrayList; 

private LayoutInflater mInflater; 


  public MyCustomBaseAdapter(Context context, ArrayList<SearchResults> results) { 
  searchArrayList = results; 
  mInflater = LayoutInflater.from(context); 
  } 


public int getCount() { 
  return searchArrayList.size(); 
} 

public Object getItem(int position) { 
  return searchArrayList.get(position); 
} 

public long getItemId(int position) { 
  return position; 
} 

public View getView(int position, View convertView, ViewGroup parent) { 
    ViewHolder holder; 
  if (convertView == null) { 
    convertView = mInflater.inflate(R.layout.test, null); 
    holder = new ViewHolder(); 
    holder.txtNaam = (TextView) convertView.findViewById(R.id.naam); 
    holder.txtWaarde = (TextView) convertView.findViewById(R.id.waarde); 

    convertView.setTag(holder); 
   } else { 
 holder = (ViewHolder) convertView.getTag(); 
 } 

 holder.txtNaam.setText(searchArrayList.get(position).getNaam()); 
 holder.txtWaarde.setText(searchArrayList.get(position).getWaarde()); 

 return convertView; 
} 

static class ViewHolder { 
  TextView txtNaam; 
  TextView txtWaarde; 
} 


} 

编辑:我现在有这样的东西,但得到异常 indexsize 12:

public ArrayList<SearchResults> GetSearchResults(){

          ArrayList<SearchResults> results = new ArrayList<SearchResults>();
          ArrayList<SearchResults> results1 = new ArrayList<SearchResults>();
          ArrayList<SearchResults> results2 = new ArrayList<SearchResults>();

          Document doc = Jsoup.parse(kpn);  
          Elements tdsFromSecondColumn = doc.select("table.personaltable td:eq(0)");
          Elements tdsFromSecondColumn1 = doc.select("table.personaltable td:eq(1)");

          SearchResults sr1 = new SearchResults();
          SearchResults sr2 = new SearchResults();


          for (Element tdFromSecondColumn : tdsFromSecondColumn) {                             
               sr1 = new SearchResults();  
               sr1.setNaam(tdFromSecondColumn.text());  
               results.add(sr1);  
          }  

          for (Element tdFromSecondColumn1 : tdsFromSecondColumn1) {                               
               sr2 = new SearchResults();  
               sr2.setWaarde(tdFromSecondColumn1.text());  
               results1.add(sr2);  
          }  

          for (int i = 0; i < results.size();i++) { 
              results.add(results.get(i));
              results2.add(results1.get(i)); 
            } 

          return results2;

         }
     }

Edit2:异常消失了,但它只显示了结果 1 ia setWaarde:

public ArrayList<SearchResults> GetSearchResults(){

          ArrayList<SearchResults> results = new ArrayList<SearchResults>();
          ArrayList<SearchResults> results1 = new ArrayList<SearchResults>();
          ArrayList<SearchResults> results2 = new ArrayList<SearchResults>();

          Document doc = Jsoup.parse(kpn);  
          Elements tdsFromSecondColumn = doc.select("table.personaltable td:eq(0)");
          Elements tdsFromSecondColumn1 = doc.select("table.personaltable td:eq(1)");

          SearchResults sr1 = new SearchResults();
          SearchResults sr2 = new SearchResults();


          for (Element tdFromSecondColumn : tdsFromSecondColumn) {                             
               sr1 = new SearchResults();  
               sr1.setNaam(tdFromSecondColumn.text());  
               results.add(sr1);  
          }  

          for (Element tdFromSecondColumn1 : tdsFromSecondColumn1) {                               
               sr1 = new SearchResults();  
               sr1.setWaarde(tdFromSecondColumn1.text());  
               results1.add(sr1);  
          }  

          for (int i = 0; i < results.size() && i < results1.size();i++) { 
              results.add(results.get(i));
              results2.add(results1.get(i)); 
            } 

          return results2;

         }
     }
4

3 回答 3

1

问题是SearchResults每次遍历每个循环时都会创建一个新对象。在第一个循环的每次迭代期间创建一个SearchResults对象是可以的,但是您需要将SearchResults在第一个循环中创建的对象用于第二个循环,不要创建一个新对象。

于 2011-10-25T18:31:14.053 回答
1

+1 克里斯所说的话。此外,如果您尝试组合这两个循环,则无法使用该语法进行操作。只需使用简单的计数方法,例如

for(Object o : oList1) {
    doStuff(o);
}

for (Object o : oList2) {
    doStuff2(o);
}

变成

for (int i = 0; i < oList1.size() && i < oList2.size(); i++) {
    doStuff(oList1.get(i));
    doStuff2(oList2.get(i));
}

当然,这假设两个列表的长度相同,否则其中一个列表将不会被完全消耗。如果它们有所不同,您可以在循环中使用 OR 而不是 AND,并在对其进行操作之前检查每个是否为 null,但您最好只使用两个单独的循环。

于 2011-10-25T19:05:20.387 回答
0

得到它的工作!在听取了 kcoppock 的建议后退了一会儿,喝了啤酒和 sigaret,解决方案比我想的更容易:

public ArrayList<SearchResults> GetSearchResults(){

          ArrayList<SearchResults> results = new ArrayList<SearchResults>();

          Document doc = Jsoup.parse(kp);  
       // Get all td's 
          Elements waardes= doc.select("td"); 
       // Iterator over those elements      
          ListIterator<Element> postIt = waardes.listIterator(); 

          SearchResults sr1 = new SearchResults();

          while (postIt.hasNext()) { 
                sr1 = new SearchResults();
                // Add the value text to the ArrayList      
                sr1.setNaam(postIt.next().text()); 
                sr1.setWaarde(postIt.next().text());  
                results.add(sr1); 
            } 

          return results;

         }
     }
于 2011-10-25T21:33:21.763 回答