XMLPullParser 中的位图项问题

Issue with bitmap items in XMLPullParser(XMLPullParser 中的位图项问题)
本文介绍了XMLPullParser 中的位图项问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

According to stackoverflow searching, this question exactly describes what I need to understand now, but there are no answers or comments and It viewed only 6 times:

XMLPullParser 剪切位图的最后一个元素

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

I can add my structure of XML-document to this question:

<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 索引开始.所以在最后一个 listview 元素中,我有 n-1 张图片.

In the listview I have images which are atarting from 1st index of listview instead of 0 index. So in the last listview element I have n-1 image.

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 将与下一个图像重叠.

The issue is that AsysForBitmap is not finishing the job while you are reading the next item and myBitmap will be overlapped with the next image.

试着这样做

           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;

这篇关于XMLPullParser 中的位图项问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)