将自定义值添加到数组

时间:2014-07-26 19:42:19

标签: android

我有一个示例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);
    }

}

我还在学习Android,我有疑问 - 为什么我可以从这个数组中添加自定义值?我试试:

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

}

我在Eclipse中没有错误,但如果我在手机上打开这个应用程序,那么她就崩溃了。

1 个答案:

答案 0 :(得分:1)

更改

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

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

for循环在光标没有值的索引处停止,这是您需要开始写入的索引。但是,您正在跳过该值并写入下一个3,而当时只有2个可用。

我建议你将整个代码更改为:

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

在我看来,列表更便携。另外,你正在对光标进行一些检查。

还有一件事。据我所知,startManagingCursor已被弃用。你不应该使用它。而是使用CursorLoader