开发者

Calling ArrayAdapter causes infinite loop?

开发者 https://www.devze.com 2023-03-05 07:00 出处:网络
In my app, I have an option to save an item to favourites. I save two ID\'s in an ArrayList. When a user calls favourites, it loops through the arraylist and for each item, I get the corresponding dat

In my app, I have an option to save an item to favourites. I save two ID's in an ArrayList. When a user calls favourites, it loops through the arraylist and for each item, I get the corresponding data from my database. The information I get back is again stored in an arraylist. I want to display this list in a Listview.

But the last step doesn't seem to work, as I'm in an infinite loop if I call my ListViewAdapter. I don't quite understand why. It will be something stupid, but I can't find what's wrong.

Here I loop through my first arralist with my ID's:

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

Here I call my data from database:

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

Here I do a call to my adapter and will dismiss the dialog when the list is looped. This is where it goes wrong.

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

This is my adapter class (Note, getArray just returns my arrVacature array.):

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.fi开发者_高级运维ndViewById(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;
    }
}


Looking at this line:

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

An ArrayAdapter's add() method is used to add an item to whichever array has been assigned to back that ArrayAdapter. I assume that adapter is an instance of VacatureFavoAdapter. Therefore, when you call adapter.add() you're adding arrVacature.get(i) to the array that backs the ArrayAdapter. How have you constructed adapter?

Also, I'm not totally sure why you're calling adapter.notifyDataSetChanged() twice.

0

精彩评论

暂无评论...
验证码 换一张
取 消