我在最后一年做了重大项目,我对Android加上我很新;我不擅长编码。我的登录页面需要帮助。我创建了类似数据库连接java文件的东西:
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();
}
}
我已经为使用SQLite的用户创建了一个数据库。数据库名称为Users,该表名为User。表中的记录是Username,Password,LastName,FirstName。我已将一个用户的信息插入数据库。问题是我不知道我的UserDB.java是否正确。
我还创建了login.java。硬编码登录页面:
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);
}
}
所以我想知道如何在login.java上应用数据库连接。我应该插入数据库连接,如db.Open();?几个月前我研究了ASP.Net,我忘记了大部分学到的东西。
那么我应该如何在login.java上打开数据库连接以及如何用数据库检查用户是否输入了正确的用户名和密码?
只是因为你需要我的xml,这是代码:
<?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>
感谢任何帮助。我需要学习如何做到这一点,以便我可以申请其他页面,例如Register.java页面。
-Dayne
答案 0 :(得分:0)
第一个,是否要检查是否在用户表中记录。好的,你可以。在Eclipse中,打开文件资源管理器 - &gt;数据 - &gt;数据 - &gt;你的应用程序包(我认为one.two)。之后,单击图标从设备中提取文件(图标如FDD驱动程序)。 之后安装SQLite Expert Professional打开此文件。好的,你会知道的。
在Android中制作数据库。看到这个网站:
http://developer.android.com/resources/tutorials/notepad/index.html
希望对你有所帮助。
答案 1 :(得分:0)
对于数据库:您应该拥有一个在用户表上自动增量的主键。并尝试将代码放在onCreate()和onUpgrade()中。这里的例子:
@Override
public void onCreate(SQLiteDatabase db) {
// Creating Tables
String CREATE_EMPLOYEES_TABLE = "CREATE TABLE " + TABLE_EMPLOYEES + "("
+ KEY_EMPID + " INTEGER PRIMARY KEY autoincrement," + KEY_FIRSTNAME
+ " TEXT not null," + KEY_LASTNAME + " TEXT not null," + KEY_DEPARTMENTNAME
+ " TEXT not null," + KEY_EMAIL + " TEXT not null," + KEY_PHONE
+ " TEXT not null," + KEY_PASSWORD + " TEXT not null " + ")";
db.execSQL(CREATE_EMPLOYEES_TABLE);
String CREATE_DEPARTMENTS_TABLE = "CREATE TABLE " + TABLE_DEPARTMENTS
+ "(" + KEY_DEPARTMENTID + " INTEGER PRIMARY KEY autoincrement,"
+ KEY_DEPARTMENTNAME + " TEXT not null"
+ ")";
db.execSQL(CREATE_DEPARTMENTS_TABLE);
}
// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_EMPLOYEES);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_DEPARTMENTS);
// Create tables again
onCreate(db);
}`
登录时,请尝试:
@Override
public void onClick(View v) {
if(et1.getText().toString().equals("admin") &&
et2.getText().toString().equals("admin")) {
Toast.makeText(getApplicationContext(),
"Redirecting...",Toast.LENGTH_SHORT).show();
Intent intent = new Intent(Login.this, AdminHome.class);
startActivity(intent);
finish();
}else{
Toast.makeText(getApplicationContext(), "Wrong Credentials",Toast.LENGTH_SHORT).show();
}
}
});