Android 使用内容提供者登录详细信息,带来许多循环
我想在登录Android 使用内容提供者登录详细信息,带来许多循环,android,login,Android,Login,我想在登录活动上创建一个Toast文本(关于我尚未在数据库中注册)。然而,当我把这个Toast文本放进去时,出现了一个问题。如果我将它放入while循环Cursor.MoveToNext(),它将循环它所拥有的条目数的倍 循环内部 if(cursor!=null) { { int i = 0; w
活动
上创建一个Toast
文本(关于我尚未在数据库中注册)。然而,当我把这个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();
}
}
}
但当我把球打到圈外时,它会带来错误。你知道这个问题的解决方案是什么吗
在循环之外
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();
}
}
}
只是想澄清一下,我最终在我的登录部分使用For和While成功登录了。 另一个问题来自Toast文本(我无法将失败的Toast文本放入循环)。这是我的密码:
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();
}
});
}
private static final String TAG=“LoginActivity”;
私人按钮btnLogin,btnLinkToRegister;
私人会话管理器会话;
私人编辑文本密码、电子邮件;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u登录);
//定义一切
email=(EditText)findviewbyd(R.id.email);
密码=(EditText)findViewById(R.id.password);
btnLinkToRegister=(按钮)findViewById(R.id.btnLinkToRegister);
btnLogin=(按钮)findViewById(R.id.btnLogin);
//登录按钮点击事件
btnLogin.setOnClickListener(新视图.OnClickListener(){
公共void onClick(视图){
ContentResolver ContentResolver=getContentResolver();
String input_email=email.getText().toString().trim();
字符串输入_password=password.getText().toString().trim();
Log.d(标记“onClick:”+输入电子邮件+输入密码);
//检查表单中是否有空数据
如果(!input_email.isEmpty()&&!input_password.isEmpty()){
Log.d(标记“onClick:if语句成功”);
//从数据库中检查
String[]projection={ServiceProvidersContract.Columns.SEmail,ServiceProvidersContract.Columns.spPassword};
Cursor Cursor=contentResolver.query(ServiceProvidersContract.CONTENT\u URI,projection,null,null);
Log.d(标记“检查光标”+光标);
如果(光标!=null)
{
cursor.moveToFirst();
while(cursor.moveToNext())
{
Log.d(标记“while process”);
for(int i=0;i您得到的错误是什么?当您在循环中进行迭代时,cusror有100个值,因此对于所有值,如果它不输入,则将执行条件else,因此将生成100次toast。是的,我很抱歉。我没有将for循环放在这里,因此它无法工作。