我回到活动时没有加载数据

时间:2018-05-29 09:26:03

标签: android firebase android-layout firebase-realtime-database android-recyclerview

public class AllUsersActivity extends AppCompatActivity {

private Toolbar mToolbar;
private RecyclerView mUserRecycler;

private DatabaseReference mAllUsersDatabase;
private DatabaseReference mUserRef;
private FirebaseAuth mAuth;
private FirebaseUser mCurrentUser;
private ArrayList<Users> users_list = new ArrayList<>();
private Users users;
private AllUsersAdapter mAllUsersAdapter;
private LinearLayoutManager linearLayoutManager;
String TAG = " ALL USERS ";


@Override

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_all_users);

    Log.i("ON CREATE METHOD", "ALL USERS");

    mToolbar = (Toolbar) findViewById(R.id.users_app_bar);
    setSupportActionBar(mToolbar);
    getSupportActionBar().setTitle("All Users");
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    mAuth = FirebaseAuth.getInstance();
    mCurrentUser = mAuth.getCurrentUser();
    mUserRef = FirebaseDatabase.getInstance().getReference().child("Users").child(mAuth.getCurrentUser().getUid());

    mAllUsersDatabase = FirebaseDatabase.getInstance().getReference().child("Users");
    mAllUsersDatabase.keepSynced(true);


    linearLayoutManager = new LinearLayoutManager(this);
    linearLayoutManager.setStackFromEnd(true);


    mAllUsersAdapter = new AllUsersAdapter(users_list);
    mUserRecycler = (RecyclerView) findViewById(R.id.allusers_recycler_view);
    mUserRecycler.setHasFixedSize(true);
    mUserRecycler.setLayoutManager(linearLayoutManager);
    mUserRecycler.setAdapter(mAllUsersAdapter);


}

@Override
protected void onStart() {
    super.onStart();
    Log.i("ON START METHOD", "ALL USERS");


}


@Override
protected void onPostResume() {
    super.onPostResume();
    Log.i("ON RESUME METHOD", "ALL USERS");
    Get_USER();
    mUserRecycler.setAdapter(mAllUsersAdapter);

}

public void Get_USER() {

    ValueEventListener eventListener = new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            for (DataSnapshot ds : dataSnapshot.getChildren()) {
                String name = ds.child("name").getValue(String.class);
                String thumb_image = ds.child("thumb_image").getValue(String.class);
                //Log.i("TAG", name);
                // Log.i("TAG", thumb_image);
                users = new Users(name, thumbimage);
                users_list.add(users);
            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {
        }
    };
    mAllUsersDatabase.addListenerForSingleValueEvent(eventListener);
}

}

我能够从firebase获得结果但是当我第一次打开活动时,我在屏幕上按预期获取数据,但是当我按下&#34;向上导航按钮或后退按钮时#34 ;然后回到活动,然后我可以看到日志中的数据,屏幕上没有任何内容。此外,当我按下主页按钮并再次返回时,则会加载数据。 我的代码在OnResume方法中,但仍未按预期工作。我正在检索在Firebase上注册的所有用户的列表

1 个答案:

答案 0 :(得分:1)

addListenerForSingleValueEvent用于读取数据一次,如果要在数据发生变化时读取数据,请使用addValueEventListener(ValueEventListener)

 // remove **Get_USER** method from onPostResume and add **Get_USER** method at the end of **onCreate**
// and also remove this **mUserRecycler.setAdapter(mAllUsersAdapter)** in onPostResume method 
        public void Get_USER() {

            ValueEventListener eventListener = new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                    for (DataSnapshot ds : dataSnapshot.getChildren()) {
                        String name = ds.child("name").getValue(String.class);
                        String thumb_image = ds.child("thumb_image").getValue(String.class);
                        //Log.i("TAG", name);
                        // Log.i("TAG", thumb_image);
                        users = new Users(name, thumbimage);
                        users_list.add(users);
                        mAllUsersAdapter.notifyItemInserted(users_list.size() - 1);
                    }
                }

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

           mAllUsersDatabase.addValueEventListener(eventListener);
        }