问题描述
我正在解析XML文件,并且列表视图中的信息出现错误.
I am parsing XML file and getting error with information in listview.
有以下XML解析器的工作代码:
private Bitmap myBitmap;
private List<SomeItem> items = new ArrayList();
XmlPullParser parser = getResources().getXml(R.xml.myxml);
try {
while (parser.getEventType() != XmlPullParser.END_DOCUMENT) {
switch (parser.getEventType()){
case XmlPullParser.START_DOCUMENT:
Log.d(TAG, "Начало документа");
break;
case XmlPullParser.START_TAG:
tagname = parser.getName();
if (parser.getName().equals(iconsrc)){ //parse icon
iconsrcVALUE = parser.getAttributeValue(null, "src");
myBitmap = new AsyncForBitmap().execute(iconsrcVALUE).get();
}
if (parser.getName().equals(displayname)) {
displaynameValue = parser.nextText();
items.add(new SomeItem(displaynameValue, myBitmap));
}
break;
case XmlPullParser.TEXT :
tagtext = parser.getText();
break;
case XmlPullParser.END_TAG:
parser.getName();
break;
default:
break;
}
parser.next();
}
} catch (Throwable t) {
Toast.makeText(this,
"Error while loading XML: " + t.toString(), Toast.LENGTH_LONG)
.show();
}
在输出中,我得到一个包含必要元素的列表视图,但是此元素并不完全正确:
At the output, I get a listview with the necessary elements, but this elements is not fully correct:
-
所需的文本,没有图像;
The desired text, no image;
所需的文本和图像,必须在上一个元素中;
The desired text and image, which must be in the previous element;
...
N.上一个元素所需的文本和图像.
N. The desired text and image of the previous element.
我认为对案例" START_TAG和END_TAG的处理不当.我试图合并,更改地点,但没有找到解决方案.
I think on the wrong handling of the "case" START_TAG and END_TAG. I tried to combine, change places, but did not find a solution.
我如何填充列表视图:
SomeAdapter adapter = new SomeAdapter (this, R.layout.list_item, items);
listView.setAdapter(adapter);
位图异步:
class AsyncForBitmap extends AsyncTask<String, Void, Bitmap> {
private Exception exception;
protected Bitmap doInBackground(String... urls) {
try {
URL url=new URL(urls[0]);
Bitmap bitmap = BitmapFactory.decodeStream((InputStream)url.getContent());
return bitmap;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(Bitmap bitmap) {
super.onPostExecute(bitmap);
}
}
这里是适配器:
public class SomeAdapter extends ArrayAdapter<SomeItem>{
private LayoutInflater inflater;
private int layout;
private List<SomeItem> items;
public SomeAdapter (Context context, int resource, List<SomeItem> items) {
super(context, resource, programmes);
this.programmes = programmes;
this.layout = resource;
this.inflater = LayoutInflater.from(context);
}
public View getView(int position, View convertView, ViewGroup parent) {
View view=inflater.inflate(this.layout, parent, false);
ImageView icon = view.findViewById(R.id.iconsrc);
TextView nameView = view.findViewById(R.id.name);
SomeItem programme = programmes.get(position);
icon.setImageBitmap(programme.getIconResource());
nameView.setText(programme.getName());
return view;
}
所以主要问题是:我的代码有什么问题,我该如何解决?
我很高兴不仅能获得有效的代码,而且还能解释如果案例"出现问题,为什么要在一个或另一个案例"中执行此操作.
I would be glad to get not just the working code, but explanation of why this or that action should be done in one or in another "case" if problem in the "case".
推荐答案
问题在于,在阅读下一项时AsynsForBitmap
尚未完成工作,并且myBitmap将与下一张图像重叠.
The issue is that AsynsForBitmap
is not finishing the job while you are reading the next item and myBitmap will be overlapped with the next image.
尝试这样做(假设displayname
在iconsrc
之前,并且SomeItem
类具有称为getIcon()
的方法)
Try to do it like this (Assuming displayname
comes before iconsrc
and SomeItem
class has a method called getIcon()
)
case XmlPullParser.START_TAG:
tagname = parser.getName();
if (parser.getName().equals(iconsrc)){
iconsrcVALUE = parser.getAttributeValue(null, "src");
SomeItem item = new SomeItem(displaynameValue, myBitmap);
item.setName(displaynameValue);
item.add(item);
item.getIcon() = new AsyncForBitmap().execute(iconsrcVALUE).get();
}
if (parser.getName().equals(displayname)) {
displaynameValue = parser.nextText();
}
break;
这篇关于XMLPullParser剪切位图的最后一个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!