组合 2 个 for 循环
我通过使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
克里斯所说的话+1。另外,如果您尝试组合这两个循环,则无法使用该语法来实现。只需使用简单的计数方法,例如
变为
当然,这假设两个列表的长度相同,否则其中一个列表将不会被完全消耗。如果它们有所不同,您可以在循环中使用 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.
becomes
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.
问题是您每次执行每个循环时都会创建一个新的
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 aSearchResults
object during each iteration of the first loop, but you need to use theSearchResults
object created in the first loop for the second loop, don't create a new one.成功了!,在听取了 kcoppock 的建议后退了一会儿,喝了杯啤酒和一支香烟后,解决方案变得更容易了,正如我所想的:
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: