, поэтому я занят созданием приложения, в котором есть система галерей, которая загружает изображения из Firebase. Я начал индекс моего массива с 0. Пожалуйста, смотрите комментарии для дальнейшего объяснения, почему происходит эта ошибка.
Структура базы данных Firebase:
Провинция> Идентификатор события> Галерея> Номер рисунка> URL рисунка
Код для извлечения данных:
private void loadEventGalleryFromDB(final String eventName){
DatabaseReference userEventsGFound = Constants.eventsRef.child(eventProvSelected);
userEventsGFound.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
eventIDFound = String.valueOf(snapshot.getKey());
if(!eventIDFound.equals("Total Events")){
eventNameFound = String.valueOf(snapshot
.child("Festival Name")
.getValue());
if(eventNameFound.equals(eventName)){
DatabaseReference userEventGalleryFound = Constants.eventsRef
.child(eventProvSelected)
.child(eventIDFound)
.child("Gallery")
.child("2018");
userEventGalleryFound.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
dbSize = (int) dataSnapshot.getChildrenCount();
Toast.makeText(ScreenUserViewEvent.this, "dbSize: " + dbSize, Toast.LENGTH_SHORT).show();
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
eventGalleryItem = String.valueOf(snapshot.getKey());
eventGalleryItemPic = String.valueOf(snapshot.getValue());
if(galleryCount == 0 && tempSize < dbSize){
arrayListEventGalleryPic1.add(eventGalleryItemPic);
arrayListEventGalleryItem.add(eventGalleryItem);
Toast.makeText(ScreenUserViewEvent.this, "tempSize: " + tempSize, Toast.LENGTH_SHORT).show();
tempSize++;
}else if(galleryCount == 1 && tempSize < dbSize){
arrayListEventGalleryPic2.add(eventGalleryItemPic);
arrayListEventGalleryItem.add(eventGalleryItem);
Toast.makeText(ScreenUserViewEvent.this, "tempSize: " + tempSize, Toast.LENGTH_SHORT).show();
tempSize++;
}else if(galleryCount == 2 && tempSize < dbSize){
arrayListEventGalleryPic3.add(eventGalleryItemPic);
arrayListEventGalleryItem.add(eventGalleryItem);
Toast.makeText(ScreenUserViewEvent.this, "tempSize: " + tempSize, Toast.LENGTH_SHORT).show();
tempSize++;
galleryCount = -1;
}
galleryCount++;
}
recyclerViewEventGallery.setVisibility(View.VISIBLE);
txtNoEventGallery.setVisibility(View.GONE);
initRecyclerEventGallery();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {}
});
}
}
}
}else{
txtNoEventGallery.setVisibility(View.VISIBLE);
recyclerViewEventGallery.setVisibility(View.GONE);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {}
});
}
Код в initRecyclerEventGallery ():
private void initRecyclerEventGallery(){
layoutManagerUserEventGallery = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL,false);
recyclerViewEventGallery.setLayoutManager(layoutManagerUserEventGallery);
adapterUserEventGallery = new AdapterRecycleUserEventGallery(arrayListEventGalleryItem,
arrayListEventGalleryPic1,
arrayListEventGalleryPic2,
arrayListEventGalleryPic3,
this);
recyclerViewEventGallery.setAdapter(adapterUserEventGallery);
}
Код в AdapterRecycleUserEventGallery:
public class AdapterRecycleUserEventGallery extends RecyclerView.Adapter<AdapterRecycleUserEventGallery.ViewHolder>{
private ArrayList<String> arrayListEventGalleryItemName;
private ArrayList<String> arrayListEventOptionsPic1;
private ArrayList<String> arrayListEventOptionsPic2;
private ArrayList<String> arrayListEventOptionsPic3;
private Context mContext;
String sEventOpt;
public AdapterRecycleUserEventGallery(ArrayList<String> arrayListEventGalleryItemName,
ArrayList<String> arrayListEventOptionsPic1,
ArrayList<String> arrayListEventOptionsPic2,
ArrayList<String> arrayListEventOptionsPic3,
Context mContext) {
this.arrayListEventGalleryItemName = arrayListEventGalleryItemName;
this.arrayListEventOptionsPic1 = arrayListEventOptionsPic1;
this.arrayListEventOptionsPic2 = arrayListEventOptionsPic2;
this.arrayListEventOptionsPic3 = arrayListEventOptionsPic3;
this.mContext = mContext;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_layout_event_gallery, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
Glide.with(mContext).asBitmap().load(arrayListEventOptionsPic1.get(position)).into(holder.image1);
Glide.with(mContext).asBitmap().load(arrayListEventOptionsPic2.get(position)).into(holder.image2);
Glide.with(mContext).asBitmap().load(arrayListEventOptionsPic3.get(position)).into(holder.image3);
}
@Override
public int getItemCount() {
return arrayListEventGalleryItemName.size();
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
CircleImageView image1, image2, image3;
public ViewHolder(View itemView) {
super(itemView);
image1 = itemView.findViewById(R.id.imageViewUserEventItem1);
image2 = itemView.findViewById(R.id.imageViewUserEventItem2);
image3 = itemView.findViewById(R.id.imageViewUserEventItem3);
image1.setOnClickListener(this);
image2.setOnClickListener(this);
image3.setOnClickListener(this);
}
@Override
public void onClick(View view) {
/*sEventOpt = arrayListEventOptions.get(getLayoutPosition());
Intent intent = new Intent("user-event-opt-selected");
intent.putExtra("userSelectedEventOpt", sEventOpt);
LocalBroadcastManager.getInstance(mContext).sendBroadcast(intent);*/
}
}
}
Код ошибки, который я получаю:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: evolutionaryapps.festivals4u, PID: 10487
java.lang.IndexOutOfBoundsException: Index: 7, Size: 7
at java.util.ArrayList.get(ArrayList.java:437)
at evolutionaryapps.festivals4u.Adapters.AdapterUser.AdapterRecycleUserEventGallery.onBindViewHolder(AdapterRecycleUserEventGallery.java:54)
at evolutionaryapps.festivals4u.Adapters.AdapterUser.AdapterRecycleUserEventGallery.onBindViewHolder(AdapterRecycleUserEventGallery.java:20)
at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6673)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6714)
at android.support.v7.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5647)
at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5913)
at android.support.v7.widget.GapWorker.prefetchPositionWithDeadline(GapWorker.java:285)
at android.support.v7.widget.GapWorker.flushTaskWithDeadline(GapWorker.java:342)
at android.support.v7.widget.GapWorker.flushTasksWithDeadline(GapWorker.java:358)
at android.support.v7.widget.GapWorker.prefetch(GapWorker.java:365)
at android.support.v7.widget.GapWorker.run(GapWorker.java:396)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6938)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)