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());
}
}
あなたのデータベース構造を提供することができますが動作していただければ幸いですか! – Wilik