Reputation: 255
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.
Upvotes: 0
Views: 35
Reputation: 44188
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.
Upvotes: 1