0

I'm just learning programming in the Android and Firebase. I've learned from the Youtube tutorial how to add a task to the database and how to display it. Unfortunately, at this moment I can only add one element and only one display (I do it in TextView). Can you help me create a list that will display many elements? There are not many tutorials to Cloud Firestore.

My next goal is to edit and delete data, but first I want to learn it.

This is the code of my two fragments:

public class AddFragment extends Fragment implements View.OnClickListener{

    private static final String CEL_KEY = "Cel";
    private static final String MIEJSCE_KEY = "Miejsce";
    private static final String TAG= "TAG";

    private DocumentReference mDocRef = FirebaseFirestore.getInstance().document("goalsData/daily");

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View layout = inflater.inflate(R.layout.fragment_add_goal, container, false);

        Button saveButton = (Button)layout.findViewById(R.id.bn_save);
        saveButton.setOnClickListener(this);

        return layout;
    }

    @Override
    public void onClick(View v){
        switch (v.getId()) {
            case R.id.bn_save:
                saveGoals();
                break;
        }
    }

    public void saveGoals (){

        EditText celView = (EditText) getActivity().findViewById(R.id.et_cel);
        EditText miejsceView = (EditText) getActivity().findViewById(R.id.et_miejsce);
        String celText = celView.getText().toString();
        String miejsceText = miejsceView.getText().toString();

        if (celText.isEmpty() || miejsceText.isEmpty()) { return; }
        Map<String, Object> dataToSave = new HashMap<String, Object>();
        dataToSave.put(CEL_KEY, celText);
        dataToSave.put(MIEJSCE_KEY, miejsceText);

        mDocRef.set(dataToSave).addOnSuccessListener(new OnSuccessListener<Void>() {
            @Override
            public void onSuccess(Void aVoid) {
                Log.d(TAG, "Dokument został zapisany!");
            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                Log.w(TAG, "Błąd, dokument nie został zapisany.", e);
            }
        });

    }

}

Display:

public class DailyFragment extends Fragment implements View.OnClickListener{

    private static final String CEL_KEY = "Cel";
    private static final String MIEJSCE_KEY = "Miejsce";
    private static final String TAG= "TAG";

    private DocumentReference mDocRef = FirebaseFirestore.getInstance().document("goalsData/daily");

    TextView wszystkieCele;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View layout = inflater.inflate(R.layout.fragment_daily, container, false);

        Button fetchButton = (Button)layout.findViewById(R.id.bn_fetch);
        fetchButton.setOnClickListener(this);
        wszystkieCele = (TextView) layout.findViewById(R.id.tv_goals);

        return layout;
    }

    @Override
    public void onStart(){
        super.onStart();
        mDocRef.addSnapshotListener((new EventListener<DocumentSnapshot>() {
            @Override
            public void onEvent(DocumentSnapshot documentSnapshot, FirebaseFirestoreException e) {
                if(documentSnapshot.exists()){
                    String celText = documentSnapshot.getString(CEL_KEY);
                    String miejsceText = documentSnapshot.getString(MIEJSCE_KEY);
                    wszystkieCele.setText(getString(R.string.cel_wynik, celText, miejsceText));
                } else if (e != null){
                    Log.w(TAG, "Wyjątek!, e");
                }
            }
        }));
    }


    @Override
    public void onClick(View v){
        switch (v.getId()) {
            case R.id.bn_fetch:
                fetchQuote(v);
                break;
        }
    }

    public void fetchQuote (View view){
        mDocRef.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
            @Override
            public void onSuccess(DocumentSnapshot documentSnapshot) {
                if(documentSnapshot.exists()){
                    String celText = documentSnapshot.getString(CEL_KEY);
                    String miejsceText = documentSnapshot.getString(MIEJSCE_KEY);
                    wszystkieCele.setText(getString(R.string.cel_wynik, celText, miejsceText));
                }
            }
        });
    }

}
5
  • There is actually a tutorial with Cloud Firestore and Android, which explains how to add and display data, step by step. You may take a look. Regarding your code, what is wrong with it? Commented Feb 15, 2018 at 8:47
  • Your tutorials are great. I have not seen them before because they are new. Thank you. Currently, I have new problems with the code, because I started to do it with a different methods (your tutorials). dailyGoalsRef = rootRef.collection ("goalsData"). document ("[email protected]"). collection ("dailyGoals"); -> NullPointerException - this is very popular, but unfortunately I do not know how to fix it in this case Commented Feb 15, 2018 at 18:25
  • Thanks @Kamil. Post a new question with the exact issue and I'll take a look at it. Post also here a comment to be notified. Commented Feb 15, 2018 at 18:31
  • stackoverflow.com/questions/48814388/… - it's for U. Fingers crossed Commented Feb 15, 2018 at 18:56
  • Got the issue. See my answer. Commented Feb 15, 2018 at 19:07

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.