gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 03:26:33 25 4
gpt4 key购买 nike

我正在从下面列出的 MainActivity 类调用 Recipe 类的 getRecipesFromDB 方法。 getRecipesFromDB 从 Firebase DB 检索数据并填充recipesList。在 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);
}
}

最佳答案

其中一种方法:您可以将接口(interface)从 MainActivity 传递到监听器类,然后在收到监听器中的数据时调用该方法。在MainActivity中实现接口(interface)。它可以是这样的:

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

您需要使用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
}
...
}

关于Java 如何返回监听器外部的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38802039/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com