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;
}
}
+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.