使用ArrayList使用ParseFile下载多个图像

时间:2018-06-09 11:57:12

标签: java android arraylist imageview parse-server

我无法将图像位图存储在全局arraylist中。我宣布了一个全球性的arraylist final ArrayList prod_image = new ArrayList(); 我试图从Parse Server调用图像并将其保存在数组列表中。 在ParseFile回调中,我试图保存位图,  prod_image.add(BP); 但是,数组在回调之外返回一个空值。

如何在回调中将图像存储在数组中?

ParseFile file = (ParseFile) object.get("image_book");

                            file.getDataInBackground(new GetDataCallback() {
                                @Override
                                public void done(byte[] data, ParseException e) {
                                    if(e== null && data != null){

                                       Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);

                                       if(bitmap != null){
                                           Log.i("image download","Success");

                                           bp = bitmap;
                                           Log.i("image Content",bp.toString());

                                           prod_image.add(bp);
                                           //adapter.notifyDataSetChanged();
                                           if(prod_image.size()>0){
                                               Log.i("inside count","Ok");
                                           }else {
                                               Log.i("inside Count","Not Ok");
                                           }


                                           //adapter.notifyDataSetChanged();

                                       }else {
                                           Log.i("image download","Failure");
                                       }
                                    }else {
                                        e.printStackTrace();
                                    }
                                }
                            });


`
public class UserFeed extends AppCompatActivity {

    ImageView account;
    ImageView notif;
    RecyclerView recyclerView;
    RecyclerView.Adapter adapter;
    //UserFeedAdapter adapter;
    ArrayList<String> prod_name ;
    ArrayList<String> prod_cat;
    ArrayList<String> prod_pub;
    ArrayList<String> prod_price;

    Bitmap bp=null;
    RecyclerView.LayoutManager layoutManager;
    final ArrayList<Bitmap> prod_image =  new ArrayList<Bitmap>();
    //final Bitmap bitmap;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_feed);
        //setTitle("My Feed");

        account = (ImageView)findViewById(R.id.account);
        notif = (ImageView)findViewById(R.id.notif);

        account.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getApplicationContext(), AccountActivity.class);
                startActivity(intent);
            }
        });

        notif.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getApplicationContext(), NotificationActivity.class);
                startActivity(intent);
            }
        });

        //myListView = (ListView)findViewById(R.id.listView);
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        recyclerView.setHasFixedSize(true);

        layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);


        prod_name = new ArrayList<>();
        prod_cat = new ArrayList<>();
        prod_pub = new ArrayList<>();
        prod_price = new ArrayList<>();


        //MyData myImage = new MyData();

        ParseQuery<ParseObject> dashboardQuery = new ParseQuery<ParseObject>("Sell");
        dashboardQuery.findInBackground(new FindCallback<ParseObject>() {
            @Override
            public void done(List<ParseObject> objects, ParseException e) {
                if(e == null){
                    if(objects.size() > 0){
                        for (final ParseObject object : objects){
                            String name = (String) object.get("name");
                            String cat = (String) object.get("category");
                            String pub = (String)object.get("publisher");
                            String price = (String) object.get("price");
                            //Bitmap image = (Bitmap)object.get("image_book");

                            prod_name.add(name);
                            prod_cat.add(cat);
                            prod_pub.add(pub);
                            prod_price.add(price);


                            //prod_image.add(image);

                            ParseFile file = (ParseFile) object.get("image_book");

                            file.getDataInBackground(new GetDataCallback() {
                                @Override
                                public void done(byte[] data, ParseException e) {
                                    if(e== null && data != null){

                                       Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);

                                       if(bitmap != null){
                                           Log.i("image download","Success");

                                           bp = bitmap;
                                           Log.i("image Content",bp.toString());

                                           prod_image.add(bp);
                                           //adapter.notifyDataSetChanged();
                                           if(prod_image.size()>0){
                                               Log.i("inside count","Ok");
                                           }else {
                                               Log.i("inside Count","Not Ok");
                                           }


                                           //adapter.notifyDataSetChanged();

                                       }else {
                                           Log.i("image download","Failure");
                                       }
                                    }else {
                                        e.printStackTrace();
                                    }
                                }
                            });

                            //prod_image.add(bp);
                            //<-- not ok
                          //adapter.notifyDataSetChanged();

                        }

                    }
                }
                if(prod_image.size()>0){
                    Log.i("outside count","Ok");
                }else {
                    Log.i("outside Count","Not Ok");
                }
                //<-- It's first crashing then second time its loading successfully!!
                adapter = new UserFeedAdapter(prod_name,prod_cat,prod_pub,prod_price,prod_image);
                //recyclerView.setAdapter(adapter);
                adapter.notifyDataSetChanged();
                recyclerView.setAdapter(adapter);
            }
        });


        //<---Not working at all
        //adapter = new UserFeedAdapter(prod_name,prod_cat,prod_pub,prod_price,prod_image);
       //recyclerView.setAdapter(adapter);
        if(prod_image.size()>0){
            Log.i("very outside count","Ok");
        }else {
            Log.i("very outside Count","Not Ok");
        }


    }




    }`

1 个答案:

答案 0 :(得分:0)

如果将数组声明为全局数组,则在将位图添加到位图数组中之后,必须更新适配器,它应该可以工作。但是要小心,因为这样做需要花费时间,并且应该在ASYNC中完成,尤其是在图像又大又沉的情况下。

public void done(byte[] data, ParseException e) {
if(e== null && data != null){
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
if(bitmap != null){
Log.i("image download","Success");
bp = bitmap;
Log.i("image Content",bp.toString());
prod_image.add(bp);  
//----------------- UPDATE YOUR ARRAY-ADAPTER HERE-----------------
yourView.setAdapter(prod_image);
enter code here