I'm trying to get value from list.get(position).getPosition()
and pass it to extras.putInt("STORY_POSITION", list.get(position).getPosition());
, but the problem starts when I want to access value outside from method public void onDataChange(@NonNull DataSnapshot dataSnapshot)
. It throws me error: IndexOutOfBoundsException: Index: 0, Size: 0
. So basically, I need a method, to get access to list values outside of onDataChange()
method.
final List<User> list = new ArrayList<>();
holder.cont.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
rootRef.child("users").child(mAuth.getUid()).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot ds: dataSnapshot.getChildren()){
user = ds.getValue(User.class);
list.add(user);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) { }
});
Intent intent = new Intent(getApplicationContext(), StoryActivity.class);
Bundle extras = new Bundle();
extras.putInt("PROJECT_ID", position);
extras.putInt("STORY_POSITION", list.get(position).getPosition());
extras.putInt("STORY_ID", model.getPosition_on_tree());
extras.putInt("CHAPTER_SIZE", model.getChapter_size());
intent.putExtras(extras);
startActivity(intent);
finish();
}
});
EDIT: The reason I need to get list.get(position).getPosition()
outside of onDataChange
method is that I have another method where calling onStop
I pass new position value to DB, and automatically when the MainActivity
starts, it closes and opens StoryActivity because value has changed in DB. It also happens inside MainActivity when I manually override values in DB.This is the link to my MainActivity code and link to StoryActivity.