setOnItemClickListener在AutoCompleteTextView中崩溃

时间:2016-08-04 06:53:20

标签: android autocompletetextview simplecursoradapter

我有一个Android应用程序。在这个应用程序中,我使用自定义SimpleCursorAdapter的AutoCompleteTextView。在AutoCompleteTextView中,addTextChangedListener的onTextChanged方法,

我在代码

下面使用
SearchFeedResultsAdaptorCustomer mSearchViewAdapter = new SearchFeedResultsAdaptorCustomer(this.getActivity(), R.layout.searchview_customer, null,
            columns, null, -1000);

textView = (AutoCompleteTextView) mRoot.findViewById(R.id.search_box);
    textView.setAdapter(mSearchViewAdapter);
    textView.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            try {
                loadData(s.toString());
            } catch (JSONException e) {
                e.printStackTrace();
            }
            textView.setAdapter(mSearchViewAdapter);

        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });

loadData方法是:

private void loadData(String searchText) throws JSONException {
    ArrayList<JSONObject> filterCustomers = new ArrayList<>();
    if (searchText == null || searchText == "") {
        filterCustomers = prgmNameList;
    } else {
        int i = 0;
        for (JSONObject customer : prgmNameList) {
            if (customer.getString("fullName").toUpperCase().contains(searchText.toUpperCase())) {
                filterCustomers.add(customer);
            }
        }
    }
    Log.e("searchhh", "load data" + filterCustomers.size());
    MatrixCursor matrixCursor = convertToCursor(filterCustomers);
    mSearchViewAdapter.changeCursor(matrixCursor);
}

private MatrixCursor convertToCursor(ArrayList<JSONObject> customerList) throws JSONException {
    MatrixCursor cursor = new MatrixCursor(columns);
    int i = 0;
    for (JSONObject customer : customerList) {
        String[] temp = new String[8];
        i = i + 1;
        temp[0] = "" + i;
        temp[1] = String.valueOf(customer.getString("accountNumber"));
        temp[2] = String.valueOf(customer.getString("address"));
        temp[3] = String.valueOf(customer.getString("email"));
        temp[4] = String.valueOf(customer.getString("fullName"));
        temp[5] = String.valueOf(customer.getString("mobile"));
        float point;
        if (customer.getString("earn_point") == null) {
            point = 0;
        } else {
            point = Float.parseFloat(customer.getString("earn_point").toString());
        }
        temp[6] = String.valueOf(point);
        temp[7] = String.valueOf(customer.getString("uid"));
        cursor.addRow(temp);
    }
    return cursor;
}

它将数据显示为下拉列表,但是当我点击列表项时,我收到了以下错误。

以下是我的点击项方法:

textView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            Log.e("Position", "pos:"+position);
            Cursor cursor = (Cursor) parent.getItemAtPosition(position);
            //or
            //Cursor cursor = (Cursor) parent.getItem(position);
            //MatrixCursor matrix = (MatrixCursor)parent.getItemAtPosition(position);
            cursor.moveToPosition(position);
            Log.e("Position", "pos:"+cursor.getCount());
            String feedName = cursor.getString(1);
            searchView.setQuery("", false);
            searchView.clearFocus();
            name.setText(cursor.getString(4));
            mobile.setText(cursor.getString(5));
            email.setText(cursor.getString(3));
            point.setText(cursor.getString(6));
            account.setText(cursor.getString(7));
            usepontHidden.setText(String.valueOf(0));
        }
    });

以下是错误消息:

08-04 12:19:18.692 2619-2619/? E/AndroidRuntime: FATAL EXCEPTION: main
android.database.CursorIndexOutOfBoundsException: After last row.
at android.database.MatrixCursor.get(MatrixCursor.java:73)
at android.database.MatrixCursor.getString(MatrixCursor.java:229)
at com.nybsys.tillbox.sales.FragmentTwo$2.onItemClick(FragmentTwo.java:234)
at android.widget.AutoCompleteTextView.performCompletion(AutoCompleteTextView.java:902)
at android.widget.AutoCompleteTextView.access$500(AutoCompleteTextView.java:91)
at android.widget.AutoCompleteTextView$DropDownItemClickListener.onItemClick(AutoCompleteTextView.java:1192)
at android.widget.AdapterView.performItemClick(AdapterView.java:298)
at android.widget.AbsListView.performItemClick(AbsListView.java:1128)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:2812)
at android.widget.AbsListView$1.run(AbsListView.java:3571)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:153)
at android.app.ActivityThread.main(ActivityThread.java:5299)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)

0 个答案:

没有答案