Select One的Spinner抛出IndexOutOfBoundsException?

时间:2015-03-15 14:08:14

标签: android android-spinner

我尝试在Spinner的BaseAdapter中插入文本:"选择一个选项"。问题是在插入此文本后,我的微调器停止工作并抛出异常IndexOutOfBoundsException

我怎么解决?

微调

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;
}

}

异常

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)

2 个答案:

答案 0 :(得分:1)

因为在方法getCount()上你应该只有list.size()。因为你有list.size()+1,它使适配器相信它还有一个元素。当它要求它时它就不存在了。

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

错误最有可能发生在Cliente cliente = list.get(position)。

如果你想在微调器中添加字符串“Select one option”,你可以将它添加到你用作数据源的列表中,或用Cliente cliente =替换Cliente cliente = list.get(position)空;

答案 1 :(得分:1)

您的getSize()返回list.size() +1,因此{0}与getView()之间的所有数字都会调用list.size() +1 -

getView()的第二行开始list 如果您的list有1个元素,

  • getSize()返回2
  • getView()制作Cliente cliente = list.get(1);

它在列表中不存在! (仅存在[...].get(0))。

您必须更改为:

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

并更改getView()

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