AsyncTask, CustomBaseAdapter and Listview

2019-09-03 14:45发布

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;

    }

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.

1条回答
放我归山
2楼-- · 2019-09-03 15:21

Ok, i got rid of the exception by doing:

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

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

It should be:

naam: waarde
naam: waarde

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

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


}
查看更多
登录 后发表回答