调用ArrayAdapter会导致无限循环?

时间:2011-05-11 11:33:48

标签: android arraylist infinite-loop android-arrayadapter

在我的应用中,我可以选择将项目保存到收藏夹。我在ArrayList中保存了两个ID。当用户调用收藏夹时,它会循环遍历arraylist,对于每个项目,我从我的数据库中获取相应的数据。我收到的信息再次存储在一个arraylist中。我想在Listview中显示此列表。

但最后一步似乎不起作用,因为如果我调用ListViewAdapter,我就处于无限循环中。我不太明白为什么。这将是一个愚蠢的事情,但我找不到什么是错的。

在这里,我用我的ID循环我的第一个集会者:

public void getJobs(){
    for(int i = 0; i<vaca.size(); i++){
        getVacatures(vaca.get(i), kantoor.get(i));
        arrVacature.add(vacature);
    }
}

这里我从数据库中调用我的数据:

 private void getVacatures(String vacaid, String kantoorid){
        try {
            Log.e("in try", "try");
            /* Create a URL we want to load some xml-data from. */

            URL url = new URL("http://172.21.150.140:80/scripts/cgiip.exe/WService=brAccentBe/Android/getFavorieten.p?vacaid="+vacaid+"&kantoorid=" + kantoorid);

            System.out.println("URL: "+ url);
            //URL url = new URL("http://dl.dropbox.com/u/22409181/offices.xml");
            /* Get a SAXParser from the SAXPArserFactory. */
            SAXParserFactory spf = SAXParserFactory.newInstance();
            SAXParser sp = spf.newSAXParser();

            /* Get the XMLReader of the SAXParser we created. */
            XMLReader xr = sp.getXMLReader();
            /* Create a new ContentHandler and apply it to the XML-Reader*/ 
            favorietenWebService vs = new favorietenWebService();
            xr.setContentHandler(vs);

            /* Parse the xml-data from our URL. */
            xr.parse(new InputSource(url.openStream()));
            /* Parsing has finished. */

            /* Our ExampleHandler now provides the parsed data to us. */
            vacature = vs.getVacatures();

        } catch (Exception e) {
        }
        runOnUiThread(returnRes);
}

这里我调用了我的适配器,并在列表循环时关闭对话框。这是它出错的地方。

private Runnable returnRes = new Runnable(){
    public void run(){
        if (arrVacature.size() == 0){
            dialog.dismiss();
        }

        //for each time I loop this in debugger, it adds items to my ArrayList...WHY?
        if(arrVacature!=null && arrVacature.size() > 0){
            adapter.notifyDataSetChanged();
            for(int i= 0; i< arrVacature.size();i++){
                adapter.add(arrVacature.get(i));
            }
            dialog.dismiss();

            TextView atlVacatures = (TextView)findViewById(R.id.atlVacatures);
            TextView atlVacaturesnr = (TextView)findViewById(R.id.atlVacaturesnummer);
            atlVacaturesnr.setText("" + arrVacature.size());
            atlVacatures.setText(" jobs op maat gevonden!");

            adapter.notifyDataSetChanged();
        }
    }
};

这是我的适配器类(注意,getArray只返回我的arrVacature数组。):

private class VacatureFavoAdapter extends ArrayAdapter<Vacature>{

    private ArrayList<Vacature> vacatures;


    public VacatureFavoAdapter(Context context, int textViewResourceId, ArrayList<Vacature> vacatures){
        super(context, textViewResourceId, vacatures);
        this.vacatures = getArray();
    }

    @Override
    public View getView(int position, View convertview, ViewGroup parent){

        View view = convertview;
        if(view==null){
            LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = vi.inflate(R.layout.vacature_list_item, null);
            //view.setBackgroundColor((position % 2) == 1? Color.LTGRAY: Color.WHITE);
        }

        Vacature vaca = vacatures.get(position);        

        if(vaca != null){               
            TextView tvNaam = (TextView) view.findViewById(R.id.vacatureNaam);
            TextView tvWerkveld = (TextView) view.findViewById(R.id.vacatureWerkveld);
            TextView tvRegio = (TextView) view.findViewById(R.id.vacatureRegio);
        if(tvNaam != null){   

            tvNaam.setText(vaca.getTitel());
            if(tvWerkveld != null){
                tvWerkveld.setText("Werkveld: " + vaca.getWerkveld());
                if(tvRegio!=null){
                    tvRegio.setText("Regio: "+vaca.getRegio());
                }
            }
        }
        }
        return view;
    }
}

1 个答案:

答案 0 :(得分:1)

看看这一行:

adapter.add(arrVacature.get(i));

ArrayAdapter的add()方法用于将项目添加到已分配给ArrayAdapter的任何数组。我假设adapterVacatureFavoAdapter的一个实例。因此,当您致电adapter.add()时 您将arrVacature.get(i)添加到支持ArrayAdapter的数组中。你是如何构建adapter的?

另外,我不完全确定你为什么要两次致电adapter.notifyDataSetChanged()