获得零大小列表android

时间:2017-04-11 04:19:34

标签: android firebase firebase-realtime-database

  

代码

import android.support.v7.app.AppCompatActivity;
import android.util.Log;

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.ValueEventListener;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    List<String> list=new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        testdb();
       Log.e("list size",String.valueOf(list.size()));

    }

    void testdb()
    {
        DatabaseReference mDatabase;
// ...
        mDatabase = FirebaseDatabase.getInstance().getReference();

        mDatabase.child("User").child("hrcj7").child("Url").addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                // Get Post object and use the values to update the UI
                for (DataSnapshot childDataSnapshot : dataSnapshot.getChildren()) {
                    Log.e("Test value",""+ childDataSnapshot.getKey());
                    list.add(childDataSnapshot.getKey());//displays the key for the node

                }
               // Url post = dataSnapshot.getValue(Url.class);
                // ...
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                // Getting Post failed, log a message
                Log.w("Test", "loadPost:onCancelled", databaseError.toException());
                // ...
            }
        });
    }
}
  

logcat的

04-11 09:31:31.546 10007-10007/com.example.rajitha.firebasetest I/DynamiteModule: Selected remote version of com.google.android.gms.firebase_database, version >= 5
04-11 09:31:31.626 10007-10007/com.example.rajitha.firebasetest E/list size: 0
04-11 09:31:31.626 10007-10049/com.example.rajitha.firebasetest V/FA: Activity resumed, time: 112595588
04-11 09:31:31.666 10007-10007/com.example.rajitha.firebasetest D/ViewRootImpl: Buffer Count from app info with  ::-1 && -1 for :: com.example.rajitha.firebasetest from View :: -1 DBQ Enabled ::false false
04-11 09:31:31.666 10007-10099/com.example.rajitha.firebasetest D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: false
04-11 09:31:31.676 10007-10007/com.example.rajitha.firebasetest D/PhoneWindow: *FMB* isFloatingMenuEnabled mFloatingMenuBtn : null
04-11 09:31:31.676 10007-10007/com.example.rajitha.firebasetest D/PhoneWindow: *FMB* isFloatingMenuEnabled return false
04-11 09:31:31.761 10007-10099/com.example.rajitha.firebasetest D/libEGL: eglInitialize EGLDisplay = 0x9b506c54
04-11 09:31:31.761 10007-10099/com.example.rajitha.firebasetest I/OpenGLRenderer: Initialized EGL, version 1.4
04-11 09:31:31.901 10007-10099/com.example.rajitha.firebasetest I/OpenGLRenderer: HWUI protection enabled for context ,  &this =0xaf833970 ,&mEglDisplay = 1 , &mEglConfig = -1348926820 
04-11 09:31:31.906 10007-10099/com.example.rajitha.firebasetest D/OpenGLRenderer: Get maximum texture size. GL_MAX_TEXTURE_SIZE is 8192
04-11 09:31:31.906 10007-10099/com.example.rajitha.firebasetest D/OpenGLRenderer: Enabling debug mode 0
04-11 09:31:31.906 10007-10099/com.example.rajitha.firebasetest D/mali_winsys: new_window_surface returns 0x3000,  [540x960]-format:1
04-11 09:31:32.221 10007-10049/com.example.rajitha.firebasetest D/FA: Connected to remote service
04-11 09:31:32.221 10007-10049/com.example.rajitha.firebasetest V/FA: Processing queued up service tasks: 1
04-11 09:31:32.226 10007-10007/com.example.rajitha.firebasetest I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@c6a2c90 time:69975189
04-11 09:31:35.441 10007-10007/com.example.rajitha.firebasetest E/Test value: test1
04-11 09:31:35.441 10007-10007/com.example.rajitha.firebasetest E/Test value: test2
04-11 09:31:35.456 10007-10007/com.example.rajitha.firebasetest W/ClassMapper: No setter/field for test1 found on class com.example.rajitha.firebasetest.Url
04-11 09:31:35.456 10007-10007/com.example.rajitha.firebasetest W/ClassMapper: No setter/field for test2 found on class com.example.rajitha.firebasetest.Url
04-11 09:31:37.321 10007-10049/com.example.rajitha.firebasetest V/FA: Inactivity, disconnecting from AppMeasurementService
04-11 09:32:38.016 10007-12156/com.example.rajitha.firebasetest V/FA: Activity paused, time: 112661965
04-11 09:32:38.161 10007-10007/com.example.rajitha.firebasetest V/ActivityThread: updateVisibility : ActivityRecord{1f9a7008 token=android.os.BinderProxy@c6a2c90 {com.example.rajitha.firebasetest/com.example.rajitha.firebasetest.MainActivity}} show : true
04-11 09:32:39.021 10007-12156/com.example.rajitha.firebasetest D/FA: Application backgrounded. Logging engagement
  

这里我附上了我的logcat以获取更多详细信息。运行testdb方法后我得到零大小列表。无法识别问题。提前谢谢。(列表大小日志打印在value之前可能吗?)

1 个答案:

答案 0 :(得分:2)

如果你看一下日志附近的行,你会看到:

5.441 10007-10007/com.example.rajitha.firebasetest E/Test value: test1
04-11 09:31:35.441 10007-10007/com.example.rajitha.firebasetest E/Test value: test2

这意味着你要从你的火力基地获得字符串。

您想知道onCreate方法的列表大小。 Firebase请求为asynchronous,这就是您获得零长度的原因。

将你的尺寸放在回调中:

        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            // Get Post object and use the values to update the UI
            for (DataSnapshot childDataSnapshot : dataSnapshot.getChildren()) {
                Log.e("Test value",""+ childDataSnapshot.getKey());
                list.add(childDataSnapshot.getKey());//displays the key for the node

            }
            Log.e("list size",String.valueOf(list.size()));

        }

在你的firebase在列表中加载字符串之前,你显示零大小