FernandoPaiva
FernandoPaiva

Reputation: 4462

Spinner with Select One throws IndexOutOfBoundsException?

I'm trying insert in my BaseAdapter of a Spinner the text: "Select one option". The problem is after insert this text my spinner stop works and throws an exception IndexOutOfBoundsException.

How could I solve it ?

Spinner

Spinner spin = (Spinner)findViewById(R.id.mySpinner);
ConsultaClienteVendaProdutoListAdapter adapter = new ConsultaClienteVendaProdutoListAdapter(this, lista);
spin.setAdapter(adapter);

BaseAdapter

public class ConsultaClienteVendaProdutoListAdapter extends BaseAdapter{
private List<Cliente> list;
private Context context;


public ConsultaClienteVendaProdutoListAdapter(List<Cliente> list, Context context) {
    this.list = list;
    this.context = context;
}

public void changeList(List<Cliente> list){
    this.list = list;
    notifyDataSetChanged();
}

@Override
public int getCount() {
    return list.size();
}

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

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder viewHolder;
    Cliente cli = null;
    list.add(0, cli);

    if (convertView == null) {

        viewHolder = new ViewHolder();
        LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.consulta_cliente_venda_produto_adapter, parent, false);

        viewHolder.llClienteAdapter = (LinearLayout) convertView.findViewById(R.id.llClienteAdapter);
        viewHolder.tvNome = (TextView) convertView.findViewById(R.id.tvNome);


        convertView.setTag(viewHolder);
    }else{
        viewHolder = (ViewHolder)convertView.getTag();
    }

    Cliente cliente = list.get(position -1);

    if(position == 0){
        viewHolder.tvNome.setText("Select one option");
    }else{
        viewHolder.tvNome.setText(cliente.getNome());
    }

    return convertView;
}


/** pattern view holder */
private static class ViewHolder{
    LinearLayout llClienteAdapter;
    TextView tvNome;
}

}

Exception

java.lang.IndexOutOfBoundsException: Invalid index 2, size is 2
            at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
            at java.util.ArrayList.get(ArrayList.java:308)
            at br.com.williarts.kontrole.listadap.ConsultaClienteVendaProdutoListAdapter.getView(ConsultaClienteVendaProdutoListAdapter.java:53)
            at android.widget.BaseAdapter.getDropDownView(BaseAdapter.java:71)
            at android.widget.Spinner$DropDownAdapter.getDropDownView(Spinner.java:886)
            at android.widget.Spinner$DropDownAdapter.getView(Spinner.java:882)
            at android.widget.Spinner.measureContentWidth(Spinner.java:762)
            at android.widget.Spinner$DropdownPopup.computeContentWidth(Spinner.java:1123)
            at android.widget.Spinner$DropdownPopup.show(Spinner.java:1149)
            at android.widget.Spinner.performClick(Spinner.java:675)
            at android.view.View$PerformClick.run(View.java:19330)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:157)
            at android.app.ActivityThread.main(ActivityThread.java:5356)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
            at dalvik.system.NativeStart.main(Native Method)

Upvotes: 0

Views: 1522

Answers (2)

Catalina
Catalina

Reputation: 2118

It because on the method getCount() you should have list.size() only. Because you have list.size() +1 it makes the adapter believe it has one more element. And when it requests this it doesn't exist.

@Override public int getCount() { return list.size() +1; }

The error most likely occurs at line Cliente cliente = list.get(position).

If you want to add the string "Select one option" in the spinner you could just add it in the list that you are using as your data source or replace Cliente cliente = list.get(position) with just Cliente cliente=null;

Upvotes: 1

Kevin Robatel
Kevin Robatel

Reputation: 8386

Your getSize() return list.size() +1, so getView() is called for all number between 0 and list.size() +1 - 1.

And the second line of getView() make a get on list.
If your list has 1 element,

  • getSize() return 2
  • getView() make a Cliente cliente = list.get(1);

it doesn't exist in the list! (only [...].get(0) exist).

You have to change to:

@Override
public int getCount() {
    return list.size();
}

and change in getView():

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder viewHolder;
    Cliente cliente = list.get(position);
    if (convertView == null) {
[...]

Upvotes: 1

Related Questions