0

I have an android application. In this app I used a AutoCompleteTextView with custom SimpleCursorAdapter. In AutoCompleteTextView addTextChangedListener's onTextChanged method,

I am using below Code

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) {

        }
    });

And the loadData method is:

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

It shows data as list of drop down but when I clicked a list item, I got following error.

Here is my click item method:

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

Here is the error message:

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)
Android Surya
  • 544
  • 3
  • 17
Mohammad Rajob
  • 743
  • 1
  • 11
  • 29
  • you dont need any `TextWatcher`, see [this](http://stackoverflow.com/a/19860624/2252830) – pskink Aug 04 '16 at 07:02
  • There is something strange going on in your onItemClick method. You are calling parentGetItemAtPosition and expecting a Cursor? But that does not explain the exception. Are you sure the cursor is filled with values? – GPuschka Aug 04 '16 at 07:07
  • @GPuschka In onItemClick, cursor.getCount() returns 0, but why? – Mohammad Rajob Aug 04 '16 at 07:11
  • Nevermind the getItemAtPosition being a cursor. I had the wrong asumption there. What is the log output for 'Log.e("searchhh", "load data" + filterCustomers.size());' – GPuschka Aug 04 '16 at 07:14
  • and inside `onItemClick` just do: `CursorAdapter ca = (CursorAdapter) parent.getAdapter(); Cursor c = ca.getCursor(); DatabaseUtils.dumpCurrentRow(c); // call any c.get*() method ` – pskink Aug 04 '16 at 07:24
  • @pskink error in line dumpCurrentRow, android.database.CursorIndexOutOfBoundsException: Before first row. – Mohammad Rajob Aug 04 '16 at 07:37
  • I am working on another issue, I will tell you later after try. Thanks for your answer. – Mohammad Rajob Aug 05 '16 at 03:42

0 Answers0