Send string value from one method to another

Tags: , ,



My first method deviceList is able to work successfully and I am able to store a value in the variable String.id. I am then trying to use that string in my second method addReview1 to implement that string in my database search. However, the method addReview1 is requested through a button onclick. I tried making my second method as addRview1 (String id), but then the button onclick would not work and the app would crash. Therefore I need help in sending the String id from deviceList to addReview1.

Method deviceList

public void deviceList (View V){

    db.collection("devices")
            .whereEqualTo("name", value)
            .get()
            .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                @Override
                public void onComplete(@NonNull Task<QuerySnapshot> task) {
                    if(task.isSuccessful()){
                        for (final QueryDocumentSnapshot document : task.getResult()){
                            Log.i("DeviceId", document.getId());
                            String id = document.getId();


                        }
                    }
                    else
                    {
                        Log.i("Devices","Error" + task.getException());
                        Toast.makeText(getBaseContext(),"Error retrieving getting device names",Toast.LENGTH_LONG).show();
                    }
                }
            });

};

Method addReview1

public void addReview1 (View V){

    Name = (EditText) findViewById(R.id.Name);
    Content = (EditText) findViewById(R.id.Content);
    Rating = (EditText) findViewById(R.id.Rating);
    button4 = (Button) findViewById(R.id.button4);

    Map<String, Object> review = new HashMap<>();

    review.put("date", new Date());
    review.put("name", Name.getText().toString());
    review.put("content", Content.getText().toString());
    review.put("rating", Integer.parseInt(Rating.getText().toString()));

    db.collection("devices").document("1").collection("reviews").add(review)
            .addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
                @Override
                public void onSuccess(DocumentReference documentReference) {
                    Toast.makeText(getApplicationContext(), "Comment added", Toast.LENGTH_LONG).show();
                }
            })
            .addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
                }
            });
    };

Answer

I have managed to come with an easy fix for it.

Declare String above onCreate as such

String NewId;

In method deviceList

public void deviceList (View V){

    db.collection("devices")
            .whereEqualTo("name", value)
            .get()
            .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                @Override
                public void onComplete(@NonNull Task<QuerySnapshot> task) {
                    if(task.isSuccessful()){
                        for (final QueryDocumentSnapshot document : task.getResult()){
                            Log.i("DeviceId", document.getId());
                            NewId = document.getId();


                        }
                    }
                    else
                    {
                        Log.i("Devices","Error" + task.getException());
                        Toast.makeText(getBaseContext(),"Error retrieving getting device names",Toast.LENGTH_LONG).show();
                    }
                }
            });

};

In method addReview1

public void addReview1 (View V){

    Name = (EditText) findViewById(R.id.Name);
    Content = (EditText) findViewById(R.id.Content);
    Rating = (EditText) findViewById(R.id.Rating);
    button4 = (Button) findViewById(R.id.button4);

    Map<String, Object> review = new HashMap<>();

    review.put("date", new Date());
    review.put("name", Name.getText().toString());
    review.put("content", Content.getText().toString());
    review.put("rating", Integer.parseInt(Rating.getText().toString()));

    db.collection("devices").document(NewId).collection("reviews").add(review)
            .addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
                @Override
                public void onSuccess(DocumentReference documentReference) {
                    Toast.makeText(getApplicationContext(), "Comment added", Toast.LENGTH_LONG).show();
                }
            })
            .addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
                }
            });
    };


Source: stackoverflow