onItemClickListener崩溃了

时间:2012-09-20 15:54:30

标签: android listview android-arrayadapter

  

可能重复:
  ArrayAdapter custom with AutocompleteTextView and onclicklistener

我正在努力了解我的Android应用程序中发生了什么,它使用了一个很好的自定义自动完成类。 特别是我发出一个休息请求,获取数据,填充一个ArrayList(我需要更多的信息),使用过滤器并显示一个建议列表(根据服务器响应是正确的)。 但是,当我点击项目时,我的应用程序崩溃了。 建议??

这是我的适配器类:

package xxx;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;

public class AutoCompleteCustomAdapter extends ArrayAdapter<Intervento>  implements Filterable {

    static class ViewHolder{
         private TextView  textName;
    }

    private ArrayList<Intervento> arrayListIntervento;
    private int              layout;


    public ArrayList<Intervento> getArrayListIntervento() {
        return arrayListIntervento;
    }

    public void setArrayListIntervento(ArrayList<Intervento> arrayListIntervento) {
        this.arrayListIntervento = arrayListIntervento;
    }

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

    @Override
    public Intervento getItem(int index) {
        return arrayListIntervento.get(index);
    }


     @Override
        public Filter getFilter() {
         Filter myFilter = new Filter() {
                @SuppressWarnings("unchecked")
                @Override


                protected void publishResults(CharSequence contraint, FilterResults results) {
                    if(results != null && results.count > 0) {
                        arrayListIntervento = (ArrayList<Intervento>)results.values;
                        notifyDataSetChanged();
                    }
                    else {
                        notifyDataSetInvalidated();
                    }
                }

                @Override
                protected FilterResults performFiltering(CharSequence arg0) {
                    FilterResults filterResults = new FilterResults();
                    if(arg0 != null) {
                        try {
                            arrayListIntervento = new InterventoController().execute("http://192.168.1.162:8080/.../InterventoHasSinonimoName/{interventohassinonimo_interventoNomeIntervento}/1/interventos", arg0.toString()).get();
                        }
                        catch(Exception e) {
                            e.printStackTrace();
                        }
                        // Now assign the values and count to the FilterResults object
                        filterResults.values = arrayListIntervento;
                        filterResults.count = arrayListIntervento.size();
                    }
                    return filterResults;
                }
            };
            return myFilter;
     }




    @Override
    public View getView(int position, View contentView, ViewGroup viewGroup) {

        View       view = null;
        ViewHolder viewHolder = null;

        if(contentView==null){
            LayoutInflater inflater =  (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.suggest_list, viewGroup, false);

            if (view != null) {
                viewHolder = new ViewHolder();
                viewHolder.textName = (TextView) view.findViewById(R.id.nomeIntervento);
                view.setTag(viewHolder);
                Intervento intervento = arrayListIntervento.get(position);
                if(intervento!=null){
                    viewHolder.textName.setText(intervento.getNome());
                }
            }
        }else {
            view = contentView;
            viewHolder = (ViewHolder) contentView.getTag();
        }

        if (viewHolder != null) {
            Intervento intervento = arrayListIntervento.get(position);
            if(intervento!=null){
                viewHolder.textName.setText(intervento.getNome());
            }
        }

        return view;

    }

    public AutoCompleteCustomAdapter(Context context, int textViewResourceId) {
        super(context, textViewResourceId);

        this.arrayListIntervento= new ArrayList<Intervento>();
        this.layout=textViewResourceId;
    }


}

在我的活动中,我打电话给:

autoCompleteTextView.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> listView, View view, int position, long id) {
                Intervento intervento = ((Intervento) listView.getAdapter().getItem(position));
                Integer idIntervento = intervento.getIdintervento();
                //autoCompleteTextView.setText(intervento);
            }
        });

这是logcat:

09-20 15:14:27.885: E/AndroidRuntime(941): FATAL EXCEPTION: main
09-20 15:14:27.885: E/AndroidRuntime(941): java.lang.IndexOutOfBoundsException: Invalid index 1, size is 0
09-20 15:14:27.885: E/AndroidRuntime(941):  at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
09-20 15:14:27.885: E/AndroidRuntime(941):  at java.util.ArrayList.get(ArrayList.java:304)
09-20 15:14:27.885: E/AndroidRuntime(941):  at it.stasbranger.clinigomobile.AutoCompleteCustomAdapter.getItem(AutoCompleteCustomAdapter.java:42)
09-20 15:14:27.885: E/AndroidRuntime(941):  at it.stasbranger.clinigomobile.AutoCompleteCustomAdapter.getItem(AutoCompleteCustomAdapter.java:1)
09-20 15:14:27.885: E/AndroidRuntime(941):  at it.stasbranger.clinigomobile.MainActivity$2.onItemClick(MainActivity.java:90)
09-20 15:14:27.885: E/AndroidRuntime(941):  at android.widget.AutoCompleteTextView.performCompletion(AutoCompleteTextView.java:868)
09-20 15:14:27.885: E/AndroidRuntime(941):  at android.widget.AutoCompleteTextView.access$500(AutoCompleteTextView.java:91)
09-20 15:14:27.885: E/AndroidRuntime(941):  at android.widget.AutoCompleteTextView$DropDownItemClickListener.onItemClick(AutoCompleteTextView.java:1157)
09-20 15:14:27.885: E/AndroidRuntime(941):  at android.widget.AdapterView.performItemClick(AdapterView.java:292)
09-20 15:14:27.885: E/AndroidRuntime(941):  at android.widget.AbsListView.performItemClick(AbsListView.java:1058)
09-20 15:14:27.885: E/AndroidRuntime(941):  at android.widget.AbsListView$PerformClick.run(AbsListView.java:2514)
09-20 15:14:27.885: E/AndroidRuntime(941):  at android.widget.AbsListView$1.run(AbsListView.java:3168)
09-20 15:14:27.885: E/AndroidRuntime(941):  at android.os.Handler.handleCallback(Handler.java:605)
09-20 15:14:27.885: E/AndroidRuntime(941):  at android.os.Handler.dispatchMessage(Handler.java:92)
09-20 15:14:27.885: E/AndroidRuntime(941):  at android.os.Looper.loop(Looper.java:137)
09-20 15:14:27.885: E/AndroidRuntime(941):  at android.app.ActivityThread.main(ActivityThread.java:4424)
09-20 15:14:27.885: E/AndroidRuntime(941):  at java.lang.reflect.Method.invokeNative(Native Method)
09-20 15:14:27.885: E/AndroidRuntime(941):  at java.lang.reflect.Method.invoke(Method.java:511)
09-20 15:14:27.885: E/AndroidRuntime(941):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-20 15:14:27.885: E/AndroidRuntime(941):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-20 15:14:27.885: E/AndroidRuntime(941):  at dalvik.system.NativeStart.main(Native Method)

建议?

2 个答案:

答案 0 :(得分:0)

我的2分钱:

arrayListIntervento似乎是空的:

IndexOutOfBoundsException: Invalid index 1, size is 0

可能没有正确填充。 public void setArrayListIntervent maybye应该克隆如下:

public void setArrayListIntervento(ArrayList<Intervento> arrayListIntervento) {
    this.arrayListIntervento = arrayListIntervento.clone(); 
}

答案 1 :(得分:0)

首先,您必须从类Adapter中删除implements Filterable ...因为获取Filter()方法只是在ArrayAdapter类中实现。 您如何将AutoCompleteCustomAdapter声明为活动?是最终宣言吗?