hello, I am new to sqlite and android. i m trying to develop an android app in which i have to do login. i had made a database but i am not confirmed that whether it is connected and giving the correct output of query or not?????? please help me..... i want to see the results of tables on the screen.
public class MainActivity extends Activity {
private EditText username=null;
private EditText password=null;
private TextView attempts;
private Button login;
int counter = 3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
username = (EditText)findViewById(R.id.editText1);
password = (EditText)findViewById(R.id.editText2);
attempts = (TextView)findViewById(R.id.textView5);
attempts.setText(Integer.toString(counter));
login = (Button)findViewById(R.id.button1);
public void login(View view){
String[][] userPass = { { "user1", "pass1" },
{ "user2", "pass2" },
{ "user3", "pass3" } };
String value1 = "userToCheck";
String value2 = "passToCheck";
boolean userOk = false;
for (String[] up : userPass)
if (value1.equals(up[0]) && value2.equals(up[1]))
userOk = true;
System.out.println("User authenticated: " + userOk);
Toast.makeText(getApplicationContext(), "Redirecting...",
Toast.LENGTH_SHORT).show();
}
public void onLocationChanged(Location loc) {
username.setText("");
username.setVisibility(View.INVISIBLE);
Toast.makeText(
getBaseContext(),
"Location changed : Lat: " + loc.getLatitude() + " Lng: "
+ loc.getLongitude(), Toast.LENGTH_SHORT).show();
String longitude = "Longitude: " + loc.getLongitude();
String TAG = null;
Log.v(TAG, longitude);
String latitude = "Latitude: " + loc.getLatitude();
Log.v(TAG, latitude);
String cityName = null;
Geocoder gcd = new Geocoder(getBaseContext(), Locale.getDefault());
List<Address> addresses;
try {
addresses = gcd.getFromLocation(loc.getLatitude(),
loc.getLongitude(), 1);
if (addresses.size() > 0)
System.out.println(addresses.get(0).getLocality());
cityName = addresses.get(0).getLocality();
} catch (IOException e) {
e.printStackTrace();
}
String s = longitude + "\n" + latitude + "\n\nMy Currrent City is: "
+ cityName;
username.setText(s);
}
public void onProviderDisabled(String provider) {
}
public void onProviderEnabled(String provider) {
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
public class DBOperations {
private static DataBaseWrapper dbHelper;
private String[] User_TABLE_COLUMNS = { DataBaseWrapper.username,DataBaseWrapper.userid, DataBaseWrapper.password};
private SQLiteDatabase database;
private static DBOperations operations = null;
private DBOperations()
{
}
public DBOperations(Context context) {
dbHelper = new DataBaseWrapper(context);
}
public void open() throws SQLException {
database = dbHelper.getWritableDatabase();
}
public void close() {
dbHelper.close();
}
public static DBOperations getInstance(Context context)
{
if(operations == null)
{
dbHelper = new DataBaseWrapper(context);
operations = new DBOperations();
}
return operations;
}
public UserClass adduser(String number, String string) {
ContentValues values = new ContentValues();
values.put(DataBaseWrapper.username, string);
values.put(DataBaseWrapper.password, number);
long userid = database.insert(DataBaseWrapper.User, null, values);
Cursor cursor = database.query(DataBaseWrapper.User,
User_TABLE_COLUMNS, DataBaseWrapper.userid + " = "
+ userid, null, null, null, null);
cursor.moveToFirst();
UserClass newuser = parseUser(cursor);
cursor.close();
return newuser;
}
public class DataBaseWrapper extends SQLiteOpenHelper {
public static final String User = "User";
public static final String userid = "userid";
static final String username="username";
static final String password="password";
private static final String DATABASE_NAME = "fyp.db";
protected static final int DATABASE_VERSION = 1;
private static final String UserCreate = "create table " + User
+ "(" + userid + " integer primary key autoincrement, "
+ username + " text not null);";
public DataBaseWrapper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(UserCreate);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS "+User);
onCreate(db);
}
}