gpt4 book ai didi

android - 调用 ArrayAdapter 导致无限循环?

转载 作者:太空狗 更新时间:2023-10-29 14:31:45 24 4
gpt4 key购买 nike

在我的应用程序中,我可以选择将项目保存到收藏夹。我在 ArrayList 中保存了两个 ID。当用户调用收藏夹时,它会循环遍历数组列表,对于每个项目,我从数据库中获取相应的数据。我得到的信息再次存储在数组列表中。我想在 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;
}
}

最佳答案

看这一行:

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

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

此外,我不完全确定您为什么调用 adapter.notifyDataSetChanged() 两次。

关于android - 调用 ArrayAdapter 导致无限循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5963537/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com