2016-07-12 2 views
2

저는 현재 데이터베이스로 firebase를 사용하고 있지만 onDataChange 메서드에서 변수를 가져 와서 전역 변수에 할당하면 안드로이드 응용 프로그램을 개발하고 있습니다. 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 Adapter의 생성자에 전달했습니다.

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

} 

가 나는

관련 문제