-->

XMLPullParser cuts the last element of Bitmap [dup

2019-08-20 03:10发布

问题:

This question is an exact duplicate of:

  • Issue with bitmap items in XMLPullParser 1 answer

I am parsing XML file and getting error with information in listview.

There is the following working code of XML parser:

    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:

  1. The desired text, no image;

  2. The desired text and image, which must be in the previous element;

...

N. The desired text and image of the previous element.

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.

How I am filling listview:

SomeAdapter adapter = new SomeAdapter (this, R.layout.list_item, items);
listView.setAdapter(adapter);

Async for Bitmap:

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

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

So the main question is: what is wrong in my code and how should I fix It?

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".

回答1:

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.

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;