2016-07-12 10 views
2

私は現在、データベースとしてfirebaseを使用していますが、onDataChangeメソッドで変数を取得し、それらをグローバル変数に割り当てると、NULL変数を取得しましたが、 onDataChangeメソッドでこれらの変数を呼び出すと、nullではありません。null変数がonDataChangeメソッドから外へ

public class PositionateMarkerTask extends AsyncTask { 
    public ArrayList<Location> arrayList= new ArrayList<>(); 
    public void connect() { 
     //setting connexion parameter 
     final Firebase ref = new Firebase("https://test.firebaseio.com/test"); 
     Query query = ref.orderByChild("longitude"); 

     //get the data from the DB 
     query.addListenerForSingleValueEvent(new ValueEventListener() { 

      @Override 
      public void onDataChange(DataSnapshot dataSnapshot) { 
       //checking if the user exist 
       if(dataSnapshot.exists()){ 
        for (DataSnapshot userSnapshot : dataSnapshot.getChildren()) { 
         //get each user which has the target username 
         Location location =userSnapshot.getValue(Location.class); 
          arrayList.add(location); 
         //if the password is true , the data will be storaged in the sharedPreferences file and a Home activity will be launched 
        } 
       } 
       else{ 
        System.out.println("not found"); 
       } 
      } 

      @Override 
      public void onCancelled(FirebaseError firebaseError) { 
        System.out.println("problem "); 

      } 
     }); 
    } 

    @Override 
    protected Object doInBackground(Object[] params) { 
     connect(); 
     return null; 
    } 

    @Override 
    protected void onPostExecute(Object o) { 
     super.onPostExecute(o); 

     System.out.println("the firs long is"+arrayList.get(0).getLongitude()); 

    } 
} 
+0

あなたのデータベース構造を提供することができますが動作していただければ幸いですか! – Wilik

答えて

0

同じ問題が発生しました。 onDataChange内のarraylistの値を別のメソッドに渡すだけです。

myFirebaseRef.addListenerForSingleValueEvent(new ValueEventListener() { 
     @Override 
     public void onDataChange(DataSnapshot dataSnapshot) { 

      Log.d("FIREBASE",String.valueOf(dataSnapshot.getChildrenCount())); 


      if (dataSnapshot.exists()){ 

       for (DataSnapshot snapshot : dataSnapshot.getChildren()){ 

        Log.d("FIREBASE ","FOR LOOP"); 


        //Arraylist codes 
        iosTitleVal.add(snapshot.getKey().toString()); 
        iosDescVal.add(snapshot.getValue().toString()); 
        Log.d("FIREBASE","ArrayList KEY IS "+iosTitleVal); 

       //Passing arraylist values to another method 
        storageContainer(iosTermTitle,iosTermDesc); 

       } 

      }else { 

       Log.d("FIREBASE","NO DATAS"); 

      } 

     } 

     @Override 
     public void onCancelled(DatabaseError databaseError) { 

     } 
    }); 

は、ここに私のstorageContainer方法です。このメソッドでは、arrraylist値をRecyclerViewアダプタのコンストラクタに渡しました。

public void storageContainer(ArrayList<String> arrayListTitle, ArrayList<String> arrayListDesc){ 


    iosTitleStorage = arrayListTitle; 
    iosDescStorage = arrayListDesc; 


    //No null exception here... 
    Log.d("FIREBASE"," My Data KEY "+iosTitleStorage +" and VAL "+iosDescStorage); 

} 

私はそれが

関連する問題