【问题标题】:Calling ArrayAdapter causes infinite loop?调用 ArrayAdapter 会导致无限循环?
【发布时间】:2011-08-23 05:47:27
【问题描述】:

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

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

在这里,我用我的 ID 遍历我的第一个 arralist:

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

【问题讨论】:

    标签: android arraylist infinite-loop android-arrayadapter


    【解决方案1】:

    看这一行:

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

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

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

    【讨论】:

    • 你的回答让我有点困惑。除了adapter.notifyDataSetChanged(),你有一个观点,我不太明白你想告诉我什么。
    • 好的。你用来声明/构造adapter的代码是什么?
    • 我在我的 onCreate 方法中这样做this.adapter = new VacatureFavoAdapter(this, R.layout.vacature_list_item, arrVacature); setListAdapter(this.adapter);
    • 好的。所以你用来支持adapter 的列表是arrVacature。因此,每当您调用adapter.add() 时,我认为 ArrayAdapter 会将项目添加回arrVacature。 (ArrayAdapter 不会复制列表 - 它只是保留对它的引用。)所以我认为您的 adapter.add(arrVacature.get(i))arrVacature.add(arrVacature.get(i)) 相同。
    • 我想我在关注你,但我仍然不确定如何解决它。我应该创建一个新的 ArrayList 吗?
    猜你喜欢
    • 2021-06-12
    • 2016-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-14
    • 2020-03-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多