0

I have example android app:

void fillAutoCompleteFromDatabase()
{
    mCursor = mDB.query(
            PetType.PETTYPE_TABLE_NAME,
            new String[] {PetType.PET_TYPE_NAME, 
             PetType._ID}, null, null,
             null, null,
            PetType.DEFAULT_SORT_ORDER);

    startManagingCursor(mCursor);


    int iNumberOfSpeciesTypes = mCursor.getCount();
    String astrAutoTextOptions[] = new String[iNumberOfSpeciesTypes];
    if((iNumberOfSpeciesTypes > 0) && (mCursor.moveToFirst()))
    {
        for(int i = 0; i < iNumberOfSpeciesTypes; i++)
        {
            astrAutoTextOptions[i] = 
                mCursor.getString(mCursor.
                           getColumnIndex(PetType.PET_TYPE_NAME));
            mCursor.moveToNext();
        }

        ArrayAdapter<String> adapter =
            new ArrayAdapter<String>(
                this,
                android.R.layout.simple_dropdown_item_1line,
                astrAutoTextOptions);

        AutoCompleteTextView text = 
            (AutoCompleteTextView) 
                findViewById(R.id.EditTextSpecies);
        text.setAdapter(adapter);
    }

}

I'm still learning Android and i have question - why i can add custom values from this array? I try:

void fillAutoCompleteFromDatabase()
{
    mCursor = mDB.query(
            PetType.PETTYPE_TABLE_NAME,
            new String[] {PetType.PET_TYPE_NAME, 
             PetType._ID}, null, null,
             null, null,
            PetType.DEFAULT_SORT_ORDER);

    startManagingCursor(mCursor);


    int iNumberOfSpeciesTypes = mCursor.getCount();
    String astrAutoTextOptions[] = new String[iNumberOfSpeciesTypes + 3];
    if((iNumberOfSpeciesTypes > 0) && (mCursor.moveToFirst()))
    {
        for(int i = 0; i < iNumberOfSpeciesTypes; i++)
        {
            astrAutoTextOptions[i] = 
                mCursor.getString(mCursor.
                           getColumnIndex(PetType.PET_TYPE_NAME));
            mCursor.moveToNext();
        }

        astrAutoTextOptions[iNumberOfSpeciesTypes + 1] = "aaaaaa"; 
        astrAutoTextOptions[iNumberOfSpeciesTypes + 2] = "bbbb";
        astrAutoTextOptions[iNumberOfSpeciesTypes + 3] = "cccccc";

        ArrayAdapter<String> adapter =
            new ArrayAdapter<String>(
                this,
                android.R.layout.simple_dropdown_item_1line,
                astrAutoTextOptions);

        AutoCompleteTextView text = 
            (AutoCompleteTextView) 
                findViewById(R.id.EditTextSpecies);
        text.setAdapter(adapter);
    }

}

I dont have error in Eclipse, but if i open this app on my mobile phone then she crashed.

3
  • You are missing an opening brace for the for loop. Commented Jul 26, 2014 at 19:44
  • Please, provide at least the exception message!!! Commented Jul 26, 2014 at 19:45
  • Sorry, brace exists. @Joseph i dont have exception message. First example working good. In second example i modify and added only working on array Commented Jul 26, 2014 at 19:46

1 Answer 1

1

Change

    astrAutoTextOptions[iNumberOfSpeciesTypes + 1] = "aaaaaa"; 
    astrAutoTextOptions[iNumberOfSpeciesTypes + 2] = "bbbb";
    astrAutoTextOptions[iNumberOfSpeciesTypes + 3] = "cccccc";

to

    astrAutoTextOptions[iNumberOfSpeciesTypes] = "aaaaaa"; 
    astrAutoTextOptions[iNumberOfSpeciesTypes + 1] = "bbbb";
    astrAutoTextOptions[iNumberOfSpeciesTypes + 2] = "cccccc";

The for loop stops at the index for which the cursor doesn't have a value, that's the index you need to start writing to. However you are skipping that value and writing to the next 3, while only 2 are left available then.

I would advise you to change your whole code to this:

void fillAutoCompleteFromDatabase() {
    mCursor = mDB.query(
            PetType.PETTYPE_TABLE_NAME,
            new String[] {PetType.PET_TYPE_NAME,
                    PetType._ID}, null, null,
            null, null,
            PetType.DEFAULT_SORT_ORDER);

    startManagingCursor(mCursor);

    if(mCursor.moveToFirst()) {
        List<String> astrAutoTextOptions = new ArrayList<String>();
        for(int i = 0; i < mCursor.getCount(); i++) {
            astrAutoTextOptions.add(mCursor.getString(mCursor.
                    getColumnIndex(PetType.PET_TYPE_NAME)));
            mCursor.moveToNext();
        }

        astrAutoTextOptions.add("aaaaaa");
        astrAutoTextOptions.add("bbbb");
        astrAutoTextOptions.add("cccccc");

        ArrayAdapter<String> adapter =
                new ArrayAdapter<String>(
                        this,
                        android.R.layout.simple_dropdown_item_1line,
                        astrAutoTextOptions);

        AutoCompleteTextView text = (AutoCompleteTextView) findViewById(R.id.EditTextSpecies);
        text.setAdapter(adapter);
    }
}

Lists are more portable in my opinion. Plus you were making some checks for your cursor.

One more thing. startManagingCursor is deprecated as far as i know. You shouldn't be using it. Instead use a CursorLoader.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.