Я не могу сохранить растровое изображение в глобальном массиве.Я объявил глобального массива окончательных 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");
}
}
}`