简体   繁体   中英

Getting data out of a inner class

I've been getting really frustrated with an issue. How do I access the data that I get in a inner class. I'm using Parse for the back end of my new application but all parse methods are used anonymously and are inner classes so I can't really access anything that is retrieved. Is there a way around it?

I tried this and it didn't work. Everything needs to be final and finals can not be altered within the inner class.

Here's my code.

 imageView_intro = (ImageView)findViewById(R.id.imageView_intro);
    List<Bitmap> list = new ArrayList<Bitmap>();
   BackEndUtils.downloadIntroImage(list);
    imageView_intro.setImageBitmap(list.get(0));
public static void  downloadIntroImage(final List<Bitmap> list){
    ParseQuery query = new ParseQuery("Intro");
    query.whereEqualTo("name","intro");

    //BEUFindCallBack beuFindCallBack = new BEUFindCallBack("image");
    query.findInBackground(new FindCallback<ParseObject> (){

        @Override
        public void done(List<ParseObject> parseObjects, com.parse.ParseException e) {
            for(ParseObject tempObject: parseObjects){
                ParseFile tempFile = (ParseFile)tempObject.get("image");
                tempFile.getDataInBackground(new GetDataCallback() {
                    @Override
                    public void done(byte[] bytes, com.parse.ParseException e) {
                        list.add(bytesToBitMap(bytes));
                    }
                });
            }
        }
    });
}

public static Bitmap bytesToBitMap(byte[] bytes){
    Bitmap bitmap = BitmapFactory
                            .decodeByteArray(
                                    bytes, 0,
                                    bytes.length);
    return bitmap;
}

At the end, the list still has a size of 0... meaning nothing was actually added to it. How can I access the byte[] variable and pull out of the inner class!?

The weird thing is, if I send the image view and set the bitmapimage there inside the inner class it works. But I want to be able to take the byte array out of the inner anonymous class and save it somewhere so I can work on it later.

If you're using com.parse.ParseFile , then use getData() to wait for the data. Don't use getDataInBackground() .

Same for ParseQuery , use find() .

Your problem is that BackEndUtils.downloadIntroImage(list) returns before the data has been retrieved, so imageView_intro.setImageBitmap(list.get(0)) will see an empty list.

If anyone experiencing this or any other similar problems, this specific case was caused by a background thread not completing on time and therefor not returning values as intented. That's why it worked to use getData instead of getDataInBackground.

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