3

The goal for my project is save the login ID and also, when user types in login ID if it is already saved, it should auto complete.

I am trying to save the entered values to a SQLite DB and then read it from there to populate the auto complete text view. The problem I am facing is, on trying to read the entered text before or after clicking the sign in button, the string is always "" (empty). I am not sure why this is always empty. I have tried to read the string in onCreate () as well as loginOnClick(View v). Both places I get an empty string. Here is the code:

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

}
4

2 回答 2

1

getEditableText()返回一个Editable对象,而不是一个String. 你想要的方法是getText()

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

于 2011-06-26T02:51:06.807 回答
1

我已经解决了从 getText() 返回空字符串的问题。在 onCreate() 中声明 AutoCompleteTextView loginId 是一个小错误。由于一旦执行 onCreate() loginID 就失去了作用域,当我们尝试读取 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(View 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);
    }   
    
于 2011-06-27T17:36:54.987 回答