AsyncTask,CustomBaseAdapter和Listview

时间:2011-10-24 20:55:19

标签: android arraylist android-asynctask android-listview baseadapter

我有一个CustomBaseAdapter,用它来填充某些字段:

   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,但是我得到了一个强制关闭:

@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();

    } 

这段代码可以吗? 如果没有什么应该改变。 提前谢谢。

1 个答案:

答案 0 :(得分:0)

好的,我通过以下方式摆脱了异常:

kp.setAdapter(new MyCustomBaseAdapter(AppName.this, searchResults));

但是JSoup中的字段仍然填写不正确,首先显示所有naam字段,然后显示waarde字段。

naam:
naam:
naam:
waarde
waarde
waarde

应该是:

naam: waarde
naam: waarde

有人知道如何组合JSoup的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);
          }

这里我的CustomBaseAdapter按要求:

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


}