При попытке открыть приложение оно крашится.
Код ошибки
Process: com.buratinoapps.toplistbitcoin, PID: 9638
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.get(ArrayList.java:437)
at com.buratinoapps.toplistbitcoin.MainActivity$1.getItemViewType(MainActivity.java:71)
at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6186)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6118)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6114)
MainActivity.class код
package com.buratinoapps.toplistbitcoin;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.view.LayoutInflaterFactory;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.util.Log;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.Toast;
import com.buratinoapps.toplistbitcoin.Interface.ItemClickListener;
import com.buratinoapps.toplistbitcoin.R;
import com.buratinoapps.toplistbitcoin.model.Item;
import com.buratinoapps.toplistbitcoin.viewHolder.ItemViewHolder;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.firebase.ui.database.FirebaseRecyclerOptions;
import com.github.aakira.expandablelayout.ExpandableLayoutListener;
import com.github.aakira.expandablelayout.ExpandableLayoutListenerAdapter;
import com.github.aakira.expandablelayout.Utils;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;
import com.google.firebase.database.ValueEventListener;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.MatchResult;
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
List<Item> items = new ArrayList();
FirebaseRecyclerAdapter<Item, ItemViewHolder> adapter;
SparseBooleanArray expandState = new SparseBooleanArray();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = (RecyclerView) findViewById(R.id.lst_item);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
retrieveData();
setData();
}
private void setData() {
Query query = FirebaseDatabase.getInstance().getReference().child("Items");
FirebaseRecyclerOptions<Item> options = new FirebaseRecyclerOptions.Builder<Item>()
.setQuery(query, Item.class)
.build();
adapter = new FirebaseRecyclerAdapter<Item, ItemViewHolder>(options) {
@Override
public int getItemViewType(int position) {
if(items.get(position).isExpandable())
return 1;
else
return 0;
}
@Override
protected void onBindViewHolder(@NonNull ItemViewHolder holder, int position, @NonNull Item model) {
switch (holder.getItemViewType()){
case 0:
{
ItemViewHolder viewHolder = (ItemViewHolder)holder;
viewHolder.setIsRecyclable(false);
viewHolder.txt_item_text.setText(model.getText());
viewHolder.setItemClickListener(new ItemClickListener() {
@Override
public void onClick(View view, int position) {
Toast.makeText(MainActivity.this, ""+items.get(position).getText(), Toast.LENGTH_SHORT).show();
}
});
}
break;
case 1:
{
final ItemViewHolder viewHolder = (ItemViewHolder)holder;
viewHolder.setIsRecyclable(false);
viewHolder.txt_item_text.setText(model.getText());
viewHolder.expandableLinearLayout.setInRecyclerView(true);
viewHolder.expandableLinearLayout.setExpanded(expandState.get(position));
viewHolder.expandableLinearLayout.setListener(new ExpandableLayoutListenerAdapter() {
@Override
public void onPreOpen() {
changeRotate(viewHolder.button,0f,180f).start();
expandState.put(position,true);
}
@Override
public void onPreClose() {
changeRotate(viewHolder.button,180f,0f).start();
expandState.put(position,false);
}
});
viewHolder.button.setRotation(expandState.get(position)?180f:0f);
viewHolder.button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
viewHolder.expandableLinearLayout.toggle();
}
});
viewHolder.txt_child_item_text.setText(model.getText());
viewHolder.txt_child_item_text.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,""+viewHolder.txt_child_item_text.getText(), Toast.LENGTH_SHORT).show();
}
});
viewHolder.setItemClickListener(new ItemClickListener() {
@Override
public void onClick(View view, int position) {
Toast.makeText(MainActivity.this,""+model.getText(), Toast.LENGTH_SHORT).show();
}
});
}
break;
default:
break;
}
}
@NonNull
@Override
public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
if(viewType == 0){
View itemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.layout_without_child, viewGroup, false);
return new ItemViewHolder(itemView, viewType == 1);
}
else{
View itemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.layout_with_child, viewGroup, false);
return new ItemViewHolder(itemView, viewType == 1);
}
}
};
expandState.clear();
for(int i=0;i<items.size();i++)
expandState.append(i,false);
recyclerView.setAdapter(adapter);
}
private ObjectAnimator changeRotate(RelativeLayout button, float from, float to) {
ObjectAnimator animator = ObjectAnimator.ofFloat(button, "rotation", from, to);
animator.setDuration(300);
animator.setInterpolator(Utils.createInterpolator(Utils.LINEAR_INTERPOLATOR));
return animator;
}
private void retrieveData() {
items.clear();
DatabaseReference db = FirebaseDatabase.getInstance()
.getReference()
.child("items");
db.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot itemSnapShot:dataSnapshot.getChildren()){
Item item = itemSnapShot.getValue(Item.class);
items.add(item);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.d("ERROR", ""+ databaseError.getMessage());
}
});
}
@Override
protected void onStart() {
if(adapter != null)
adapter.startListening();
super.onStart();
}
@Override
protected void onStop() {
if(adapter != null)
adapter.stopListening();
super.onStop();
}
}
Ошибку показывает на 71 строке
if(items.get(position).isExpandable())