不要在我的TextView上从Firestore获取数据

时间:2018-11-18 22:01:26

标签: android firebase google-cloud-firestore

我试图在TextView上显示文本,该文本是用Firestore上的saveQuote方法保存的,但是只收到null。通过Firebase的“普通”数据库来执行此操作也遇到了一些问题。我为XML上的按钮定义了OnClick方法。有人可以告诉我如何解决该问题吗?我的日志猫说:“ 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());

                    }
                });

    }
}

In the imgae you can see that im recieving "null"

1 个答案:

答案 0 :(得分:0)

根据我的评论

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

在代码中,添加值时,您是在声明字符串值。但是,在检索值时,已声明具有不同值的字符串变量。因此,它找不到您想要的东西。