2016-08-06 9 views
0

私は以下のMainActivityクラスからRecipeクラスのgetRecipesFromDBメソッドを呼び出しています。 getRecipesFromDBはFirebase DBからデータを取得し、作成されたrecipeListを取得します。 EventListenerのスコープ内で使用されると、コンテンツを正常に印刷できます。しかし、recipeListがメインクラスに返されると、nullを返します。Javaリスナーの外で変数を返す方法

質問:recipeListをMainActivityクラスに戻すにはどうすればよいですか?現在はnullを返します。

コードは以下の通りです:

方法の
public class Recipe { 

    public String title; 
    public String description; 
    public String image; 
    public String url; 
    public String dietLabel; 

    public Recipe() { 
    } 

    public String getTitle() { 
    return title; 
    } 
public String getDescription() { 
    return description; 
    } 
public String getImageUrl() { 
    return image; 
    } 
public String getInstructionUrl() { 
    return url; 
    } 
public String getLabel() { 
    return dietLabel; 
    } 

public static ArrayList<Recipe> getRecipesFromDB(){ 
    final ArrayList<Recipe> recipeList = new ArrayList<>(); 
    final DatabaseReference mDatabase; 
    final Recipe recipe = new Recipe(); 


mDatabase = FirebaseDatabase.getInstance().getReference("recipes"); 

mDatabase.addValueEventListener(new ValueEventListener() { 
    @Override 
    public void onDataChange(DataSnapshot snapshot) { 
    System.out.println("There are " + snapshot.getChildrenCount() + " recipes"); 

    for (DataSnapshot postSnapshot : snapshot.getChildren()) { 
     Recipe post = postSnapshot.getValue(Recipe.class); 

     recipe.title = post.getTitle(); 
     recipe.description = post.getDescription(); 
     recipe.image = post.getImageUrl(); 
     recipe.url = post.getInstructionUrl(); 
     recipe.dietLabel = post.getLabel(); 

     recipeList.add(recipe); 
    } 
} 

    @Override 
    public void onCancelled(DatabaseError databaseError) { 
    System.out.println("The read failed: " + databaseError.getMessage()); 
    } 
}); 

return recipeList; //This return null 
/*I am trying to return as above to another class where it is called but this returns null. What needs to be done for this to return the recipeList which has been set inside the listener?*/ 
    } 
} 

/* Below is the class where the Recipe.getRecipesFromDB is called */ 

public class MainActivity extends AppCompatActivity { 
    private ListView mListView; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    mListView = (ListView) findViewById(R.id.recipe_list_view); 

    final ArrayList<Recipe> recipeList = Recipe.getRecipesFromDB(); 

    RecipeAdapter adapter = new RecipeAdapter(this, recipeList); 
    mListView.setAdapter(adapter); 
} 
} 
+0

を適切なフォーマットで、[MCVE]を投稿してください - あなたのコードはすべての場所にありますその時、読むのがずっと難しくなります。ここには無関係のコードがたくさんあります。 –

答えて

1

一つ:あなたのMainActivityからリスナークラスにインターフェースを渡し、その後、あなたがそれを受信したときに、あなたのリスナーのデータとメソッドを呼び出すことができます。

public interface DataListener { 
    void newDataReceived(ArrayList<Recipe> recipeList); 
} 

編集:使用例:

あなたのようにあなたのリスナーメソッドが定義されている必要があります:あなたが必要となります

public static ArrayList<Recipe> getRecipesFromDB(DataListener dataListener){ 
    final ArrayList<Recipe> recipeList = new ArrayList<>(); 
    final DatabaseReference mDatabase; 
    final Recipe recipe = new Recipe(); 


    mDatabase = FirebaseDatabase.getInstance().getReference("recipes"); 

mDatabase.addValueEventListener(new ValueEventListener() { 
    @Override 
    public void onDataChange(DataSnapshot snapshot) { 
    System.out.println("There are " + snapshot.getChildrenCount() + " recipes"); 

    for (DataSnapshot postSnapshot : snapshot.getChildren()) { 
     Recipe post = postSnapshot.getValue(Recipe.class); 

     recipe.title = post.getTitle(); 
     recipe.description = post.getDescription(); 
     recipe.image = post.getImageUrl(); 
     recipe.url = post.getInstructionUrl(); 
     recipe.dietLabel = post.getLabel(); 

     recipeList.add(recipe); 
    } 

    // Transaction complete, sending to listener 
    dataListener.newDataReceived(recipeList); 
} 

MainActivityにインターフェイスを実装.ITのようなものにすることができますDataListenerのインスタンスでこのメソッドを呼び出す。あなたはどちらか匿名でこれを呼び出すことができます。

getRecipesFromDB(new DataListener() { 
    @Overrride 
    public void newDataReceived(recipeList) { 
     // Data will be received here 
    } 
}); 

それとも、あなたはあなたのMainActivityメソッドを実装することができ:

public class MainActvity implements DataListener { 
    ... 
    @Overrride 
    public void newDataReceived(recipeList) { 
     // Data will be received here 
    } 
    ... 
} 
+0

ありがとう。できます!私は少し私は理解していないが、それが動作する提案を変更したようにいくつかのクエリがあります。 – dacscan3669

+0

'public static void getRecipesFromDB(final DataListener dataListener){......' – dacscan3669

+0

なぜ私はここで最終的なキーワードを使用しなければならなかったのですか? – dacscan3669

関連する問題