组合 2 个 for 循环

发布于 2024-12-12 01:30:16 字数 5568 浏览 0 评论 0原文

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

naam: waarde 
naam: waarde 

但它们在列表视图中显示如下,由 CustomBaseAdapter 填充。

naam: 
naam: 
naam: 
waarde 
waarde 
waarde 

有人可以帮我完成这件事吗?这让我现在很忙,要辞职一段时间了。

也欢迎大家提出其他建议,谢谢。

2 个 for 循环,其中列表“naam”和“waarde”被填充:

 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;

         }
     }

I parse 2 elements, TD tags from a website by doing 2 for loops with JSoup.
I want them to diplay next to each other in a Listview, like this:

naam: waarde 
naam: waarde 

But they are shown like this in the Listview, which gets filled by a CustomBaseAdapter.

naam: 
naam: 
naam: 
waarde 
waarde 
waarde 

Can somebody please help me to get this done? This is keeping me busy for quit some time now.

Any other suggestions are also welcome, thank you.

The 2 for loops, with which the lists, "naam" and "waarde" get filled:

 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); 
      } 

I know it should be something like this, but how?:

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

}

Here is my 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; 
} 


} 

Edit:
I have now someting like this but get exception 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:
Exception is gone but it only shows the of results1 i.a 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;

         }
     }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

山田美奈子 2024-12-19 01:30:16

克里斯所说的话+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,并在对其进行操作之前检查每个循环是否为空,但您最好只使用两个单独的循环。

+1 to what Chris said. Also, if you're trying to combine those two loops, you can't do it with that syntax. Just use the simple counting method, e.g.

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

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

becomes

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

Of course, this assumes the two lists are the same length, otherwise one of the lists won't be fully consumed. If they vary, you COULD use OR instead of AND in the loop and check each one for null before acting on it, but you're probably better off just using two separate loops.

浅沫记忆 2024-12-19 01:30:16

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

The problem is that you are creating a new SearchResults object each time you go through each loop. It's fine to create a SearchResults object during each iteration of the first loop, but you need to use the SearchResults object created in the first loop for the second loop, don't create a new one.

你的心境我的脸 2024-12-19 01:30:16

成功了!,在听取了 kcoppock 的建议后退了一会儿,喝了杯啤酒和一支香烟后,解决方案变得更容易了,正如我所想的:

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;

         }
     }

Got it working!, after taking advise from kcoppock to step back a little while, having a beer and a sigaret the solution was easier as i thought:

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;

         }
     }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文