Android Studio, problem with matrix [N] x [N]

1

I'm sorry to bother with the query, I have 1 data matrix, and a vector where I save the images. I fill the matrix myself entering the data, and I can not understand why the code does not work because I assign it a different place than the one shown below:

 java.lang.ArrayIndexOutOfBoundsException: length=2; index=2

Next I explain the code:

Adapter Class

public class Adaptador extends BaseAdapter {

private static LayoutInflater inflater= null;

Context contexto;
String [][] datos;
int [] datosImg;

public Adaptador(Context contexto, String[][] datos, int[] datosImg) {


    this.contexto = contexto;
    this.datos = datos;
    this.datosImg = datosImg;

    inflater = (LayoutInflater)contexto.getSystemService(contexto.LAYOUT_INFLATER_SERVICE);
}



@Override
public View getView(int i, View convertView, ViewGroup parent) {
    final View vista= inflater.inflate(R.layout.layout_listitem, null);

    TextView titulo = (TextView) vista.findViewById(R.id.tvTitulo);
    TextView descripcion = (TextView) vista.findViewById(R.id.tvDescripcion);
    ImageView imagen= (ImageView) vista.findViewById(R.id.ivImagen);

    titulo.setText(datos[i][0]);
    descripcion.setText(datos[i][1]);
    imagen.setImageResource(datosImg[i]);

    return vista;


}

@Override
public int getCount() {
    return datosImg.length;
}

@Override
public Object getItem(int position) {
    return null;
}

@Override
public long getItemId(int position) {
    return 0;
}

Food activity

public class alimentos extends AppCompatActivity {
ListView lista;

String[] [] datos = {
        {"Manzana con helado de banana","Tarta de Manzanas", "Revuelto light de zapallitos y fideos","Albondigas light con semillas de Sesamo y avena", "Sorrentinos light con poquitos ingredientes"},
        {"Manzanas o peras, ralladura de limon, canela, edulcorante, agua, bananas" , "Manzanas peladas, huevos, aceite, leche, stevia o edulcorante liquido", "Zapallitos, cebolla mediana, aceite, fideos cocidos, ajo, perejil" ,"Carne picada especial, avena, huevo grande, semillas de sesamo, maizena, sal","Masa de empanadas, queso cremoso, albahaca"}
};

int [] datosImg= {R.drawable.comida1, R.drawable.comida2, R.drawable.comida3, R.drawable.comida4, R.drawable.comida5};

@Override
protected void onCreate(Bundle savedInstanceState) {

    AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_alimentos);
lista = (ListView) findViewById(R.id.lvLista);

lista.setAdapter(new Adaptador(this,datos,datosImg));
}


}

logcat

12-06 11:55:04.024 29051-29051/com.example.juan.diabetapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.juan.diabetapp, PID: 29051
java.lang.ArrayIndexOutOfBoundsException: length=2; index=2
    at com.example.juan.diabetapp.Adaptador.getView(Adaptador.java:39)
    at android.widget.AbsListView.obtainView(AbsListView.java:2474)
    at android.widget.ListView.measureHeightOfChildren(ListView.java:1301)
    at android.widget.ListView.onMeasure(ListView.java:1203)
    at android.view.View.measure(View.java:19176)
    at android.support.constraint.ConstraintLayout.internalMeasureChildren(ConstraintLayout.java:1227)
    at android.support.constraint.ConstraintLayout.onMeasure(ConstraintLayout.java:1572)
    at android.view.View.measure(View.java:19176)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6116)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:223)
    at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:143)
    at android.view.View.measure(View.java:19176)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6116)
    at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:401)
    at android.view.View.measure(View.java:19176)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6116)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:223)
    at android.view.View.measure(View.java:19176)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6116)
    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1723)
    at android.widget.LinearLayout.measureVertical(LinearLayout.java:788)
    at android.widget.LinearLayout.onMeasure(LinearLayout.java:648)
    at android.view.View.measure(View.java:19176)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6116)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:223)
    at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2695)
    at android.view.View.measure(View.java:19176)
    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2496)
    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1455)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1709)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1328)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6730)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:894)
    at android.view.Choreographer.doCallbacks(Choreographer.java:696)
    at android.view.Choreographer.doFrame(Choreographer.java:631)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:880)
    at android.os.Handler.handleCallback(Handler.java:815)
    at android.os.Handler.dispatchMessage(Handler.java:104)
    at android.os.Looper.loop(Looper.java:207)
    at android.app.ActivityThread.main(ActivityThread.java:5765)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:826)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:716)
    
asked by Juan Iriarte 06.12.2018 в 16:08
source

3 answers

2

The problem is specified here:

  

java.lang.ArrayIndexOutOfBoundsException: length = 2; index = 2

indicates that you are trying to get an element of your array located in the element with index 2 ( index=2 ) that does not really exist.

Remember that starts indexing from 0, therefore the indices would be 0,1,2 (in total 3 elements) but in reality in your array you contain only 2 elements ( length=2 )

Specifically, the error occurs in getView() of your Adapter , the solution is to specify datos.length with number of items to display in your adapter.

Remember that the getCount() method specifies the amount of items to display to your adapter, in this case it should be

datos.length and not datosImg.length

@Override
public View getView(int i, View convertView, ViewGroup parent) {
    final View vista= inflater.inflate(R.layout.layout_listitem, null);

    TextView titulo = (TextView) vista.findViewById(R.id.tvTitulo);
    TextView descripcion = (TextView) vista.findViewById(R.id.tvDescripcion);
    ImageView imagen= (ImageView) vista.findViewById(R.id.ivImagen);

    titulo.setText(datos[i][0]);
    descripcion.setText(datos[i][1]);
    imagen.setImageResource(datosImg[i]);

    return vista;


}

@Override
public int getCount() {
    //return datosImg.length;
     return datos.length; 
}
    
answered by 06.12.2018 в 16:46
0

You are getting the wrong data in the Adapter change this:

titulo.setText(datos[i][0]);
descripcion.setText(datos[i][1]);
imagen.setImageResource(datosImg[i]);

for this:

titulo.setText(datos[0][i]);
descripcion.setText(datos[1][i]);
imagen.setImageResource(datosImg[i]);
    
answered by 06.12.2018 в 17:14
0

getCount() must return the number of items in RecyclerView and must match the number of items in the array of datos .

The data array has two data (each one an array of string).

But instead of returning the amount 2 that is the amount of data that you have, you are returning the amount of images that is greater.

When getView() tries to find the third data (index 2) it pulls the out of bounds exception because datos does not have 3 elements.

Should return datos.length

@Override
public int getCount() {
    return datos.length;
}
    
answered by 06.12.2018 в 17:29