XMLPullParser 中的位图项问题

Issue with bitmap items in XMLPullParser

根据Whosebug搜索,这个问题准确描述了我现在需要理解的内容,但是没有答案和评论,只看了6次:

XMLPullParser cuts the last element of Bitmap

我可以将我的 XML-document 结构添加到这个问题中:

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

在列表视图中,我有从列表视图的第一个索引而不是 0 索引开始的图像。所以在最后一个列表视图元素中,我有 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 class:

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;