How to use ArrayList get() method in Android Studio

Tags: ,



I made a code using an ArrayList (short[]) in Android Studio but when I use the get() method it always return the last data stored and I don’t know why. I’m using two buttons, one execute adquirir_audio() just to store some values. The second one prints in the console the stored data using a for loop, but it always return the same values.

Here is the code:

public class MainActivity extends AppCompatActivity {

private static String TAG = "AudioClient";

//Instancia de elementos de la interfaz de usuario.
Button btniniciar;
Button btnfiltrar;
TextView  lblfiltro;
RadioGroup radioGroup;
RadioButton radioButton;

boolean grabar = false;

private static int BUFFER_SIZE2 =16;

public static short[] buffer_debug    = new short[BUFFER_SIZE2];

public List<short[]> datos_short = new ArrayList<>();


private Thread myThread;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //Identificando componentes de la IU.
    radioGroup = (RadioGroup) findViewById(R.id.RadioGroup);
    btniniciar = (Button) findViewById(R.id.btnIniciar);
    btnfiltrar = (Button) findViewById(R.id.btnFiltrar);
    lblfiltro = (TextView) findViewById(R.id.lblFiltro);

    lblfiltro.setText("Filtro: -- n Orden: -- n Fstop: --");

    btniniciar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            grabar = !grabar;
            btniniciar.setText(grabar == true? "parar" : "iniciar");
            if (grabar){
                adquirir_audio();

            }
            else{

            }
        }
    });

    btnfiltrar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            for(int i = 0; i < datos_short.size(); i++){
                Log.d(TAG, "Buffer guardado: "+i + Arrays.toString(datos_short.get(i)));
            }
        }
    });

}

public void adquirir_audio(){

    int dir = 0;
    int dir2 = 0;

    while (dir < 5) {

    buffer_debug[0] = (short) (dir2 * 16);
    buffer_debug[1] = (short) (dir2 * 16 + 1);
    buffer_debug[2] = (short) (dir2 * 16 + 2);
    buffer_debug[3] = (short) (dir2 * 16 + 3);
    buffer_debug[4] = (short) (dir2 * 16 + 4);
    buffer_debug[5] = (short) (dir2 * 16 + 5);
    buffer_debug[6] = (short) (dir2 * 16 + 6);
    buffer_debug[7] = (short) (dir2 * 16 + 7);
    buffer_debug[8] = (short) (dir2 * 16 + 8);
    buffer_debug[9] = (short) (dir2 * 16 + 9);
    buffer_debug[10] = (short) (dir2 * 16 + 10);
    buffer_debug[11] = (short) (dir2 * 16 + 11);
    buffer_debug[12] = (short) (dir2 * 16 + 12);
    buffer_debug[13] = (short) (dir2 * 16 + 13);
    buffer_debug[14] = (short) (dir2 * 16 + 14);
    buffer_debug[15] = (short) (dir2 * 16 + 15);

    Log.d(TAG, "Buffer: " + dir2 + Arrays.toString(buffer_debug));

    datos_short.add(dir,buffer_debug);

    dir = dir + 1;
    dir2 = (dir2 + 1);}}}

And here is the console

enter image description here Hope you can help me.

Answer

In java the Arrays are not a primitive type and you are always changing the same buffer_debug object in all elements of the ArrayList.

You can use something like:

public void adquirir_audio(){

        short[] buffer_debug;
      
        int dir = 0;
        int dir2 = 0;

        while (dir < 5) {

            //Initialize a new Array
            buffer_debug = new short[16];  //<---

            buffer_debug[0] = (short) (dir2 * 16);
            buffer_debug[1] = (short) (dir2 * 16 + 1);
            //...

            datos_short.add(dir,buffer_debug);
            //...
       
       }
}


Source: stackoverflow