I want a simple sign in app in which a user logins successfully if his username and password are same as in the database otherwise a toast message is displayed. But I am unable to fetch data from the database to validate the user. My database contains fields named username, password, email and phoneno. Plzz help me.
Here is my code:
package com.buttons;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class signin extends Activity implements OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.signin);
Button signin = (Button) findViewById(R.id.signin);
signin.setOnClickListener(this);
Button cancel = (Button) findViewById(R.id.cancel);
cancel.setOnClickListener(this);
}
public void onClick(View v) {
if(v.getId()== R.id.signin)
{
EditText username = (EditText) findViewById(R.id.UserName);
String unm= username.getText().toString();
EditText password = (EditText) findViewById(R.id.Password);
String pwd = password.getText().toString();
if(unm.equals(""))
{
Toast.makeText(getBaseContext(), "enter username",5000).show();
}
else
{
SQLiteDatabase db = openOrCreateDatabase("TravellApp", MODE_PRIVATE, null);
Cursor c = db.rawQuery("select * from user where username='"+unm+"'", null);
String pass = c.getColumnName(c.getColumnIndex("password"));
if(pwd.equals(""))
{
Toast.makeText(getBaseContext(), "enter password", 5000).show();
}
else
{
if(pass.equals(pwd))
{
Intent it = new Intent(signin.this, home.class);
it.putExtra("username", unm);
it.putExtra("password", pwd);
startActivity(it);
}
else
{
Toast.makeText(getBaseContext(), "password does not match", 5000).show();
}
}
}
}
if(v.getId()==R.id.cancel)
{
Intent it = new Intent(signin.this, main.class);
startActivity(it);
}
}
}
This might be helpful: http://android-er.blogspot.com/2011/06/simple-example-using-androids-sqlite.html