Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/376.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java如何在监听器外部返回变量_Java_Android - Fatal编程技术网

Java如何在监听器外部返回变量

Java如何在监听器外部返回变量,java,android,Java,Android,我正在调用下面列出的MainActivity类的Recipe类的GetRecipes FromDB方法。getRecipesFromDB从Firebase DB检索数据并填充recipeList。在EventListener范围内使用时,我能够成功打印内容。但是当recipeList返回到主类时,它返回null 问题:如何将recipeList返回到MainActivity类?当前它返回null 代码如下: public class Recipe { public String titl

我正在调用下面列出的MainActivity类的Recipe类的GetRecipes FromDB方法。getRecipesFromDB从Firebase DB检索数据并填充recipeList。在EventListener范围内使用时,我能够成功打印内容。但是当recipeList返回到主类时,它返回null

问题:如何将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);
 }
}
公共类配方{
公共字符串标题;
公共字符串描述;
公共字符串图像;
公共字符串url;
公共字符串标签;
公共配方(){
}
公共字符串getTitle(){
返回标题;
}
公共字符串getDescription(){
返回说明;
}
公共字符串getImageUrl(){
返回图像;
}
公共字符串getInstructionUrl(){
返回url;
}
公共字符串getLabel(){
退货标签;
}
公共静态ArrayList GetRecipes FromDB(){
最终ArrayList recipeList=新ArrayList();
最终数据库参考mDatabase;
最终配方=新配方();
mDatabase=FirebaseDatabase.getInstance().getReference(“配方”);
mDatabase.addValueEventListener(新的ValueEventListener(){
@凌驾
公共无效onDataChange(数据快照快照){
System.out.println(“有”+snapshot.getChildrenCount()+“配方”);
对于(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.添加(配方);
}
}
@凌驾
已取消的公共void(DatabaseError DatabaseError){
System.out.println(“读取失败:+databaseError.getMessage());
}
});
return recipeList;//此返回值为null
/*我正试图如上所述返回到另一个调用它的类,但它返回null。需要做什么才能返回在侦听器中设置的recipeList*/
}
}
/*下面是调用Recipe.getRecipesFromDB的类*/
公共类MainActivity扩展了AppCompatActivity{
私有列表视图;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mListView=(ListView)findViewById(R.id.recipe\u list\u视图);
final ArrayList recipeList=Recipe.getRecipesFromDB();
RecipeAdapter=新的RecipeAdapter(此为recipeList);
mListView.setAdapter(适配器);
}
}

方法之一:您可以将接口从
main活动
传递给侦听器类,然后在收到该接口时使用侦听器中的数据调用该方法。在
MainActivity
中实现该接口。它可以类似于:

public interface DataListener {
    void newDataReceived(ArrayList<Recipe> recipeList);
}
或者,您可以让您的
main活动
实现以下方法:

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

方法之一:您可以将接口从
MainActivity
传递到侦听器类,然后在收到该接口时使用侦听器中的数据调用该方法。在
MainActivity
中实现该接口。它可以类似于:

public interface DataListener {
    void newDataReceived(ArrayList<Recipe> recipeList);
}
或者,您可以让您的
main活动
实现以下方法:

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

请用适当的格式发布a-你的代码现在到处都是,使它更难阅读,这里有很多不相关的代码。请用适当的格式发布a-你的代码现在到处都是,使它更难阅读,这里有很多不相关的代码。非常感谢。它起作用了!我有一些疑问,因为我修改了一些建议,我不明白,但它是有效的。不确定为什么我必须在这里使用final关键字?很抱歉,我想适当地粘贴代码,但注释框很奇怪,它不允许粘贴完整的代码/格式化,并且对字符数也有限制。Recipe.getRecipesFromDB(主要活动。本);原因与Java的限制有关。请在此处阅读更多信息:非常感谢。它很有效!我有一些疑问,因为我修改了一些建议,但我不理解,但它有效。不确定为什么我必须在此处使用final关键字?抱歉,我想适当地粘贴代码,但注释框很奇怪,它不允许粘贴完整代码/格式化,字符数也有限制。Recipe.getRecipesFromDB(MainActivity.this);原因与Java的限制有关。请在此处阅读更多信息: