Android 为什么AutoCompleteTextView上的getText()和getEditableText()总是生成空字符串?

Android 为什么AutoCompleteTextView上的getText()和getEditableText()总是生成空字符串?,android,string,gettext,autocompletetextview,Android,String,Gettext,Autocompletetextview,我的项目的目标是保存登录ID,而且,当用户输入登录ID时(如果已经保存),它应该自动完成 我试图将输入的值保存到SQLite DB中,然后从那里读取它以填充自动完成文本视图。 我面临的问题是,在单击“登录”按钮之前或之后尝试读取输入的文本时,字符串始终为“”(空)。我不知道为什么这个总是空的。 我尝试读取onCreate()和loginOnClick(视图v)中的字符串。这两个地方我都得到一个空字符串。 代码如下: public class LoginActivity extends Activ

我的项目的目标是保存登录ID,而且,当用户输入登录ID时(如果已经保存),它应该自动完成

我试图将输入的值保存到SQLite DB中,然后从那里读取它以填充自动完成文本视图。 我面临的问题是,在单击“登录”按钮之前或之后尝试读取输入的文本时,字符串始终为“”(空)。我不知道为什么这个总是空的。 我尝试读取onCreate()和loginOnClick(视图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;
    }

}
公共类LoginActivity扩展活动{
私有LoginFoSqliteOpenHelper LoginFoSqliteHelper;
专用数据库;
私有字符串用户标识;
私有字符串passwd;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
//仅测试
deleteDatabase(DB_NAME);//出于测试目的-删除DB以获得一个干净的开始
loginInfoSQLiteHelper=新的LoginFoSqliteOpenHelper(LoginActivity.this);
database=logininfosqliteheloper.getWritableDatabase();
persistLoginId(“Champ”);
persistLoginId(“柬埔寨”);
persistLoginId(“加拿大”);
persistLoginId(“Champ”);
字符串[]用户名=getAllUserNames();
for(字符串str:用户名){
Toast.makeText(this,str,Toast.LENGTH_SHORT).show();
}
AutoCompleteTextView loginId=(AutoCompleteTextView)findViewById(R.id.id\u text);
ArrayAdapter=新的ArrayAdapter(此,R.layout.list_项,用户名);
loginId.setAdapter(适配器);
userId=loginId.getEditableText().toString();//始终为空
EditText密码=(EditText)findViewById(R.id.password\u text);
}
@凌驾
public void onBackPressed(){
super.onBackPressed();
完成();
}
public void LoginClick(视图v){
persistLoginId(“加拿大”);
persistLoginId(userId);
//userID=”“空
//Toast.makeText(这个“登录按钮有效”,Toast.LENGTH_LONG.show();
}
公共字符串[]getAllUserNames(){
Cursor Cursor=database.query(DB_TABLE_NAME,新字符串[]{DB_COLUMN_1_NAME},null,
空,空,空,空);
String[]str=新字符串[cursor.getCount()];
int i=0;
cursor.moveToFirst();
如果(!cursor.isAfterLast()){
做{
字符串名称=cursor.getString(cursor.getColumnIndex(DB_COLUMN_1_name));
str[i]=名称;
i++;
}while(cursor.moveToNext());
cursor.close();
返回str;
}
否则{
cursor.close();
返回新字符串[]{};
}
}
私有void persistLoginId(字符串loginId){
//检查loginID是否存在,然后不添加
如果(!loginId存在(loginId)){
ContentValues ContentValues=新ContentValues();
contentValues.put(DB\u COLUMN\u 1\u NAME,loginId);
Log.i(this.toString()+“insertUserName”,“插入用户名:”+loginId);
insert(DB_TABLE_NAME,null,contentValues);
}否则{
//用于测试
Toast.makeText(这个,loginId+“存在。未添加”,Toast.LENGTH_LONG.show();
}
}
公共布尔loginIdExists(字符串id){
字符串DB\u SELECT\u SCRIPT=“SELECT”+DB\u COLUMN\u 1\u NAME+”来自“+DB\u TABLE\u NAME”;
SQLiteDatabase db=logininfossqlitehelper.getReadableDatabase();
Cursor Cursor=db.rawQuery(db\u SELECT\u脚本,null);
如果(光标!=null){
cursor.moveToFirst();
如果(!cursor.isAfterLast()){
做{
字符串名称=cursor.getString(cursor.getColumnIndex(DB_COLUMN_1_name));
如果(id.equals(name)){
cursor.close();
返回true;
}
}while(cursor.moveToNext());
}
否则{
cursor.close();
返回false;
}
}
返回false;
}
公共布尔removeUserName(字符串用户名){
int result=database.delete(DB_TABLE_NAME,“user_NAME='”+userName+“'”,null);
如果(结果>0)
返回true;
其他的
返回false;
}
}
getEditableText()
返回可编辑的对象,而不是字符串。您需要的方法是
getText()


来源:

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

  • 已将AutoCompleteTextView loginId声明为活动的私有成员-

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

  • 我也尝试过getText()。也就是返回一个空字符串。
    loginId = (AutoCompleteTextView) findViewById(R.id.id_text);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_item, userNames);
    loginId.setAdapter(adapter);
    
    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);
    }