根据stackoverflow搜索,这个问题确切地描述了我现在需要理解的内容,但没有答案或评论,它只查看了6次:
XMLPullParser cuts the last element of Bitmap
我可以将我的XML文档结构添加到这个问题:
<name id="92">
<display-name>Real name</display-name>
<icon src="https://image.flaticon.com/icons/svg/25/25471.svg" />
</name>
<name id="107">
<display-name>Real name 2</display-name>
<icon src="https://image.flaticon.com/icons/svg/17/17004.svg" />
</name>
分析器:
XmlPullParser parser = getResources().getXml(R.xml.myxml);
try {
while (parser.getEventType() != XmlPullParser.END_DOCUMENT) {
switch (parser.getEventType()){
case XmlPullParser.START_TAG:
tagname = parser.getName();
if (parser.getName().equals(iconsrc)){
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();
}
在列表视图中,我有从listview的第一个索引而不是0索引开始的图像。所以在最后一个listview元素中我有n-1个图像。
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;
}
SomeItem类:
public class SomeItem{
private String name;
private Bitmap iconsrc;
private String nameid;
public SomeItem(String name, Bitmap iconsrc, String nameid){
this.name=name;
this.iconsrc=iconsrc;
this.nameid=nameid;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String getnameid() {
return this.nameid;
}
public void setnameid(String nameid) {
this.nameid = nameid;
}
public Bitmap getIconResource() {
return this.iconsrc;
}
public void setIconResource(Bitmap iconsrc) {
this.iconsrc = iconsrc;
}
}
问题是当您正在阅读下一个项目时,AsysForBitmap没有完成工作,而myBitmap将与下一个图像重叠。
尝试这样做
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.getIconResource = new AsyncForBitmap().execute(iconsrcVALUE).get();
}
if (parser.getName().equals(displayname)) {
displaynameValue = parser.nextText();
}
break;