AsyncTask、CustomBaseAdapter 和 Listview

发布于 2024-12-11 13:29:21 字数 1456 浏览 0 评论 0原文

我有一个 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();

    } 

这段代码可能吗? 如果没有应该改变什么。 先感谢您。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

朮生 2024-12-18 13:29:21

好的,我通过这样做摆脱了异常:

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


}

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


}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文