检索时登录时出错

时间:2012-04-13 12:47:08

标签: android sqlite

我有一个包含四个edittexts的主屏幕。在每个edittext中,我只接受单个数字值。我在每个edittexts上设置requestFocus()。我在第四个edittext中输入值,它应该调用构造函数,触发查询和放大器;无论登录是否成功,都应该回复我。相反,它给我的消息为“登录失败”

这是我的代码

edit4.setOnKeyListener(new OnKeyListener() {
    @Override
    public boolean onKey(View arg0, int arg1, KeyEvent arg2) 
    {
        // TODO Auto-generated method stub
        if(edit4.getText().toString().length()==1)
        {
            ParentDBHelper helper = new ParentDBHelper(getApplicationContext(), "db_parents", null, 2);
            SQLiteDatabase db=helper.getWritableDatabase();
            Cursor c=db.rawQuery("SELECT * FROM tbl_countries", null);
            // check if the table is empty 
            if (!c.moveToNext())
            {
                Toast.makeText(getApplicationContext(), "No data to display, please make sure you have already inserted data!", Toast.LENGTH_LONG).show();
                db.close();
                return false;
            }
            c.moveToPrevious();
            // if the table is not empty, read the result into a string named display 
            while(c.moveToNext())
            {
                String 
                String no1=c.getColumnName(5); 
                if(no1==edit1.getText().toString()+edit2.getText().toString()+edit3.getText().toString()+edit4.getText().toString())
                {
                    flag_status_pin=1;
                    Toast.makeText(getApplicationContext(), "Login Successful!!", Toast.LENGTH_LONG).show();
                    Intent dash1=new Intent(getApplicationContext(),DashBoard.class);
                    startActivity(dash1);
                }
            }
            if(flag_status_pin==0)
            {
                Toast.makeText(getApplicationContext(), "Login Failed!!", Toast.LENGTH_LONG).show();
                Intent homes=new Intent(getApplicationContext(),home.class);
                startActivity(homes);
            }  

        }//if 

        return false;
    }}); 

感谢。

2 个答案:

答案 0 :(得分:0)

对于您的edittext(s)使用TextWatcher比使用keylistener更好:

TextWatcher watcher = new TextWatcher(){

    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    //Test here for login

}

    @Override
    public void afterTextChanged(Editable editable) {
    }
};

然后将其设置为edit4.addTextChangedListener(watcher);您需要稍微更改一下逻辑。

答案 1 :(得分:0)

您正在使用==比较字符串。这不起作用。请改用equals。

此外,您要将PIN码与列名进行比较?你不应该把它与

进行比较
c.getString(5)

代替?