-1

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 cuts the last element of Bitmap

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>

Parser:

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

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

Bitmap loading:

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

Adapter:

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

}

1 Answers1

0

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.

Try to do it like this

           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;
Ferran
  • 1,442
  • 1
  • 6
  • 10
  • Yes, you are right, this fixed my problem. Only one question: now everything works fine, but I getting Toast about `java.lang.IndexOutOfBoundsException: 0` - why? – Alex Aectaniy Apr 10 '19 at 10:01