我是Android和Java的新手。我正在做一个登录页面程序。在该程序中,我不知道如何进行用户身份验证或验证过程。
请帮帮我
public class demo extends Activity implements OnClickListener {
/** Called when the activity is first created. */
EditText txtbox1, txtbox2;
private DataHelper dh;
Button button2, button1;
String name1, name2, name;
String DB_PATH, DATABASE_NAME;
String unames;
TextView tv;
private static final String TABLE_NAME = "table1";
private DataHelper db;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
txtbox1 = (EditText) findViewById(R.id.EditText01);
txtbox2 = (EditText) findViewById(R.id.EditText02);
button2 = (Button) findViewById(R.id.Button02);
button1 = (Button) findViewById(R.id.Button01);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
tv = (TextView) this.findViewById(R.id.mywidget);
tv.setSelected(true);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
int i = v.getId();
{
if (i == R.id.Button01) {
name1 = txtbox1.getText().toString();
name2 = txtbox2.getText().toString();
System.out.println("txtbox1" + txtbox1);
System.out.println("name1" + name1);
System.out.println("name2" + name2);
StringBuilder sb = new StringBuilder();
sb.append("Insert Employee Details:\n");
dh = new DataHelper(this);
dh.deleteAll();
dh.insert(name1);
dh.insert(name2);
Toast.makeText(getApplicationContext(), name1,
Toast.LENGTH_LONG).show();
List<String> names = this.dh.selectAll();
/* sb.append("Names in database:\n"); */
for (String name : names) {
sb.append(name + "\n");
System.out.println("name=" + names);
Log.d("EXAMPLE", "names size - " + names.size());
}
}
}
if (i == R.id.Button02)
System.out.println("ok");
System.out.println("name=" + unames);
/* GetValue();
*/
{
if (name1.equals(unames)) {
System.out.println("ok1");
Toast.makeText(getApplicationContext(), name1,
Toast.LENGTH_LONG).show();
} else {
System.out.println("no");
/*
* Toast.makeText(getApplicationContext(),no ,
* Toast.LENGTH_LONG).show();
*/
}
}
}
}
数据库部分
public class DataHelper {
private static final String DATABASE_NAME = "example1rrrr.db";
private static final int DATABASE_VERSION = 1;
private static final String TABLE_NAME = "table1";
private static final String name = "name";
private static final String password = "password";
private static final String KEY_ROWID = "rowid";
private Context context;
private static SQLiteDatabase db;
private SQLiteStatement insertStmt;
private static final String INSERT = "insert into " + TABLE_NAME
+ "(name) values (?)";
public DataHelper(Context context) {
this.context = context;
OpenHelper openHelper = new OpenHelper(this.context);
this.db = openHelper.getWritableDatabase();
this.insertStmt = this.db.compileStatement(INSERT);
}
public long insert(String name) {
this.insertStmt.bindString(1, name);
return this.insertStmt.executeInsert();
}
public void deleteAll() {
this.db.delete(TABLE_NAME, null, null);
}
public List<String> selectAll() {
List<String> list = new ArrayList<String>();
Cursor cursor = this.db.query(TABLE_NAME, new String[] { "name" },
null, null, null, null, "name asc");
if (cursor.moveToFirst()) {
do {
list.add(cursor.getString(0));
} while (cursor.moveToNext());
}
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
return list;
}
private static class OpenHelper extends SQLiteOpenHelper {
OpenHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABLE_NAME
+ "(id INTEGER PRIMARY KEY, name TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w("Example",
"Upgrading database, this will drop tables and recreate.");
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
答案 0 :(得分:0)
您使用数据库的原因是什么?将登录数据存储在共享首选项中会更容易:
http://developer.android.com/guide/topics/data/data-storage.html#pref