I see that it is a recurring theme in those who are starting to program like me. But from all the forums I have read I have not been able to draw a conclusion for my particular problem.
I encounter the following problem: I am saving 4 columns in the database: id, title, url and priority When the data is entered, everything works perfectly, everything is saved as it should. But when collecting that data and displaying it, the following happens:
java.lang.RuntimeException: Unable to start activity ComponentInfo{es.ifp.playlistonline/es.ifp.playlistonline.ReproductorActivty}: java.lang.IndexOutOfBoundsException: Index: 2, Size: 2
And I don't know what the hell to do anymore :(
This is my complete activity code:
package es.ifp.playlistonline;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.MediaController;
import android.widget.Spinner;
import android.widget.VideoView;
import java.util.ArrayList;
public class ReproductorActivty extends AppCompatActivity {
// COMPONENTES DE LA PÁGINA
protected VideoView video;
//Base de datos
protected BaseDatos db;
// ADICIONALES
// VerVideo
private ArrayList<String> verVideo = new ArrayList<String>();
private ArrayAdapter<String> adaptadorSpinner;
// VARIABLES
private String titulo = "";
private String url = "";
private int prioridad = -1;
private Bundle extras;
// Controlador de vídeo
private MediaController mc;
// Para pasar de pantalla
private Intent pasarPantalla;
private int id = 0;
private String contenidoItem = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reproductor);
db = new BaseDatos(this);
video = (VideoView) findViewById(R.id.videoView_reproductor);
extras = getIntent().getExtras();
titulo = extras.getString("id");
//https://test-videos.co.uk/vids/bigbuckbunny/mp4/h264/1080/Big_Buck_Bunny_1080_10s_5MB.mp4
verVideo = db.getListaRepro(id);
System.out.println(verVideo.get(0));
System.out.println(verVideo.get(1));
System.out.println(verVideo.get(2)); // AQUÍ ME MARCA EL ERROR, GET() Y GET(1) LO PILLA PERFECTAMENTE
System.out.println(verVideo.get(3));
}
}
And this is the SQLite database part:
public ArrayList<String> getListaRepro(int id) {
Cursor res = null;
String titulo = "";
String url = "";
ArrayList<String> verVideo = new ArrayList<String>();
db = this.getReadableDatabase();
res = db.rawQuery("SELECT * FROM PlayListOnline WHERE id = '"+id+"' ", null);
res.moveToFirst();
if( res != null && res.moveToFirst() ){
titulo = res.getString(res.getColumnIndex("titulo"));
url = res.getString(res.getColumnIndex("url"));
res.close();
}
verVideo.add(titulo);
verVideo.add(url);
return verVideo;
}
Thank you very much for the help :)
Are you sure that the query that is filtering by id returns 4 records?
Since the following error
it means your array only has 2 elements and you are trying to read a third one, so get(0) and get(1) work fine.
I recommend you to see your DB, since you can have 4 records in the DB, but in the query:
You are filtering by id
This line
It can be summarized like this:
Later:
Following, you have a problem with your method, you are telling your query to bring you all the data from all the columns of your DB:
But, then you only pass two:
How do you want to display the other data if you're not passing it?
Another thing, as a personal recommendation, do not use variables like this:
You can do something like: