views:

71

answers:

1

Hi,

I'm doing Major Project on my final year and I'm very new to Android plus I;m not good at codings. I need help with my login page. I've created something like a database connection java file which is this:

package one.two;

import java.util.List;

import android.app.ListActivity;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import java.util.ArrayList;

    public class UserDB 
    {
     public String KEY_USERNAME = "Username";
     public  String KEY_PASSWORD = "Password";
     public String KEY_LNAME = "LastName";
     public String KEY_FNAME = "FirstName";

     private static final String DATABASE_NAME = "Users";
     private static final String DATABASE_TABLE = "User";

     private static final int DATABASE_VERSION = 1;

     private static Context context;

     private static DatabaseHelper DBHelper;
     private static SQLiteDatabase db;

     public UserDB(Context ctx)
     {
      this.context = ctx;
      DBHelper = new DatabaseHelper(context);
     }

     private static class DatabaseHelper extends SQLiteOpenHelper
     {
      DatabaseHelper(Context context)
      {
       super(context, "Users", null, 1);
      }

      @Override
      public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
      {
      }

      @Override
      public void onCreate(SQLiteDatabase db)
      {
      }
     }//end DatabaseHelper

     // ---opens the database---
     public UserDB open() throws SQLException
     {
      db = DBHelper.getWritableDatabase();
      return this;
     }

     // ---closes the database---
     public void close()
     {
      DBHelper.close();
     }


    }

I've already created a database for users using SQLite. The database name is Users and the table is called User. The records inside the table are Username, Password, LastName, FirstName. I've inserted one user's info into the database. The problem is I do not know whether my UserDB.java is correct.

And I've also created login.java. Hardcoded Login page:

package one.two;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.TextView;



public class Login extends Activity implements OnClickListener{


 UserDB db = new UserDB(this);
/** Called when the activity is first created. */

 private EditText etUsername;
 private EditText etPassword;
 private Button btnLogin;
 //private Button btnRegister;
 private TextView lblResult;

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.login);
     // Get the EditText and Button References
        etUsername = (EditText)findViewById(R.id.usernametxt);
        etPassword = (EditText)findViewById(R.id.passwordtxt);
        btnLogin = (Button)findViewById(R.id.btnLogin);
        //btnRegister = (Button)findViewById(R.id.btnRegister);
        lblResult = (TextView)findViewById(R.id.msglbl);

        //Cursor c = (Cursor) db.getAllTitles();

        Button btnArrival = (Button) findViewById(R.id.btnRegister);
     btnArrival.setOnClickListener(this);


    // Set Click Listener
    btnLogin.setOnClickListener(new OnClickListener() {
 @Override
 public void onClick(View v) {
  // Check Login
  String username = etUsername.getText().toString();
  String password = etPassword.getText().toString();

  if(username.equals("guest") && password.equals("guest")){
   lblResult.setText("Login successful.");
  } else {
   lblResult.setText("Login failed. Username and/or password doesn't match.");
  }
 }
});



 }

    public void onClick(View v)
 {
   Intent intent = new Intent(this, DatabaseActivity.class);
   startActivity(intent);
}

}

So I want to know how I should apply the database connection on the login.java. Should I insert database connection something like db.Open();? I studied ASP.Net a few months back and I kind of forget most of what I've learnt.

So how should I open the database connection on login.java and how to check with database whether the user enters the right username and password?

Just incase you need my xml, here's the code:

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:id="@+id/widget0"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background = "@drawable/image"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<TextView
android:id="@+id/widget32"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ferry Hub"
android:textSize="25sp"
android:textStyle="bold"
android:textColor="#ff009999"
android:layout_x="97px"
android:layout_y="15px"
>
</TextView>
<TextView
android:id="@+id/widget35"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"
android:textColor="#ff100999"
android:layout_x="116px"
android:layout_y="48px"
>
</TextView>
<EditText
android:layout_width="168px"
android:layout_height="42px"
android:text=""
android:textSize="18sp"
android:layout_x="133px"
android:layout_y="146px"
android:id="@+id/passwordtxt">
</EditText>
<EditText
android:layout_width="169px"
android:layout_height="41px"
android:text=""
android:textSize="18sp"
android:layout_x="132px"
android:layout_y="93px"
android:id="@+id/usernametxt">
</EditText>

<TextView
android:id="@+id/widget31"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Username:"
android:textColor="#ff000000"
android:layout_x="32px"
android:layout_y="102px"
>
</TextView>
<TextView
android:id="@+id/widget32"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Password:"
android:textColor="#ff000000"
android:layout_x="32px"
android:layout_y="155px"
>
</TextView>

<Button
android:id="@+id/btnLogin"
android:layout_width="109px"
android:layout_height="34px"
android:text="Login"
android:layout_x="38px"
android:layout_y="272px"
>
</Button>
<Button
android:id="@+id/btnRegister"
android:layout_width="110px"
android:layout_height="35px"
android:text="Register"
android:layout_x="159px"
android:layout_y="272px"
>
</Button>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click 'Register' if you do not have an account."
android:textSize="13px"
android:textColor="#ff000000"
android:layout_x="3px"
android:layout_y="305px"
android:id="@+id/msglbl">
</TextView>
</AbsoluteLayout>

Any help is appreciated. I need to learn how to do it so that I can apply for other pages for example Register.java page.

-Dayne

A: 

the first, Are you want to check whether or not record in User table. Ok you can. In Eclipse, Open File Explorer-->data-->data-->package of your app (i think one.two). after that click icon pull a file from the device (icon like FDD driver). After that install SQLite Expert Professional to open this file. Ok you will know.

To make database in Android. See this site:

http://developer.android.com/resources/tutorials/notepad/index.html

hope helpful for you.

Hoang Anh Song
ouh yeap i did that part already.
Dayne