Android 如何在onDataChange方法之外获取arrayList值,使其不激活startActivity

Android 如何在onDataChange方法之外获取arrayList值,使其不激活startActivity,android,firebase,exception,firebase-realtime-database,Android,Firebase,Exception,Firebase Realtime Database,我试图从list.get(position).getPosition()中获取值,并将其传递给extras.putin(“STORY\u position”,list.get(position).getPosition()),但当我想从方法public void onDataChange(@NonNull DataSnapshot DataSnapshot)访问外部值时,问题就开始了。它向我抛出错误:IndexOutOfBoundsException:Index:0,Size:0。所以基本上,我

我试图从
list.get(position).getPosition()中获取值,并将其传递给
extras.putin(“STORY\u position”,list.get(position).getPosition()),但当我想从方法
public void onDataChange(@NonNull DataSnapshot DataSnapshot)
访问外部值时,问题就开始了。它向我抛出错误:
IndexOutOfBoundsException:Index:0,Size:0
。所以基本上,我需要一个方法,来访问
onDataChange()
方法之外的列表值

                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();
                }
            });
final List=new ArrayList();
holder.cont.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
rootRef.child(“用户”).child(mAuth.getUid()).addValueEventListener(新的ValueEventListener()){
@凌驾
public void onDataChange(@NonNull DataSnapshot DataSnapshot){
对于(DataSnapshot ds:DataSnapshot.getChildren()){
user=ds.getValue(user.class);
列表。添加(用户);
}                             
}
@凌驾
已取消的公共void(@NonNull DatabaseError DatabaseError){}
});
Intent Intent=新的Intent(getApplicationContext(),StoryActivity.class);
Bundle extras=新Bundle();
额外输入(“项目ID”,职位);
putInt(“STORY_POSITION”,list.get(POSITION.getPosition());
putInt(“STORY_ID”,model.getPosition_on_tree());
extras.putInt(“CHAPTER_SIZE”,model.getChapter_SIZE());
意图.临时演员(临时演员);
星触觉(意向);
完成();
}
});

编辑:我之所以需要获取
list.get(position).getPosition()
onDataChange
方法之外,是因为我有另一个方法,在上面调用
onStop
时,我会将新的位置值传递给DB,并在
main活动开始时自动执行,它关闭并打开StoryActivity,因为DB中的值已更改。当我手动重写数据库中的值时,它也会发生在MainActivity内部。这是我的MainActivity代码和StoryActivity的最新版本。

onDataChange
是一个异步调用,将在其他时间点执行。由于您遇到问题,因此不会按顺序执行

按如下方式更新代码:

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);
            }        
            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();                 
       }

       @Override
       public void onCancelled(@NonNull DatabaseError databaseError) { }
  });
}

onDataChange
是一个异步调用,将在其他时间点执行。由于您遇到问题,因此不会按顺序执行

按如下方式更新代码:

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);
            }        
            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();                 
       }

       @Override
       public void onCancelled(@NonNull DatabaseError databaseError) { }
  });
}

这是一个简单的技巧,您可以使用处理程序并在后运行之前检查列表数据

     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) { }
                });
             Handler handler = new Handler();
             int delay = 1000; //milliseconds

             handler.postDelayed(new Runnable(){
                 public void run(){
                     if(!list.isEmpty())//checking if the data is loaded or not
                    {
                          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();
                    }
                     else
                         handler.postDelayed(this, delay);
                 }
             }, delay);
           }
        });
final List=new ArrayList();
holder.cont.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
rootRef.child(“用户”).child(mAuth.getUid()).addValueEventListener(新的ValueEventListener()){
@凌驾
public void onDataChange(@NonNull DataSnapshot DataSnapshot){
对于(DataSnapshot ds:DataSnapshot.getChildren()){
user=ds.getValue(user.class);
列表。添加(用户);
}                             
}
@凌驾
已取消的公共void(@NonNull DatabaseError DatabaseError){}
});
Handler=newhandler();
int delay=1000;//毫秒
handler.postDelayed(新的Runnable(){
公开募捐{
if(!list.isEmpty())//检查是否加载了数据
{
Intent Intent=新的Intent(getApplicationContext(),StoryActivity.class);
Bundle extras=新Bundle();
额外输入(“项目ID”,职位);
附加输入(“故事位置”,
list.get(position.getPosition());
putInt(“STORY_ID”,model.getPosition_on_tree());
extras.putInt(“CHAPTER_SIZE”,model.getChapter_SIZE());
意图.临时演员(临时演员);
星触觉(意向);
完成();
}
其他的
handler.postDelayed(这个,delay);
}
},延误);
}
});

代码仅在从firebase获取数据时执行。

这是一个简单的技巧,您可以使用处理程序并在后运行之前检查数据列表

     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) { }
                });
             Handler handler = new Handler();
             int delay = 1000; //milliseconds

             handler.postDelayed(new Runnable(){
                 public void run(){
                     if(!list.isEmpty())//checking if the data is loaded or not
                    {
                          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();
                    }
                     else
                         handler.postDelayed(this, delay);
                 }
             }, delay);
           }
        });
final List=new ArrayList();
holder.cont.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
rootRef.child(“用户”).child(mAuth.getUid()).addValueEventListener(新的ValueEventListener()){
@凌驾
public void onDataChange(@NonNull DataSnapshot DataSnapshot){
对于(DataSnapshot ds:DataSnapshot.getChildren()){
user=ds.getValue(user.class);
列表。添加(用户);
}                             
}
@凌驾