В последний год я участвую в крупном проекте, и я очень плохо знаком с 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.Имя базы данных - «Пользователи», а таблица - «Пользователь».Записи внутри таблицы: имя пользователя, пароль, фамилия, имя.Я вставил информацию одного пользователя в базу данных.Проблема в том, что я не знаю, правильный ли мой 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.
-Дэйн