使用Content Provider登录详细信息,带来许多循环

时间:2017-01-10 09:05:23

标签: android login

我想在登录Toast上创建一个Activity文本(关于我未在数据库中注册)。但是,当我放置此Toast文本时出现问题。如果我将它放在while循环Cursor.MoveToNext()中,它将循环多次输入。

循环内

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

                            }
                        }

                    }

1 个答案:

答案 0 :(得分:0)

只是想澄清一下,我最终使用For和While成功登录我的登录部分。 另一个问题来自Toast Text(我无法将失败的Toast Text放入循环中)。这是我的代码:

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

}