Invalid index 0, size is 0 (Android)

2

Hi I try to fill an Arraylist in a Fragment with data from a Sqlite table (the table already has data) and it shows me the error of the title in the catch.

Class HomeFragment

public class InicioFragment extends Fragment {
    public RecyclerView recyclerView;
    public RecyclerView.Adapter adapter;
    public RecyclerView.LayoutManager layoutManager;
    public SearchView searchView;
    public Context context;
    public ArrayList<InicioItem> arrayList;
    UtilidadesSQLite inicio;
    public List<UtilidadesSQLite > listCreditos;
    private conexion con;
    SQLiteDatabase bd;
    String id;

    public InicioFragment() { }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_inicio, container, false);


        id=getArguments().getString("id");
        inicio= new UtilidadesSQLite ();
        recyclerView = (RecyclerView) rootView.findViewById(R.id.rclvInicio);
        recyclerView.setHasFixedSize(true);
        searchView = (SearchView) rootView.findViewById(R.id.searchInicio);
        this.context = this.getContext();
        arrayList = new ArrayList<InicioItem>();

        listCreditos=new ArrayList<Inicio>();
        arrayList=GetArrayListInicioItem();
        adapter = new InicioRecyclerAdapter(context, arrayList);
        recyclerView.setAdapter(adapter);

        layoutManager = new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(layoutManager);
        return rootView;
    }

    public ArrayList<InicioItem> GetArrayListInicioItem(){
        con = new conexion(getActivity(), "bd_C", null, 1);
        bd=con.getReadableDatabase();
        try {
            String query="Select * from " + Inicio.TInicio +" where Id= '"+id+"'";

            Cursor cursor = bd.rawQuery(query, null);
            if (cursor != null) {
                if (cursor.moveToFirst() == true) {
                    do {
                        for (int i = 0; i < cursor.getCount(); i++) {
                            inicio = listCreditos.get(i);
                            arrayList.add(new InicioItem(inicio));
                        }
                    } while (cursor.moveToNext());
                }
            }
            cursor.close();
        }
        catch (Exception e) {
            e.toString();//Error aqui
        }
        bd.close();
        return arrayList;
    }

StartItem Class

public class InicioItem {

    public InicioItem(UtilidadesSQLite inicio) {
        this._inicio = inicio;
    }

    private UtilidadesSQLite _inicio;

    public UtilidadesSQLite inicio(){
        return _inicio;
    }

    public int imgBackground(){
        int imgbackground = R.drawable.background_item_red;
        if(_inicio != null){
            switch (Integer.parseInt(_inicio.Estatus)){
            case 0: 
                imgbackground =  R.drawable.background_item_red;
                break;
            case 1: 
                imgbackground =  R.drawable.background_item_teal;
                break;
            case 2: 
                imgbackground =  R.drawable.background_item_blue;
                break;
            }
        }
        return imgbackground;
    }

    public int img(){
        int img = R.drawable.ic_item_no_asignado;
        try{  
            if(_inicio != null){
                switch (Integer.parseInt(_inicio.Estatus)){
                case 0: 
                    img =  R.drawable.ic_item_no_asignado;
                    break;
                case 1: 
                    img =  R.drawable.ic_item_activo;
                    break;
                case 2: 
                    img =  R.drawable.ic_item_convenio_promesa_pago;
                    break;
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return img;
    }

    public String nombre(){
        if(_inicio != null)
        return _inicio.Nombre.toUpperCase();
        return "No hay registro";
    }
}

Thank you!

    
asked by Geek 04.09.2018 в 02:46
source

1 answer

0

If you check your code, you are creating a ArrayList

listCreditos=new ArrayList<Inicio>();

but later you are trying to get an element of this ArrayList, obviously it does not contain values for that reason the error:

 inicio = listCreditos.get(i);

Actually I see a bad design in your application, you must first get the UtilitiesSQLite objects (start) to add them to listCreditos , for now you can comment on this line:

                    for (int i = 0; i < cursor.getCount(); i++) {
                        //inicio = listCreditos.get(i);
                        arrayList.add(new InicioItem(inicio));
                    }
    
answered by 04.09.2018 / 20:19
source