android - 不要在我的 TextView 上从 Firestore 获取数据
问题描述
我试图在我用 Firestore 上的 saveQuote 方法保存的 TextView 上显示文本,但只收到 null。通过使用 Firebase 的“普通”数据库执行此操作,我也遇到了一些问题。我为 xml 上的按钮定义了 OnClick 方法。有人可以告诉我如何解决这个问题吗?我的 logcat 说“E/memtrack:无法加载 memtrack 模块”、“GCM_HB_ALARM 版本没有匹配的获取!” 和“没有为 HAL 提供足够的数据,预期的位置”。
public class MainActivity extends AppCompatActivity {
private static final String TAG = "InspiringQuote";
public static final String AUTHOR_KEY = "author";
public static final String QUOTE_KEY = "quote";
private TextView textViewData;
private DocumentReference mDocRef = FirebaseFirestore.getInstance().document("sampleData/inspiration");
TextView mQuoteTextView;
private FirebaseFirestore db = FirebaseFirestore.getInstance();
private CollectionReference noteRef = db.collection("inspiration");
private Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//mQuoteTextView = (TextView) findViewById(R.id.text_view_data);
textViewData = (TextView) findViewById(R.id.text_view_data);
//Toolbar Options
//toolbar = (Toolbar) findViewById(R.id.testToolbar);
//setSupportActionBar(toolbar);
//getSupportActionBar().setTitle("HomePage");
//getSupportActionBar().setDisplayHomeAsUpEnabled(true);
//
}
public void saveQuote(View view){
EditText quoteView = (EditText) findViewById(R.id.editTextQuote);
EditText authorVIew = (EditText) findViewById(R.id.editTextAuthor);
String quoteText = quoteView.getText().toString();
String authorText = authorVIew.getText().toString();
if (quoteText.isEmpty() || authorText.isEmpty()){return;}
Map<String, Object> dataToSave = new HashMap<String, Object>();
dataToSave.put("QUOTE_KEY", quoteText);
dataToSave.put("AUTHOR_KEY", authorText);
mDocRef.set(dataToSave).addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Log.d(TAG, "Document has been saved");
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.w(TAG, "Document was not saved", e);
}
});
}
public void loadNote(View v){
mDocRef.get()
.addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
@Override
public void onSuccess(DocumentSnapshot documentSnapshot) {
if(documentSnapshot.exists()){
String title = documentSnapshot.getString(QUOTE_KEY);
String description = documentSnapshot.getString(AUTHOR_KEY);
//Map<String, Object> note = documentSnapshot.getData();
textViewData.setText("Title" + title + "\n" + "Description: " + description);
}else{
Toast.makeText(MainActivity.this, "Document does not exits", Toast.LENGTH_SHORT);
}
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(MainActivity.this, "ERROR", Toast.LENGTH_SHORT).show();
Log.d(TAG, e.toString());
}
});
}
}
解决方案
根据我的评论
String title = documentSnapshot.getString("QUOTE_KEY");
String description = documentSnapshot.getString("AUTHOR_KEY");
在您的代码中,当您添加值时,您正在声明一个字符串值。但是,当您检索值时,您已经声明了具有不同值的字符串变量。因此,它无法找到您要查找的内容。
推荐阅读
- javascript - 在提交 redux 表单循环 axios 帖子以获取多个值
- html - 将 X 轴除以 HIghchart 中的可点击区域
- c++ - std::array 的堆分配
- rest - 是否可以将 Pydio 的 v1 或 v2 API 与 Pydio Cells 一起使用?
- git - 从 bitbucket 管道推送 git 标签?
- javascript - 从另一个文件中提取 JS 对象到变量中
- android - 尝试在空对象引用上调用虚拟方法“boolean com.google.firebase.firestore.QuerySnapshot.isEmpty()”
- javascript - socket.io/socket.io.js 的位置在哪里?
- svg - 可以创建一个直接在浏览器中显示的 SVGZ 文件吗?
- java - 我的应用程序运行良好,除了 android 9 版本。我不明白是什么问题