Bitmaps added to ArrayList in the wrong order

I have an article in my database that has an image, title and price. I add a title and price to each of my arrays and they are in the correct order, so the price matches the title. But when I load the image, I decode it like this:

    private Bitmap decodeFile(byte[] b) {
        // Decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeByteArray(b, 0, b.length, o);

        // The new size we want to scale to
        final int REQUIRED_SIZE=200;

        // Find the correct scale value. It should be the power of 2.
        int scale = 1;
        while(o.outWidth / scale / 2 >= REQUIRED_SIZE && 
              o.outHeight / scale / 2 >= REQUIRED_SIZE) {
            scale *= 2;
        }

        // Decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        return BitmapFactory.decodeByteArray(b, 0, b.length, o2);
    }

      

After decoding is complete, I add it to the ArrayList image like this:

imageList.add(bmp);

      

But then the price and title of the image could be, for example, at index 0 in the price and header of ArrayLists, but the image may be slower to decode than another image, so how can I get the image to be added to index 0? Here is my general upload and add code:

    private void loadData(){
        ParseQuery<ParseObject> query = ParseQuery.getQuery("Items");
        query.setLimit(20);
        query.findInBackground(new FindCallback<ParseObject>() {
            public void done(List<ParseObject> itemList, ParseException e) {
                if (e == null) {
                    Log.d("Parse", "Retrieved " + itemList.size() + " items");
                    itemsLoaded = itemList.size();
                    for (int i = 0; i < itemList.size(); i++){
                        pricesList.add(itemList.get(i).getInt("price"));
                        titlesList.add(itemList.get(i).getString("title"));
                        Log.d("Parse", "objectID = " + itemList.get(i).getObjectId());
                    }                       
                } else {
                    Log.d("Parse", "Error: " + e.getMessage());
                }
                for (int j = 0; j < titles.size(); j++){
                    ParseQuery<ParseObject> query2 = ParseQuery.getQuery("Images");
                    query2.whereEqualTo("imageId", itemList.get(j).getObjectId());
                    query2.findInBackground(new FindCallback<ParseObject>() {
                        public void done(List<ParseObject> picList, ParseException e) {
                            if (picList.size() != 0){
                            ParseFile pFile = (ParseFile) picList.get(0).get("image");

                            pFile.getDataInBackground(new GetDataCallback() {
                                public void done(byte[] data, ParseException e) {
                                    if (e == null) {
                                        Log.d("Parse", "We've got data in data.");
                                        Bitmap bmp = decodeFile(data);
                                        imageList.add(bmp);                                         
                                        if (imageList.size() == titles.size())
                                            updateGridView();   
                                    } else {
                                        Log.d("test", "There was a problem downloading the data.");
                                    }
                                }                                   
                            });
                            }
                            Log.d("Parse", "Downloaded images = " + picList.size());
                        }
                    }); 
                }
            }

        });
    }

      

+3


source to share


3 answers


As I commented: I would use Map

where you can define a key for each value. The key can be a unique product number. If you use this strategy for all individual attributes, you are not dependent on synchronization issues in different boot procedures.



The techniques described in other answers are very interesting when working for optimal performance in regards to memory usage. A long load may take longer to actually display the image, but it will save you working memory as you won't have all the images in memory.

+1


source


Hi You can use Lazy list to load images correctly. refer http://androidtutorialbd.blogspot.in/2013/03/lazy-list-view.html



0


source


I think it would be better if you had a class Article

with an image (or perhaps a better image reference, so you only decode them when you need it), a title, and a price variable. When you load the data you create Article

... This way you are sure that the article has the correct image, title and price ...

Now you are dependent on the order of 3 lists, so mistakes are inevitable!

0


source







All Articles