How to save variables to be used in the next . Java?

Asked

Viewed 1,208 times

0

My main (parser) code is this way:

public class pFormasDePagamento {

public static void parseXML(String xml)
        throws ParserConfigurationException, SAXException, IOException {
    InputSource is = new InputSource(new StringReader(xml));
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document doc = dBuilder.parse(is);
    doc.getDocumentElement().normalize();
    String valFormaOutros = doc.getElementsByTagName("VALOR").item(0)
            .getTextContent();
    String descForma = doc.getElementsByTagName("DESCRICAO").item(0)
            .getTextContent();
    String valForma = doc.getElementsByTagName("VALOR").item(1)
            .getTextContent();
    String descForma1 = doc.getElementsByTagName("DESCRICAO").item(1)
            .getTextContent();
    String valForma1 = doc.getElementsByTagName("VALOR").item(2)
            .getTextContent();
    String descForma2 = doc.getElementsByTagName("DESCRICAO").item(2)
            .getTextContent();
    String valForma2 = doc.getElementsByTagName("VALOR").item(3)
            .getTextContent();
    String descForma3 = doc.getElementsByTagName("DESCRICAO").item(3)
            .getTextContent();
    String valForma3 = doc.getElementsByTagName("VALOR").item(4)
            .getTextContent();
    String descForma4 = doc.getElementsByTagName("DESCRICAO").item(4)
            .getTextContent();
    String valForma4 = doc.getElementsByTagName("VALOR").item(5)
            .getTextContent();
    String descForma5 = doc.getElementsByTagName("DESCRICAO").item(5)
            .getTextContent();
    String valForma5 = doc.getElementsByTagName("VALOR").item(6)
            .getTextContent();
}}

I would like to save all these strings (valForma1...5) to be reused in others. Java

How can I fix this?

2 answers

2


Create a class that contains these properties and return an instance(in an array, for example) of that class.

Kind of:

public class FormaDePagamento {
    public String desc;
    public String valor;
}

public class pFormasDePagamento {

    public static FormaDePagamento[] parseXML(String xml)
            throws ParserConfigurationException, SAXException, IOException {
        InputSource is = new InputSource(new StringReader(xml));
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(is);
        doc.getDocumentElement().normalize();

        FormaDePagamento[] formas = new FormaDePagamento[5];
        for(int n = 0; n < 5; n++) {
            formas[n] = new FormaDePagamento();
            formas[n].desc = doc.getElementsByTagName("DESCRICAO").item(n).getTextContent();
            formas[n].valor = doc.getElementsByTagName("VALOR").item(n).getTextContent();
        }

        return formas;
    }
}

/* in: "forms[n]. val = doc.getelementsbytagname" would not be: "forms[n]. value = doc.getelementsbytagname" ? */

  • how would I make the call then Rafael?

  • how can I call separate variables @Rafaeltelles?

  • It could be something like "Build up[] forms = pFormasDePackage.parseXML(xml)", and in this array you would have all xml objects.

0

Renan, you can use the native database of Android Sqlite or Sharedpreferences. In the case of Sharedpreferences it is very simple, and can be used in your Activity in this way:

public static final String KEY = "nome_chave";
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
/*Para armazenar um valor*/
sharedPreferences.edit().putString(KEY , "valor").commit();
/*Para ler um valor armazenado*/
sharedPreferences.getString(KEY , "valor padrão caso não tenha valor para a chave");

Browser other questions tagged

You are not signed in. Login or sign up in order to post.