调用ArrayAdapter会导致无限循环?
在我的应用程序中,我可以选择将项目保存到收藏夹。我将两个 ID 保存在 ArrayList 中。当用户调用收藏夹时,它会循环遍历数组列表,对于每个项目,我都会从数据库中获取相应的数据。我得到的信息再次存储在数组列表中。我想在列表视图中显示此列表。
但最后一步似乎不起作用,因为如果我调用 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;
}
}
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.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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
查看这一行:
ArrayAdapter 的 add() 方法用于将项目添加到已分配给该 ArrayAdapter 的数组中。我假设
adapter
是VacatureFavoAdapter
的实例。因此,当您调用adapter.add()
您将
arrVacature.get(i)
添加到支持 ArrayAdapter 的数组中。您是如何构建适配器
的?另外,我不完全确定您为什么要调用
adapter.notifyDataSetChanged()
两次。Looking at this line:
An ArrayAdapter's
add()
method is used to add an item to whichever array has been assigned to back that ArrayAdapter. I assume thatadapter
is an instance ofVacatureFavoAdapter
. Therefore, when you calladapter.add()
you're adding
arrVacature.get(i)
to the array that backs the ArrayAdapter. How have you constructedadapter
?Also, I'm not totally sure why you're calling
adapter.notifyDataSetChanged()
twice.