gpt4 book ai didi

java - Document.getString 没有给出输出 (Firestore)

转载 作者:行者123 更新时间:2023-11-29 23:27:30 24 4
gpt4 key购买 nike

enter image description here

我刚刚编写了一个基本代码来从 Firestore 数据库中获取多个文档。任务成功,您可以在日志中看到 document.getId()document.getData() 值,但在那之后,当我使用 document 时。 getString(),它给出一个空白输出。我一直在努力找出我的错误,但我无法找到,而且我明天要提交一个项目。忽略下面给定代码中的所有 textview 声明:

    private FirebaseFirestore db;
private int month,year;
private Calendar calendar;
private String email;
private String name,points;
private int i,j;
private String q,p,r;
public String[][] data= new String[10][10];
private TextView rankingTextView,a,b;
public TextView t01,t02,t03,t10,t11,t12,t20,t21,t22,t30,t31,t32,t40,t41,t42,t50,t51,t52,t60,t61,t62,t70,t71,t72,t80,t81,t82,t90,t91,t92;

private static final String TAG = "MyApp";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_leaderboard);
calendar=Calendar.getInstance();
t01=(TextView)findViewById(R.id.t00);
t02=(TextView)findViewById(R.id.t01);
t03=(TextView)findViewById(R.id.t02);
t10=(TextView)findViewById(R.id.t10);
t11=(TextView)findViewById(R.id.t11);
t12=(TextView)findViewById(R.id.t12);
t20=(TextView)findViewById(R.id.t20);
t21=(TextView)findViewById(R.id.t21);
t22=(TextView)findViewById(R.id.t22);
t30=(TextView)findViewById(R.id.t30);
t31=(TextView)findViewById(R.id.t31);
t32=(TextView)findViewById(R.id.t32);
t40=(TextView)findViewById(R.id.t40);
t41=(TextView)findViewById(R.id.t41);
t42=(TextView)findViewById(R.id.t42);
t50=(TextView)findViewById(R.id.t50);
t51=(TextView)findViewById(R.id.t51);
t52=(TextView)findViewById(R.id.t52);
t60=(TextView)findViewById(R.id.t60);
t61=(TextView)findViewById(R.id.t61);
t62=(TextView)findViewById(R.id.t62);
t70=(TextView)findViewById(R.id.t70);
t71=(TextView)findViewById(R.id.t71);
t72=(TextView)findViewById(R.id.t72);
t80=(TextView)findViewById(R.id.t80);
t81=(TextView)findViewById(R.id.t81);
t82=(TextView)findViewById(R.id.t82);
db= FirebaseFirestore.getInstance();
email = getIntent().getStringExtra("email");
month = calendar.get(Calendar.MONTH);
month = month + 1;
year = calendar.get(Calendar.YEAR);
i=0;
j=0;
dothis();
Toast.makeText(this, "MO", Toast.LENGTH_SHORT).show();
t01.setText(data[0][0]);
t02.setText(data[0][1]);
t11.setText(data[1][0]);
t12.setText(data[1][1]);
t21.setText(data[2][0]);
t22.setText(data[2][1]);
t31.setText(data[3][0]);
t32.setText(data[3][1]);
t41.setText(data[4][0]);
t42.setText(data[4][1]);
t51.setText(data[5][0]);
t52.setText(data[5][1]);
}
private void dothis()
{i=0;
j=0;

db.collection("users")
// .whereGreaterThanOrEqualTo("points" + Integer.toString(month) + Integer.toString(year), "0")
.orderBy("points" + Integer.toString(month) + Integer.toString(year)).limit(10)
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (QueryDocumentSnapshot document : task.getResult()) {
if(document.exists()) {
Log.d(TAG, document.getId() + " => " + document.getData() + " ");
data[i][j] = document.getString("username");
data[i][j + 1] = document.getString("points" + Integer.toString(month) + Integer.toString(year));
i++;
Toast.makeText(Leaderboard.this, "OK", Toast.LENGTH_SHORT).show();
}
}
} else {
Log.d(TAG, "Error getting documents: ", task.getException());
}
}
});

}

请不要重复我的问题。放之前我搜索了很多。

最佳答案

根据您的评论,您说什么时候调用以下代码行:

t01.setText(data[0][0]);

t01 TextView 没有任何设置,这是正常行为,因为来自 Firebase 数据库的数据是异步的。这意味着 onComplete() 方法在被调用后立即返回,它返回的 Task 的回调将在稍后调用。

无法保证需要多长时间。因此,在数据可用之前可能需要几百毫秒到几秒。因为此方法会立即返回,所以您尝试在 onComplete() 方法之外使用的 data[0][0] 变量的值将不会被填充来自回调。

基本上,您正在尝试从异步 API 同步使用值。那不是一个好主意。您应该按预期异步处理 API。

此问题的快速解决方案是仅在 onComplete() 方法内将所有这些文本设置到您的 TextView。如果您需要将它们设置在外部,我建议深入异步世界并从这个 post 中查看我的答案的最后一部分。 我在其中解释了如何使用自定义回调来完成。你也可以看看这个 video 以便更好地理解。

关于java - Document.getString 没有给出输出 (Firestore),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53307156/

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