No Save SparceBooleanArray Listview Android

0

I am wanting to save the positions of the items marked in listView by means of a chekbox in each item. Each time I check the items, I only save the last checked data. To check I made it show with each check a Log of sparseBooleanArray.size () in the console. The result you give me is always 1. Be False or True. How to save all marked items?

 public View getView(final int i, View view1, ViewGroup viewGroup) {
    final GuardarCheckBoxPosition guardarCheck = new GuardarCheckBoxPosition(this);


    Chekcompra.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            GuardarPositionListview(i,b);



    return view1;
}
public void GuardarPositionListview(int position, boolean b){
    SparseBooleanArray sparseBooleanArray = new SparseBooleanArray(0);
    sparseBooleanArray.put(position,b); //aqui tendria que guardar a mi parecer

    Log.i("BooleanArrayPosition:"," "+sparseBooleanArray.size());


}

The result of the Log when marking three squares in a row is:

12-21 22:05:00.313 3683-3683/? I/BooleanArrayPosition::  1
12-21 22:05:10.298 3683-3683/? I/BooleanArrayPosition::  1
12-21 22:05:11.732 3683-3683/? I/BooleanArrayPosition::  1
    
asked by JoelRomero 22.12.2017 в 03:10
source

1 answer

1

You are initializing the instance of SparseBooleanArray whenever the method GuardarPosicionListView is executed. Try to declare it as a field in the class so that all the elements you add are added to the same instance:

// iniciamos una instancia por de la clase
private SparseBooleanArray sparseBooleanArray = new SparseBooleanArray(0);

public void GuardarPositionListview(int position, boolean b){

    // le agregamos el elemento por cada vez ue se ejecuta el metodo
    sparseBooleanArray.put(position,b); 

    Log.i("BooleanArrayPosition:"," "+sparseBooleanArray.size()); // aqui incrementara siempre que se ejecuta el metodo
}
    
answered by 22.12.2017 / 14:45
source