简体   繁体   中英

Retrieve multiple images from parse with getdatainbackground (android)

I'm a newbie with android and really need some help from who's familiar with Parse API. What I have done was setting up my card container filled with multiple images. However, the real display shows cards in random sequence according to "DownloadBitmap" instead of ascending order as my thought. The suspicious root cause might be the delay time run through "getdatainbackground() ". Is anybody know how could this disordered problem get solved?? I've tried to use "getdata()" instead, but it really kills performance and seems not a good option. My code is simply as below.

ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("food");
    query.orderByAscending("order");
    query.setLimit(10);
    query.findInBackground(new FindCallback<ParseObject>() {

        @Override
        public void done(final List<ParseObject> objects, ParseException e) {
            for (ParseObject obj_food : objects) {
                if (obj_food.getInt("order") == order) {

                    final ParseFile file = obj_food.getParseFile("picture");        

                    //bitmap
                    file.getDataInBackground(new GetDataCallback() {
                        public void done(byte[] data, ParseException e) {
                            if (e == null) {                                    
                                Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
                                DownloadBitmap.add(bitmap);
                            }                               
                        }
                    });
                }
                order++;
            }
        }
    });

Your main qry should return JSON for the linked URLs to Parse.files that are your bitmaps.

You should be using an image lib ( UIL, Picasso, AQuery ...) to manage the aspects ( Network GET, cache GETs ) based on asNeeded bitmap for Layout.VIEW for the card.

When you need a bitMap for a Card's layout/view, you simply let the image library manage it... These libs are designed to drop in to 'getView()' workflows.

example AQry (parms ref the view and a list of Url's for images)

 private static AQuery mAq;
            mAq.id(holder.imgView).width(110).image(mm.getImages().get(0).getUrl().toString(),
                    true, true, 0, R.drawable.default_video, null, 0, mAspectRatio);

example Picasso


        @Override public View getView(int i, View view, ViewGroup viewGroup) {
            //TODO how is this called w val=-1
            if(i < 0) i = 0;
            ViewHolder viewHolder;
            if (view == null) {
                viewHolder = new ViewHolder();
                view = LayoutInflater.from(viewGroup.getContext())
                        .inflate(R.layout.grid_item, viewGroup, false);
                viewHolder.image = (ImageView) view.findViewById(R.id.image);
                viewHolder.text = (TextView) view.findViewById(R.id.text);
                view.setTag(viewHolder);
            }else{
                viewHolder = (ViewHolder) view.getTag();
            }

            Picasso.with(view.getContext())
                    .load(HomeActivity.getUrlbyPosition(i))
                    .into(viewHolder.image);

            if (HomeActivity.getMsgByPosition(i).length() > 39) {
                viewHolder.text.setText(HomeActivity.getMsgByPosition(i).substring(0, 39));
            }else{viewHolder.text.setText(HomeActivity.getMsgByPosition(i));
            }
            return view;
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM