1

I'm trying to learn Java and right now I have a little problem. I'm trying to get a value from a method to another:

public void Save(View v){
        Contador c = new Contador();
        c.nContador = "123";
        c.descricao = "Teste";
        c.tipoTarifa = "Tri";

        c.leituras = new ArrayList<Leitura>();

        Leitura l1 = new Leitura();
        l1.tipoLeitura = "HP";
        l1.valorLeitura = "321";

        Leitura l2 = new Leitura();
        l1.tipoLeitura = "HV";
        l1.valorLeitura = "654";

        c.leituras.add(l1);
        c.leituras.add(l2);

        Gson gson = new Gson();
        String json = gson.toJson(c);
        System.out.print(json);
    }

public void Read(View v){
    Toast
            .makeText(this, "I want the json here", Toast.LENGTH_LONG)
            .show();
    }

So I have 2 buttons: one for save and one for read. When I press "Save" the method "Save" is called and it creates the string json. I want to get that same string when i click the button "Read". How can I do this?

Ziki
  • 1,390
  • 1
  • 13
  • 34
Ravers
  • 988
  • 2
  • 14
  • 45
  • Do you need to return the string `json` from your `Save` method? – khelwood Oct 30 '15 at 12:03
  • So I have 2 buttons, one for save and one for read. When i click "Save", i create the String json. I want to get that same string when i click the button "Read". – Ravers Oct 30 '15 at 12:05
  • [whats-the-difference-between-javascript-and-java](http://stackoverflow.com/questions/245062/whats-the-difference-between-javascript-and-java) – Ziki Oct 30 '15 at 12:28

1 Answers1

1

Note: Please use the Java Convention for naming classes, attributes and methods. The method name should be in mixed case with the first letter lowercase, with the first letter of each internal word capitalized.


Approach 1

You can return the generated string in save() and call it in read() to get it.

public String save(View v) {
    // beginning of implementation ommitted for brevity
    Gson gson = new Gson();
    String json = gson.toJson(c);
    return json;
}

public void read(View v) {
    String json = save(v);
    Toast.makeText(this, json, Toast.LENGTH_LONG).show();
}

I do not recommend doing it as you would create a dependency to save() in your read() method.


Approach 2

Same approach as before but without the dependency, using an auxiliary method.

public String save(View v) {
    // beginning of implementation ommitted for brevity
    Gson gson = new Gson();
    String json = gson.toJson(c);
    return json;
}

public void read(View v, String json) {
    Toast.makeText(this, json, Toast.LENGTH_LONG).show();
}

public void process(View v) {
    String json = save(v);
    read(v, json);
}

I still do not recommend doing it as you would not sotre the string anywhere else, and everytime you called process() to get the input needed to read() you would make an indirect call to save().


Approach 3

Set a class attribute containing the created string and use it in read().

public class StackOverflowExample {

    private String json;

    public String save(View v) {
        // beginning of implementation ommitted for brevity
        Gson gson = new Gson();
        json = gson.toJson(c); // save the JSON in your class attribute
        System.out.print(json);
    }

    public void read(View v) {
        Toast.makeText(this, json, Toast.LENGTH_LONG).show();
    }

}
Bruno Toffolo
  • 1,504
  • 19
  • 24