Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8559505
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:03:54+00:00 2026-06-11T16:03:54+00:00

I am having an issue implementing CursorAdapter on an AutoCompleteTextView. ZipCode : _ (<–EditText)

  • 0

I am having an issue implementing CursorAdapter on an AutoCompleteTextView.

ZipCode : _ (<–EditText)
City : ____ (<– AutoCompleteTextView)

Basically, I want to help the user suggesting available cities for the Zip Code entered.
My issue is that the suggestions are not shown (Cursor does not launch query i guess). What i don’t understand is why it’s working in some cases and not in other ones. I attach the faulty case below.

My Cursor Adapter :

public class SearchCursorAdapter extends CursorAdapter {

    private DataBaseHelper mDbHelper;
    private String codePostal;

    public SearchCursorAdapter(DataBaseHelper dbHelper, Context context,
            String codePostal) {
        // Call the CursorAdapter constructor with a null Cursor.
        super(context, null, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
        mDbHelper = dbHelper;
        this.codePostal = codePostal;
    }

    @Override
    public Cursor runQueryOnBackgroundThread(CharSequence constraint) {
        if (getFilterQueryProvider() != null) {
            return getFilterQueryProvider().runQuery(constraint);
        }

        Cursor cursor = mDbHelper.getStationCursor(constraint.toString(),
                codePostal);
        return cursor;
    }

    @Override
    public String convertToString(Cursor cursor) {
        return cursor.getString(1); //second column in select
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        ((TextView) view).setText(cursor.getString(1)); //second column in select
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.spinner_layout, null);
        return view;
    }
}

The select method from db adapter :

public Cursor getStationCursor(String args, String arg2) {



StringBuffer sqlQuery = new StringBuffer("");
    Cursor result = null;

    sqlQuery.append(" SELECT min(_id) as _id, ");
    sqlQuery.append(CITIES.CITY);
    sqlQuery.append(" FROM ");
    sqlQuery.append(CITIES.TABLE_NAME);
    sqlQuery.append(" WHERE ");
    sqlQuery.append(CITIES.CITY);
    sqlQuery.append(" LIKE '");
    sqlQuery.append(args);
    sqlQuery.append("%' ");
    sqlQuery.append("AND ");
    sqlQuery.append(CITIES.CODE_POSTAL);
    sqlQuery.append(" LIKE '");
    sqlQuery.append(arg2);
    sqlQuery.append("%' ");
    sqlQuery.append(" GROUP BY ");
    sqlQuery.append(CITIES.CITY);
    sqlQuery.append(" ORDER BY ");
    sqlQuery.append(CITIES.CITY);
    sqlQuery.append(" LIMIT 10 ");

    if (myDataBase != null) {
        result = myDataBase.rawQuery(sqlQuery.toString(), null);
    }
    if (result != null) {
        result.moveToFirst();
    }
    return result;
}

The code in my activity :

EditText etCodPost;
AutoCompleteTextView acCity;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout....);
    etCodPost = (EditText) ...;
    acCom = (AutoCompleteTextView) ...;
    setComAdapter(activity);
    etCodPost.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                    int count) {
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                setComAdapter(activity);
            }
        });
}
private void setComAdapter(Activity activity) {
        SearchCursorAdapter adapt = new SearchCursorAdapter(myDbHelper, activity,
                 etCodPost.getText().toString());

        acCity.setAdapter(adapt);
        acCity.setThreshold(3);
    }

Thanks for your replies and sorry for the long post. Any hint would be very appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T16:03:56+00:00Added an answer on June 11, 2026 at 4:03 pm

    You don’t mention at least one of the cases where the filtering fails so the lines below come more from guessing:

    I think you setup the filtering wrong. There is no need to set the adapter each time when the user enters a code, a simpler solution would be to have a field in the activity class, an int(or String from your code) which would be used as part of the query.

    @Override
    public void afterTextChanged(Editable s) {
         mCode = s.toString;
    }
    

    Next, filtering at the adapter level could be improved like this:

    @Override
    public Cursor runQueryOnBackgroundThread(CharSequence constraint) {
        if (constraint == null || constraint.toString.equals("")) {
            // this is the contract of this method and should be respected 
            return mDbHelper.getAllCityRecords(); // get all records
        }
        return mDbHelper.getStationCursor(constraint.toString(),
                mCode); // mCode is the field that is updated in the activity class(you should take in consideration that the user could use directly the AutoCompleteTextView, so mCode could be not set at this level)
        return cursor;
    }
    

    Last, you probably did this, but do make sure that you get what you expect when querying(directly) the database. As the filtering works for some cases it may be something to look after.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having an issue implementing an excel feature I'm after, this is my data
I am having an issue implementing Sifr3 on this particular site and it's driving
I'm having an issue with implementing a LIKE Button for my website. I'm trying
Ok, so i'm having an issue implementing a basic Twitter bootstrap Carousel via ExpressionEngine's
I'm having a small issue with implementing custom basic authentication for a asmx in
I'm having trouble implementing render to texture with OpenGL 3. My issue is that
Having issue with slider navigation. Works perfectly fine when there are no other unordered
I having issue that content assistant / intellisense is working in methods such as
Hi guys I am having issue I have this query: SELECT * FROM useraccount
i am having issue with loading my selector via a jquery ajax load call

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.