- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我刚刚编写了一个基本代码来从 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/
我注意到 Activity 类有两种不同的方法来获取字符串资源。这可以通过使用: getString(int resId):从应用程序包的默认字符串表中返回一个本地化字符串。 getResources
有什么值得注意的区别吗?我说的是一个花费大量时间读取查询结果的程序,并且某些运行(这些是网络点击)花费了 2 分钟以上的时间。如果我可以通过使用整数而不是字段名称来节省几秒钟,我想我可以免费获得一些时
要获取设备 ID,我通常使用以下代码段: String android_id = Settings.System.getString(getContentResolver(), Secure.ANDR
我在文件夹中有一些带有字符串的本地化文件,例如 values values-ru values-es etc. 在每个文件夹中都有一个 strings.xml 文件,其键相同但值不同。 在 Debug
我在 Assets 文件夹中使用不同的文本文件(arabic.txt,english.txt)我想更改设备语言的文件库 值/字符串文件中可以设置文件名吗? public static class Ti
我有一个扩展应用程序的类。我需要从 strings.xml 文件中获取一个字符串。 我正在使用此类来管理全局状态,因此它是通过 list 加载的。 加载时,getString() 生成一个空指针引用。
所以我在strings.xml中有很多字符串,它们的记录格式是: xxxxxxxxxxx xxxxxxxxxxx xxxxxxxxxxx ...... 现在我想一个一个地加载它们,而不必输入所有的字符
我有一个类,我应该在其中将一些字符串(取自 strings.xml)插入到 StringBuffer 中。这将传递给一个 Activity 。问题是我应该使用 getString() 方法,但我不能,
有没有办法以编程方式更改 R.string?因为它会抛出错误。 基本上我想这样做:String parkAdd = getString(R.string.stg_ParkAddress_+id); 因
我遇到了一个只出现在这个语法中的崩溃,在这个语句之后它崩溃了 ats+=getString(R.string.gal_grietineles , grietinele)+"\n";当我使用 ats+=
这是我的 onActivityResult 方法的代码: @Override public void onActivityResult(int requestCode, int resultCode,
我是android新手,我正在编写一个应用程序来根据纬度和经度获取地址。在 FetchAddressIntentService 类中,我在 getString() 函数中收到一个错误,指出将字符串添加
我遇到的问题是 getString() 返回的不是 id 的字符串,而是另一个字符串。但首先这是我的代码: public public class ShopFragment extends Fragm
在我的应用程序的应用程序类的 onCreate 中,我有这段代码 Locale current = getResources().getConfiguration().locale;
我想知道 getString()。我可以看到执行 getString(R.string.some_text) 是有效的。 getResources().getString(R.string.conne
基于 Android 文档: https://developer.android.com/training/basics/supporting-devices/languages https://de
我使用的是 Delphi 2009。 这对我来说适用于所有情况,除了一种情况: var BOMLength: integer; Buffer: TBytes; Encoding: TEnc
Dictionary Fields = new Dictionary();for (int i = 0; i < reader.FieldCount; i++){ Fields.Add(rea
我有一个名为 spiliaBeach 的 Place Object 实例,它接受您在下面看到的参数: public PlaceObject spiliaBeach = new PlaceObject(
我有: Toast.makeText(NewChatActivity.this, getString(R.string.invitation_sent_prompt, contact), Toast.
我是一名优秀的程序员,十分优秀!