2017-04-11 32 views
-1

代码获得零尺寸列表的Android

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: [email protected] 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 [email protected] {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 method.Can不能确定提前issue.Thanks后得到了零大小的列表。(名单大小日志打印value.how有可能过吗?)

+3

Firebase操作是_asynchronous_。你的'list'只有在'onDataChange()'运行时才会有任何内容,这在'onCreate()'日志打印之前不会发生。 –

+1

这是因为你记录你的列表大小之前onDataChange –

+0

好的我怎么能检查onDataChange()是否完成? – HRCJ

回答

2

如果你看看行borrom靠近你日志,你看到这个:

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 

这意味着你从你的firebase获取字符串。

想要知道onCreate方法的列表大小。 Firebase的要求是asynchronous,这就是为什么你的零长度。

将您的内部回调大小cheking:

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

     } 

您都出现之前你的火力大小为零加载你的字符串列表。

+0

我解决了这个问题。谢谢。我将其标记为正确。 – HRCJ