为什么AutoCompleteTextView上的getText()和getEditableText()总是产生一个空字符串?

时间:2011-06-26 01:48:18

标签: android string gettext autocompletetextview

我的项目的目标是保存登录ID,当用户输入登录ID时,如果已经保存,则应自动完成。

我正在尝试将输入的值保存到SQLite DB,然后从那里读取它以填充自动完整文本视图。 我面临的问题是,在单击登录按钮之前或之后尝试读取输入的文本时,字符串始终为“”(空)。我不知道为什么这总是空的。 我试图读取onCreate()中的字符串以及loginOnClick(View v)。两个地方我都得到一个空字符串。 这是代码:

public class LoginActivity extends Activity {

    private LoginInfoSQLiteOpenHelper loginInfoSQLiteHelper;
    private SQLiteDatabase database;
    private String userId;
    private String passwd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);

        //TEST ONLY
        deleteDatabase(DB_NAME);   // For testing purposes - Delete DB for a clean start

        loginInfoSQLiteHelper  = new LoginInfoSQLiteOpenHelper(LoginActivity.this);
        database = loginInfoSQLiteHelper.getWritableDatabase();

        persistLoginId("Champ");
        persistLoginId("Cambodia");
        persistLoginId("Canada");
        persistLoginId("Champ");

        String [] userNames = getAllUserNames();

        for(String str: userNames){
            Toast.makeText(this,  str, Toast.LENGTH_SHORT).show();
        }

        AutoCompleteTextView loginId = (AutoCompleteTextView) findViewById(R.id.id_text);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_item, userNames);
        loginId.setAdapter(adapter);
        userId = loginId.getEditableText().toString(); //always empty

        EditText password = (EditText) findViewById(R.id.password_text);

    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();              

        finish();
    }

    public void loginOnClick(View v) {
        persistLoginId("Canada");
        persistLoginId(userId);
//userID = "" empty

        //Toast.makeText(this, "Sign In Button works " , Toast.LENGTH_LONG).show();

    }

    public String[] getAllUserNames(){

    Cursor cursor = database.query(DB_TABLE_NAME,new String[]{DB_COLUMN_1_NAME},null,
                null,null,null,null);

        String[] str = new String[cursor.getCount()];
        int i = 0;

        cursor.moveToFirst();

        if(!cursor.isAfterLast()){
            do{
                String name = cursor.getString(cursor.getColumnIndex(DB_COLUMN_1_NAME));
                str[i] = name;
                i++;
            }while(cursor.moveToNext());
            cursor.close();
            return str;         
        }
        else{
            cursor.close();         
            return new String[]{};
        }

    }

    private void persistLoginId(String loginId) {

            //Check if loginID exists then do not add
            if ( !loginIdExists(loginId)){
                ContentValues contentValues = new ContentValues();
                contentValues.put(DB_COLUMN_1_NAME, loginId);
                Log.i(this.toString() + " insertUserName", "Inserting username : " + loginId);
                database.insert(DB_TABLE_NAME, null, contentValues);
            } else{
                //for testing 
                Toast.makeText(this, loginId + "  exists. Not Added", Toast.LENGTH_LONG).show();
            }

    }

    public boolean loginIdExists(String id) {
        String DB_SELECT_SCRIPT = "SELECT " + DB_COLUMN_1_NAME + " FROM " + DB_TABLE_NAME ;
        SQLiteDatabase db = loginInfoSQLiteHelper.getReadableDatabase();
        Cursor cursor = db.rawQuery(DB_SELECT_SCRIPT, null);

        if( cursor != null){
            cursor.moveToFirst();
            if(!cursor.isAfterLast()){
                do{
                    String name = cursor.getString(cursor.getColumnIndex(DB_COLUMN_1_NAME));
                    if(id.equals(name)){
                        cursor.close();
                        return true;
                    }
                }while(cursor.moveToNext());        
            }
            else{
                cursor.close();         
                return false;
            }
        }
        return false;       
    }

    public boolean removeUserName(String userName){
        int result = database.delete(DB_TABLE_NAME, "user_name='" + userName + "'", null);

        if(result > 0)
            return true;
        else
            return false;
    }

}

2 个答案:

答案 0 :(得分:1)

getEditableText()会返回Editable个对象,而不是String。您想要的方法是getText()

来源:http://developer.android.com/reference/android/widget/EditText.html

答案 1 :(得分:1)

我已经修复了从getText()返回的空字符串的问题。在onCreate()中声明AutoCompleteTextView loginId是一个小错误。由于loginID在执行onCreate()时失去作用域,当我们尝试在loginOnClick()中读取输入的字符串时,它被读为空。这就是我为解决它而做的事情。

  1. 将AutoCompleteTextView loginId声明为Activity的私有成员 -

    private AutoCompleteTextView loginId;
    
  2. 将onCreate()更改为 -

    loginId = (AutoCompleteTextView) findViewById(R.id.id_text);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_item, userNames);
    loginId.setAdapter(adapter);
    
  3. 更改了loginOnClick(查看v),如下所示

    public void loginOnClick(View v) {
        String userId = loginId.getText().toString();
        EditText password = (EditText) findViewById(R.id.password_text);
        String passwd = password.getText().toString();
                persistLoginId(userId);
    }