0

I'd like to get elements added in a GridView with a BaseAdapter.

Here is my code :

GridView grid_ordi = (GridView)findViewById(R.id.grid_ordi);
CaseAdapter casesOrdi = new CaseAdapter(this, newWidth);
grid_ordi.setAdapter(casesOrdi);
System.out.println(casesOrdi.getItem(x));

And my class CaseAdapter :

import java.util.Vector;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

public class CaseAdapter extends BaseAdapter {
private Context mContext;
private int largeur;
private Vector<Case> lesCases;

public CaseAdapter(Context c, int w) 
{
    this.lesCases = new Vector<Case>();
    mContext = c;
    largeur = w;
}

public View getView(int position, View convertView, ViewGroup parent) 
{
    Case c;
    boolean AJOUTER_ECOUTEUR = true;

    if (convertView == null)
    {
        int i = position/11;
        int j = position%11;

        if(i == 0 && j == 0) // Case haut gauche => Vide
            c = new Case(mContext, !AJOUTER_ECOUTEUR, "");
        else if(i == 0 && j > 0) // Première ligne sans case haut gauche
            c = new Case (mContext, !AJOUTER_ECOUTEUR, Integer.toString(j-1));
        else if(j == 0 && i > 0) // Première colonne
            c = new Case(mContext, !AJOUTER_ECOUTEUR, Character.toString((char)(65+(i-1))));
        else // Toutes les autres cases
            c = new Case(mContext, AJOUTER_ECOUTEUR, "");

        c.setWidth(largeur/11);
        c.setHeight(largeur/11);

        this.lesCases.add(c);
        System.out.println(this.lesCases.size());
    }
    else
        c = (Case) convertView;

    return c;
}

public int getCount() 
{
    return 121;
}

public Case getItem(int position) 
{
    return this.lesCases.get(position);
}

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

When I do :

System.out.println(casesOrdi.getItem(x));

The application stops and "System.out.println(this.lesCases.size());" isn't executed. So I can't get my Object.

How to solve the problem ?

PS : Sorry for variables names and comments, I'm French, but it's not important for solving the problem :)

EDIT : Here is the LogCat stack trace : (I tested for x = 32)

02-10 19:02:17.068: W/dalvikvm(25525): threadid=1: thread exiting with uncaught exception (group=0x41cb4930)
02-10 19:02:17.078: E/AndroidRuntime(25525): FATAL EXCEPTION: main
02-10 19:02:17.078: E/AndroidRuntime(25525): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test2/com.example.test2.Jeu}: java.lang.ArrayIndexOutOfBoundsException: length=0; index=32
02-10 19:02:17.078: E/AndroidRuntime(25525):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
02-10 19:02:17.078: E/AndroidRuntime(25525):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
02-10 19:02:17.078: E/AndroidRuntime(25525):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
02-10 19:02:17.078: E/AndroidRuntime(25525):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
02-10 19:02:17.078: E/AndroidRuntime(25525):    at android.os.Handler.dispatchMessage(Handler.java:99)
02-10 19:02:17.078: E/AndroidRuntime(25525):    at android.os.Looper.loop(Looper.java:137)
02-10 19:02:17.078: E/AndroidRuntime(25525):    at android.app.ActivityThread.main(ActivityThread.java:5039)
02-10 19:02:17.078: E/AndroidRuntime(25525):    at java.lang.reflect.Method.invokeNative(Native Method)
02-10 19:02:17.078: E/AndroidRuntime(25525):    at java.lang.reflect.Method.invoke(Method.java:511)
02-10 19:02:17.078: E/AndroidRuntime(25525):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-10 19:02:17.078: E/AndroidRuntime(25525):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-10 19:02:17.078: E/AndroidRuntime(25525):    at dalvik.system.NativeStart.main(Native Method)
02-10 19:02:17.078: E/AndroidRuntime(25525): Caused by: java.lang.ArrayIndexOutOfBoundsException: length=0; index=32
02-10 19:02:17.078: E/AndroidRuntime(25525):    at java.util.Vector.arrayIndexOutOfBoundsException(Vector.java:907)
02-10 19:02:17.078: E/AndroidRuntime(25525):    at java.util.Vector.elementAt(Vector.java:328)
02-10 19:02:17.078: E/AndroidRuntime(25525):    at java.util.Vector.get(Vector.java:442)
02-10 19:02:17.078: E/AndroidRuntime(25525):    at com.example.test2.CaseAdapter.getItem(CaseAdapter.java:60)
02-10 19:02:17.078: E/AndroidRuntime(25525):    at com.example.test2.Jeu.onCreate(Jeu.java:42)
02-10 19:02:17.078: E/AndroidRuntime(25525):    at android.app.Activity.performCreate(Activity.java:5104)
02-10 19:02:17.078: E/AndroidRuntime(25525):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
02-10 19:02:17.078: E/AndroidRuntime(25525):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
02-10 19:02:17.078: E/AndroidRuntime(25525):    ... 11 more
4

0 回答 0