AsyncTask、CustomBaseAdapter 和 Listview [英] AsyncTask, CustomBaseAdapter and Listview
问题描述
我有一个用于填充某些字段的 CustomBaseAdapter:
I have a CustomBaseAdapter with which i fill certain fields:
public ArrayList<SearchResults> GetSearchResults(){
ArrayList<SearchResults> results = 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();
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);
}
return results;
}
我想通过 AsyncTask 的 onPostExecute 启动它,但我强制关闭:
I want to fire this up the the onPostExecute of a AsyncTask but i get a force close:
@Override
protected void onPostExecute(String result) {
ListView kp = (ListView)findViewById(R.id.kpn);
ArrayList<SearchResults> searchResults = GetSearchResult();
kp.setAdapter(new MyCustomBaseAdapter(this, searchResults)); <--- think here is the error
progress.dismiss();
}
这个代码可以吗?如果没有应该改变什么.提前致谢.
Is this code possible? If no what should be changed. Thank you in advance.
推荐答案
好的,我通过执行以下操作摆脱了异常:
Ok, i got rid of the exception by doing:
kp.setAdapter(new MyCustomBaseAdapter(AppName.this, searchResults));
但是JSoup的字段还是不正确,首先显示所有的naam字段,然后是waarde字段.
But the fields are still filled incorrectly with JSoup, first all the naam fields are displayed and then the waarde fields.
naam:
naam:
naam:
waarde
waarde
waarde
应该是:
naam: waarde
naam: waarde
有人知道如何组合JSoup的2个for循环以便正确填充字段:
Does somebody know how to combine the 2 for loops of JSoup so that the fields are filled correctly:
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);
}
这里是我要求的 CustomBaseAdapter:
Here my CustomBaseAdapter as requested:
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;
}
}
这篇关于AsyncTask、CustomBaseAdapter 和 Listview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!