2017-01-10 54 views
0

我想在登錄Activity上創建一個Toast文本(關於我沒有在數據庫中註冊)。但是,當我放入Toast文本時出現問題。如果我把它放在一個while循環中,它會循環儘可能多的條目。使用Content Provider的登錄詳細信息,帶來很多循環

if(cursor!=null) 
        { 
         { 
          int i = 0; 
          while(cursor.moveToNext()) 
          { 
           if(cursor.getString(i).equals(input_email) && cursor.getString(i).equals(input_password)) 
           { 
            Log.d(TAG, "Udah masuk belum "); 
            Toast.makeText(getApplicationContext(),"Login Successful!", Toast.LENGTH_SHORT).show(); 
            Intent intent =new Intent(LoginActivity.this, RegisterActivity.class); 
            startActivity(intent); 
            finish(); 
            i = i + 1; 
           } 
           else 
           { 
            Toast.makeText(getApplicationContext(),"You haven't Registered yet!", Toast.LENGTH_SHORT).show(); 
            Intent intent =new Intent(LoginActivity.this, RegisterActivity.class); 
            startActivity(intent); 
            finish(); 
           } 
          } 
          cursor.close(); 
         } 
        } 

內,但如果我的推杆循環外,它所帶來的誤差。你知道這個解決方案是什麼嗎?

 String [] projection ={ServiceProvidersContract.Columns.SEmail, ServiceProvidersContract.Columns.spPassword}; 
        Cursor cursor = contentResolver.query(ServiceProvidersContract.CONTENT_URI, projection, null, null, null); 

        Log.d(TAG, "Checking Cursor" + cursor); 
        if(cursor!=null) 
        { 
         { 
          int i = 0; 
          while(cursor.moveToNext()) 
          { 
           if(cursor.getString(i).equals(input_email) && cursor.getString(i).equals(input_password)) 
           { 
            Log.d(TAG, "Udah masuk belum "); 
            Toast.makeText(getApplicationContext(),"Login Successful!", Toast.LENGTH_SHORT).show(); 
            Intent intent =new Intent(LoginActivity.this, RegisterActivity.class); 
            startActivity(intent); 
            finish(); 
            i = i + 1; 
           } 
          } 
          cursor.close(); 
          if(!cursor.getString(i).equals(input_email) && cursor.getString(i).equals(input_password)) 
          { 
           Toast.makeText(getApplicationContext(),"You haven't Registered yet!", Toast.LENGTH_SHORT).show(); 
           Intent intent =new Intent(LoginActivity.this, RegisterActivity.class); 
           startActivity(intent); 
           finish(); 

          } 
         } 

        } 
+0

你得到什麼錯誤? –

+0

正如你迭代while循環你cusror有說100個值,所以如果它不進入所有的價值,如果條件else將被執行,所以100次烤麪包將被生成。 – Mrinmoy

+0

是的,我很抱歉。我沒有把For循環放在這裏,所以這就是爲什麼它不能工作。 –

回答

0

只是想澄清之外,我也終於成功地在我登錄的部分使用和同時登錄。 另一個問題來自吐司文本(我不能把失敗的吐司文本放入循環中)。這是我的代碼:

private static final String TAG = "LoginActivity"; 
private Button btnLogin, btnLinkToRegister; 
private SessionManager session; 
private EditText password, email; 


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



     // Defining everything 
     email = (EditText) findViewById(R.id.email); 
     password = (EditText) findViewById(R.id.password); 
     btnLinkToRegister = (Button) findViewById(R.id.btnLinkToRegister); 
     btnLogin = (Button) findViewById(R.id.btnLogin); 


    //  Login button Click Event 
     btnLogin.setOnClickListener(new View.OnClickListener() { 

      public void onClick(View view) { 
       ContentResolver contentResolver = getContentResolver() ; 

       String input_email = email.getText().toString().trim(); 
       String input_password = password.getText().toString().trim(); 
       Log.d(TAG, "onClick: " + input_email + input_password); 

       // Check for empty data in the form 
       if (!input_email.isEmpty() && !input_password.isEmpty()) { 
        Log.d(TAG, "onClick: if statement succeseful"); 

       //  checking from the database 
        String [] projection ={ServiceProvidersContract.Columns.SEmail, ServiceProvidersContract.Columns.spPassword}; 
        Cursor cursor = contentResolver.query(ServiceProvidersContract.CONTENT_URI, projection, null, null, null); 

        Log.d(TAG, "Checking Cursor" + cursor); 
        if(cursor!=null) 
        { 
         cursor.moveToFirst(); 
         while(cursor.moveToNext()) 
         { 
          Log.d(TAG, "while process"); 
          for(int i=0; i<cursor.getColumnCount(); i++) 
          { 
           Log.d(TAG, "for process " + cursor.getString(i)); 
           if (cursor.getString(0).equals(input_email) && cursor.getString(1).equals(input_password)) { 
             Log.d(TAG, "if process"); 
             Toast.makeText(getApplicationContext(), "Login Successful!", Toast.LENGTH_SHORT).show(); 
             Intent intent = new Intent(LoginActivity.this, RegisterActivity.class); 
             startActivity(intent); 
             finish(); 
            break; 
           } 
          } 
         } 
         if (!cursor.getString(0).equals(input_email) && cursor.getString(1).equals(input_password)) { 
          Log.d(TAG, "if process"); 
          Toast.makeText(getApplicationContext(), "You haven't registered yet!", Toast.LENGTH_SHORT).show(); 
          Intent intent = new Intent(LoginActivity.this, RegisterActivity.class); 
          startActivity(intent); 
          finish(); 
         } 
         cursor.close(); 
        } 
       } else { 
        // Prompt user to enter credentials 
        Toast.makeText(getApplicationContext(), 
          "Please enter the credentials!", Toast.LENGTH_LONG) 
          .show(); 
       } 
      } 
     }); 

      btnLinkToRegister.setOnClickListener(new View.OnClickListener() { 

      public void onClick(View view){ 
        Intent i = new Intent(LoginActivity.this, RegisterActivity.class); 
        startActivity(i); 
        finish(); 
       } 
     }); 
    } 

}