Arraylist statico returns size 0 when I try to recover it in an Activity

Asked

Viewed 45 times

0

I have This class:

abstract public class ListaEstaticaDeErros {
private static ArrayList<DescritorDeErro> listErros = new ArrayList<DescritorDeErro>();
private static boolean fimSinc = false;
private static int municipio = 0;

public static boolean getFimSinc(){
    return fimSinc;
}

public static int getMunicipio(){
    return municipio;
}

public static void setMunicipio(int i){
    municipio = i;
}

public static void setFimSinc(boolean b){
    fimSinc = b;
}

public static void setListErros(ArrayList<DescritorDeErro> list){
    listErros = list;
}

public static void addRegListErros(DescritorDeErro obj){
    listErros.add(obj);
}

public static ArrayList<DescritorDeErro> getListErros(){
    return listErros;
}

}

Activity function that I try to recover and show the list size:

public void montarResultados(){
        Log.e("Teste-esus", String.valueOf(ListaEstaticaDeErros.getListErros().size()));
        Bundle bundle = getIntent().getExtras();
        ArrayList<DescritorDeErro> lst = bundle.getParcelableArrayList("lista");
        int mun = bundle.getInt("municipio");
        String usuario = bundle.getString("usuario");
        if(lst.size() > 0) {
            new ChargeList().execute(lst, mun, usuario);
        }else{
            txtVInfo.setText("Nenhum erro capturado ate este momento de sincronização.");
        }
        if(bundle.getBoolean("fim_sinc")) {
            txtEmail.setVisibility(View.VISIBLE);
        }else{
            txtEmail.setVisibility(View.INVISIBLE);
        }
    }

And inside an Activity I need to recover this listErros and it always returns me 0, ie without any element, but if I catch her in another class that is not an Activity I can without problems, then the solution I made was to pick her up in another class and send via Intent to the Activity I need, however I would like to take her directly from Activity to be able to implement what I need.

  • You can add the Activity code that accesses the list?

  • I added the function

2 answers

0

Create an interface like below, which will be able to recover the values of the listErros.

public interface Listaestaticadeerros {

Static Arraylist listErros = new Arraylist();

... }

  • It does not work, besides still not being able to see the contents of the list, the variables of an interface are "final" and therefore I can not modify any of them.

0

You can do it like this:

    public class ListaEstaticaDeErros {
    private ArrayList<DescritorDeErro> listErros = new ArrayList<DescritorDeErro>();
    private boolean fimSinc = false;
    private int municipio = 0;

    private static ListaEstaticaDeErros instance;

    public static ListaEstaticaDeErros getInstance() {
        if (instance == null) {
            instance = new ListaEstaticaDeErros()
        }

        return instance;
    }

    public  boolean getFimSinc(){
        retmSinc;
    }

    public  int getMunicipio(){
        retnicipio;
    }

    public  void setMunicipio(int i){
        mun = i;
    }

    public  void setFimSinc(boolean b){
        fim b;
    }

    public  void setListErros(ArrayList<DescritorDeErro> list){
        lis = list;
    }

    public  void addRegListErros(DescritorDeErro obj){
        lis.add(obj);
    }

    public  ArrayList<DescritorDeErro> getListErros(){
        return listErros;
    }
}
  • I’ve done this way too and nothing, it was the way I did before q this in the question.

  • @Joãocarlos why your class is Abstract?

  • I tried clotheslines shapes I found on the internet, one was equal to his reply, returned me the same result as the class Abstract, but in short has no reason to be Abstract.

Browser other questions tagged

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