我通过使用 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;
}
}