2012-04-13 44 views
0

我有一個包含四個edittexts.In每個EditText上我只接受每個edittexts.Once我在第四輸入值的EditText應該調用構造單個數字value.I設置requestFocus()方法主屏幕,火查詢&應該返回我登錄是否成功not.Instead,它給我消息「登錄失敗」錯誤登錄在檢索

這裏是我的代碼

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

感謝。

回答

0

這將是更好地使用TextWatcher您的EditText(S)比的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);您需要更改烏爾邏輯一點點。

+0

謝謝You.In哪種方法,我應該寫的代碼? – swanand 2012-04-13 14:31:04

+0

@swanand:的onCreate()將fine..you可以postthe更新的代碼,如果ü面對任何問題 – Akhil 2012-04-14 03:51:45

0

您比較字符串使用==。這不起作用。改用equals。

而且,你是比較你的PIN碼以列名?難道你不應該把它比作

c.getString(5) 

改爲?

+0

Thanks.But它不是在循環飛到哪裏,IM燒製query.Instead它總是顯示我「登錄失敗」 – swanand 2012-04-16 14:32:31

+0

我」 m不確定在你將moveToNext超出遊標限制後你可以回來。如果moveToFirst [...] else返回,我會用單個替換if和while和moveToPrevious – njzk2 2012-04-16 15:00:50