首页 > 解决方案 > 不要在我的 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());

                    }
                });

    }
}

在 imgae 中,您可以看到我收到“null”

标签: androidfirebasegoogle-cloud-firestore

解决方案


根据我的评论

String title = documentSnapshot.getString("QUOTE_KEY");
String description = documentSnapshot.getString("AUTHOR_KEY");

在您的代码中,当您添加值时,您正在声明一个字符串值。但是,当您检索值时,您已经声明了具有不同值的字符串变量。因此,它无法找到您要查找的内容。


推荐阅读