阶段二冲刺六
昨天简单学习了自带数据库的使用
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".ui.activity.LoginActivity" android:padding="40dp"> <RelativeLayout android:id="@+id/re_1" android:layout_width="match_parent" android:layout_height="60dp" android:gravity="center"> <TextView android:id="@+id/text1" android:layout_width="90dp" android:layout_height="50dp" android:text="爱学" android:textSize="40dp" /> </RelativeLayout> <EditText android:id="@+id/eidt_login_photo" android:layout_width="match_parent" android:layout_height="60dp" android:layout_below="@id/re_1" android:layout_marginTop="40dp" android:background="@drawable/loginbiankuang" android:inputType="number" android:hint="手机号" android:gravity="center"/> <EditText android:id="@+id/eidt_login_password" android:layout_width="match_parent" android:layout_height="60dp" android:layout_below="@id/eidt_login_photo" android:layout_marginTop="20dp" android:background="@drawable/loginbiankuang" android:inputType="textPassword" android:hint="密码" android:gravity="center"/> <LinearLayout android:id="@+id/line1" android:layout_width="match_parent" android:layout_height="150dp" android:gravity="center" android:layout_below="@id/eidt_login_password" android:layout_marginTop="30dp"> <Button android:id="@+id/btn_login" android:layout_width="100dp" android:layout_height="100dp" android:layout_below="@id/eidt_login_password" android:background="@drawable/denglu_2" android:text="登录" android:textSize="20dp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@id/line1" android:gravity="center"> <TextView android:id="@+id/text_login" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="没有账号?注册一个" android:gravity="bottom" /> </LinearLayout> </RelativeLayout>
public class Userdao extends AppCompatActivity {
public void insert(User user){
ContentValues values = new ContentValues();
values.put("id",user.getId());
values.put("username",user.getUsername());
values.put("password",user.getPassword());
values.put("icon",user.getIcon());
values.put("age",user.getAge());
values.put("sex",user.getSex());
values.put("address",user.getAddress());
DBcreate.sqLiteDatabase.insert("user",null,values);
values.clear();
}
public User select(String id){
Cursor cursor = DBcreate.sqLiteDatabase.query("user", new String[]{"*"},"id=?", new String[]{id},null,null,null);
User user = new User();
if(cursor.moveToFirst()){
do{
@SuppressLint("Range") String phone = cursor.getString(cursor.getColumnIndex("id"));
@SuppressLint("Range") String username = cursor.getString(cursor.getColumnIndex("username"));
@SuppressLint("Range") String password = cursor.getString(cursor.getColumnIndex("password"));
@SuppressLint("Range") String icon = cursor.getString(cursor.getColumnIndex("icon"));
@SuppressLint("Range") String age = cursor.getString(cursor.getColumnIndex("age"));
@SuppressLint("Range") String sex = cursor.getString(cursor.getColumnIndex("sex"));
@SuppressLint("Range") String address = cursor.getString(cursor.getColumnIndex("address"));
user.setId(phone);
user.setUsername(username);
user.setPassword(password);
user.setIcon(icon);
user.setAge(age);
user.setSex(sex);
user.setAddress(address);
}while (cursor.moveToNext());
}
cursor.close();
return user;
}
}