【Android Sqlite】萌动的sqlite数据库,简单实现:用户增删改查

前端之家收集整理的这篇文章主要介绍了【Android Sqlite】萌动的sqlite数据库,简单实现:用户增删改查前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

sqlite数据的使用很常见呢,我们简单学习一下app的登录注册修改删除用户吧!

下面就写一个萌动的app注册登录吧!


页面

还有2个图片按钮(虽然不是很好看,但是将就吧→。→)




EditView输入框的监听事件,2张图片的切换

(为了找22,33的图片,我反编译了 bilibii 的app,想不到图片是分为4张的 →。→)


用户注册



修改密码:这里的判断比较多!



删除


代码构成一览:




一、entity:User 实体类

自己写 →。→

private int id;
private String uname;
private String upwd;
private int isDel;

二、dao包:

DBOpenHelper:建表


package example.com.user_sqlite.dao;

import android.content.Context;
import android.database.sqlite.sqliteDatabase;
import android.database.sqlite.sqliteDatabase.CursorFactory;
import android.database.sqlite.sqliteOpenHelper;

/**  * Created by Administrator on 2017/4/5.  */ public class DBOpenHelper extends sqliteOpenHelper{

    public DBOpenHelper(Context context,String name,CursorFactory factory,int version) {
        super(context,"sqliteTest.db",null,1);
    }

    @Override
    public void onCreate(sqliteDatabase sqliteDatabase) {
        String sql=
                "create table if not exists t_user("+
                        "id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,"+
                        "uname VARCHAR(255),"+
                        "upwd VARCHAR(255),"+
                        "isDel INTEGER DEFAULT 0"+
                        ")";
        sqliteDatabase.execsql(sql);
    }

    @Override
    public void onUpgrade(sqliteDatabase arg0,int arg1,int arg2) {
    }

}

UserDao:sql语句的编写


package example.com.user_sqlite.dao;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.sqliteDatabase;
import java.lang.reflect.Array;
import java.util.ArrayList;

import example.com.user_sqlite.entity.User;

/**  * Created by Administrator on 2017/3/19.  */ public class UserDao {
    private DBOpenHelper dbOpenHelper;      //创建DBOpenHelper对象
    private sqliteDatabase sqliteDatabase; //创建sqliteDatabase对象
    public UserDao(Context context){       //定义构造函数
        dbOpenHelper=new DBOpenHelper(context,0);  //初始化DBOpenHelper对象
    }
//插入数据
    public void dbInsert(String uname,String upwd){
        sqliteDatabase =dbOpenHelper.getWritableDatabase();
        String sql="insert into t_user(uname,upwd,isDel) values (?,?,0)";
        Object bindArgs[] = new Object[]{ uname,upwd};
        sqliteDatabase.execsql(sql,bindArgs);
    }
//查询数据
    public int dbGetUserSize(){
        sqliteDatabase = dbOpenHelper.getWritableDatabase();
        String sql="select count(*) from t_user where isDel=0";
        Cursor cursor = sqliteDatabase.rawQuery(sql,null);
        if (cursor.moveToNext())       //判断Cursor中是否有数据
        {
            return cursor.getInt(0);  //返回总记录数
        }
        return 0;                     //如果没有数据,则返回0
    }

    public User dbQueryOneByUsername(String uname){
        sqliteDatabase = dbOpenHelper.getWritableDatabase();
        String sql="select * from t_user where uname=? and isDel=0";
        String[] selectionArgs = new String[]{ uname };
        Cursor cursor = sqliteDatabase.rawQuery(sql,selectionArgs);
        if (cursor.moveToNext())  //判断Cursor中是否有数据
        {
            User user=new User();
            user.setId(cursor.getInt(cursor.getColumnIndex("id")));
            user.setUname(cursor.getString(cursor.getColumnIndex("uname")));
            user.setUpwd(cursor.getString(cursor.getColumnIndex("upwd")));
            return user; //返回总记录行数
        }
        return null;
    }
//修改密码
    public void dbUpdatePassword(String uname,String newUpwd){
        sqliteDatabase = dbOpenHelper.getWritableDatabase();
        String sql="update t_user set upwd=? where uname=? and isDel=0";
        Object bindArgs[] = new Object[]{ newUpwd,uname };
        sqliteDatabase.execsql(sql,bindArgs);
    }
//查询新增数
    public ArrayList<User> dbQueryAll(){
        ArrayList<User> userArrayList = new ArrayList<User>();
        sqliteDatabase = dbOpenHelper.getWritableDatabase();
        String sql="select * from t_user where isDel=0";
        Cursor cursor= sqliteDatabase.rawQuery(sql,null);
        for (cursor.moveToFirst(); !(cursor.isAfterLast()); cursor.moveToNext()){
            if (cursor.getInt(cursor.getColumnIndex("isDel"))!=1){
                User user=new User();
                user.setId(cursor.getInt(cursor.getColumnIndex("id")));
                user.setUname(cursor.getString(cursor.getColumnIndex("uname")));
                user.setUpwd(cursor.getString(cursor.getColumnIndex("upwd")));
                userArrayList.add(user);
            }
        }
        return userArrayList;
    }

//更新数据
    public void dbDeleteUser(int id){
        sqliteDatabase = dbOpenHelper.getWritableDatabase();
        String sql="update t_user set isDel=1 where id=?";
        Object bindArgs[] = new Object[]{ id };
        sqliteDatabase.execsql(sql,bindArgs);
    }

}

三、实现的方法

1、MainActivity


package example.com.user_sqlite;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
import android.app.Activity;
import android.content.Intent;

import example.com.user_sqlite.dao.UserDao;
import example.com.user_sqlite.entity.User;

public class MainActivity extends Activity {
    private ImageView bili1,bili2;
// 用户登录
    private EditText editTextA1;
    private EditText editTextA2;
    private Button buttonA1;

    // 数据库操作类
    private UserDao userDao;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

// 注册组件
        userDao = new UserDao(this);
        editTextA1 = (EditText) findViewById(R.id.editTextA1);
        editTextA2 = (EditText) findViewById(R.id.editTextA2);

        buttonA1 = (Button) findViewById(R.id.buttonA1);
        bili1=(ImageView) findViewById(R.id.bili1);
        bili2=(ImageView) findViewById(R.id.bili2);

// 用户登录
        buttonA1.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                String uname = editTextA1.getText() + "";
                String upwd = editTextA2.getText() + "";
//一个是输入不能为空,一个是不能只打空格null
                if (uname.equals(null) || uname == "" || upwd.equals(null) || upwd == "") {
                    Toast.makeText(MainActivity.this,"用户名或密码不得为空!",Toast.LENGTH_SHORT).show();
                } else {
                    User user = userDao.dbQueryOneByUsername(uname);
                    if (userDao.dbQueryOneByUsername(uname) == null) {
                        Toast.makeText(MainActivity.this,"此用户不存在!",Toast.LENGTH_SHORT).show();
                    } else {
                        if (!user.getUpwd().equals(upwd)) {
                            Toast.makeText(MainActivity.this,"密码错误!",Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(MainActivity.this,"登录成功!",Toast.LENGTH_SHORT).show();
                            Intent intent = new Intent(MainActivity.this,TableActivity.class);
                            startActivity(intent);
                        }
                    }
                }
            }
        });
//输入框 获取/失去 焦点切换图片
        editTextA1.setOnFocusChangeListener(new android.view.View.OnFocusChangeListener(){

            @Override
            public void onFocusChange(View v,boolean hasFocus) {
            //获取焦点的时候,图片显示图片二隐藏
                if (hasFocus){
                    bili1.setVisibility(v.VISIBLE);
                    bili2.setVisibility(v.GONE);
                }else {
                    bili2.setVisibility(v.VISIBLE);
                    bili1.setVisibility(v.GONE);
                }
            }
        });

    }
//注册
    public void Login(View view){
        Intent intent =  new Intent(this,LoginActivity.class);
        startActivity(intent);
    }
//查看列表信息
    public void Look(View view){
        Intent intent =  new Intent(this,TableActivity.class);
        startActivity(intent);
    }
}

1.1:activity_main.xml


<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp"
    android:background="@android:color/white"
    tools:context="example.com.user_sqlite.MainActivity">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
<!-- 用户登录: -->
    <ImageView
        android:id="@+id/bili1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/bi22"
        android:layout_gravity="center_horizontal"/>
    <ImageView
        android:id="@+id/bili2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/bi33"
        android:visibility="gone"
        android:layout_gravity="center_horizontal"/>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_marginTop="10dp">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/textViewA"
        android:textSize="24dp"
        android:gravity="center_horizontal"
        android:textColor="@android:color/holo_blue_light"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginRight="20dp"
        android:layout_marginTop="10dp">

        <TextView
            android:layout_width="80dp"
            android:gravity="center_vertical"
            android:layout_height="match_parent"
            android:text="@string/textView1"
            android:textSize="18dp"
            android:textColor="@android:color/holo_blue_light"/>

        <EditText
            android:id="@+id/editTextA1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:inputType="text"
            android:textSize="18dp"

            android:background="@drawable/editbg"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="10dp"
        android:layout_marginRight="20dp">

        <TextView
            android:layout_width="80dp"
            android:gravity="center_vertical"
            android:layout_height="match_parent"
            android:text="@string/textView2"
            android:textSize="18dp"
            android:textColor="@android:color/holo_blue_light"/>

        <EditText
            android:id="@+id/editTextA2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:inputType="textPassword"
            android:textSize="18dp"
            android:background="@drawable/editbg"/>
</LinearLayout>
    <Button
        android:id="@+id/buttonA1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button1"
        android:textSize="18sp"
        android:textColor="@color/chuise2"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="10dp"
        android:background="@drawable/buttonbg"/>
</LinearLayout>

<!--2个图片按钮 -->
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_marginTop="30dp">
    <ImageButton
        android:onClick="Login"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="100dp"
        android:scaleType="fitXY"
        android:src="@drawable/a2"
        android:background="#00000000"/>
    <ImageButton
        android:onClick="Look"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="100dp"
        android:scaleType="fitXY"
        android:src="@drawable/a3"
        android:background="#00000000"
        android:layout_marginLeft="30dp"/>
</LinearLayout>
</LinearLayout>

</ScrollView>

2、LoginActivity.java

注册方法:这里写了很多if else的判断,可能有点看晕,但是修改密码AlterActivity 更加晕呢

或许我应该有接口封装一来,有人跟我说:写成接口,然后用true,false这么判断,

可是也有人对我说:你这里的代码复用性不高,写成接口封装会造成负担.....

所以,我还没有封装呢....


package example.com.user_sqlite;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import example.com.user_sqlite.dao.UserDao;

public class LoginActivity extends AppCompatActivity {

    // 用户注册
    private EditText editTextB1;
    private EditText editTextB2;
    private EditText editTextB3;
    private Button buttonB1;

    // 数据库操作类
    private UserDao userDao;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        userDao = new UserDao(this);
        editTextB1 = (EditText) findViewById(R.id.editTextB1);
        editTextB2 = (EditText) findViewById(R.id.editTextB2);
        editTextB3 = (EditText) findViewById(R.id.editTextB3);

        buttonB1 = (Button) findViewById(R.id.buttonB1);
        //用户注册
        buttonB1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String uname = editTextB1.getText() + "";
                String upwd = editTextB2.getText() + "";
                if (uname.equals(null) || uname == "" || upwd.equals(null) || upwd == ""){
                    Toast.makeText(LoginActivity.this,Toast.LENGTH_SHORT).show();
                }else {
                    String confirmPwd = editTextB3.getText()+ "";
                if (!upwd.equals(confirmPwd)){
                    Toast.makeText(LoginActivity.this,"两次输入密码不一致",Toast.LENGTH_SHORT).show();
                }else {
                if (userDao.dbQueryOneByUsername(uname) == null){
                    userDao.dbInsert(uname,upwd);
                    Toast.makeText(LoginActivity.this,"注册成功!用户名:"+uname+
                            ",密码:"+upwd+",请牢记!",Toast.LENGTH_SHORT).show();
                    finish();
                    Intent intent = new Intent(LoginActivity.this,MainActivity.class);
                    startActivity(intent);
                    }else {
                        Toast.makeText(LoginActivity.this,"该用户已被注册",Toast.LENGTH_SHORT).show();
                    }
                    }
                }
            }
        });

    }
}

2.1、activity_login:注册页面


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="10dp"
    tools:context="example.com.user_sqlite.LoginActivity">

    <ImageView
        android:layout_width="100dp"
        android:layout_height="120dp"
        android:src="@drawable/a1"
        android:layout_gravity="center_horizontal"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/textViewB"
            android:textSize="24sp"
            android:gravity="center_horizontal"
            android:layout_marginTop="20dp"/>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <TextView
                android:layout_width="80dp"
                android:gravity="center_vertical"
                android:layout_height="match_parent"
                android:text="@string/textView1"
                android:textSize="18sp" />

            <EditText
                android:id="@+id/editTextB1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:inputType="text"
                android:textSize="18sp" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <TextView
                android:layout_width="80dp"
                android:gravity="center_vertical"
                android:layout_height="match_parent"
                android:text="@string/textView2"
                android:textSize="18sp" />

            <EditText
                android:id="@+id/editTextB2"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:inputType="textPassword"
                android:textSize="18sp" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <TextView
                android:layout_width="wrap_content"
                android:gravity="center_vertical"
                android:layout_height="match_parent"
                android:text="@string/textView3"
                android:textSize="18sp" />

            <EditText
                android:id="@+id/editTextB3"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:inputType="textPassword"
                android:textSize="18sp" />
        </LinearLayout>

        <Button
            android:id="@+id/buttonB1"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="@string/button1"
            android:textSize="18sp"
            android:textColor="@color/chuise"
            android:layout_gravity="center_horizontal"/>
    </LinearLayout>

</LinearLayout>

3、AlterActivity.java :修改密码

这里的if_else判断更多

package example.com.user_sqlite;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import example.com.user_sqlite.dao.UserDao;
import example.com.user_sqlite.entity.User;

public class AlterActivity extends AppCompatActivity {
    // 修改密码
    private EditText editTextC1;
    private EditText editTextC2;
    private EditText editTextC3;
    private EditText editTextC4;
    private Button buttonC1;

    // 数据库操作类
    private UserDao userDao;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_alter);

        userDao = new UserDao(this);
        editTextC1 = (EditText) findViewById(R.id.editTextC1);
        editTextC2 = (EditText) findViewById(R.id.editTextC2);
        editTextC3 = (EditText) findViewById(R.id.editTextC3);
        editTextC4 = (EditText) findViewById(R.id.editTextC4);

        buttonC1 = (Button) findViewById(R.id.buttonC1);

        //修改密码
        buttonC1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String uname=editTextC1.getText() + "";
                String upwd =editTextC2.getText() + "";

            if (uname.equals(null) || uname == "" || upwd.equals(null) || upwd == ""){
                Toast.makeText(AlterActivity.this,Toast.LENGTH_SHORT).show();
            }else {
                User user = userDao.dbQueryOneByUsername(uname);
                if (userDao.dbQueryOneByUsername(uname) == null) {
                    Toast.makeText(AlterActivity.this,Toast.LENGTH_SHORT).show();
                }else {
                    if (!user.getUpwd().equals(upwd)) {
                        Toast.makeText(AlterActivity.this,Toast.LENGTH_SHORT).show();
                    } else {
                        String newPwd = editTextC3.getText() + "";
                        String confirmNewPwd = editTextC4.getText() + "";

                        if (newPwd.equals(null) || newPwd == "") {
                            Toast.makeText(AlterActivity.this,"新密码不能为空!",Toast.LENGTH_SHORT).show();
                        } else {
                            if (!newPwd.equals(confirmNewPwd)) {
                                Toast.makeText(AlterActivity.this,"两次密码输入不一致!",Toast.LENGTH_SHORT).show();
                            } else {
                                userDao.dbUpdatePassword(uname,newPwd);
                                Toast.makeText(AlterActivity.this,"修改密码成功!新密码:" + newPwd + ",请牢记",Toast.LENGTH_SHORT).show();
                                finish();
                                Intent intent= new Intent(AlterActivity.this,MainActivity.class);
                                startActivity(intent);
                            }
                        }
                    }
                }
                }
            }
        });

    }
}

3.1、activity_alter.xml

修改密码页面

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="10dp"
    tools:context="example.com.user_sqlite.AlterActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/textViewC"
            android:textSize="24sp"
            android:textColor="@android:color/holo_blue_light"/>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <TextView
                android:layout_width="80dp"
                android:gravity="center_vertical"
                android:layout_height="match_parent"
                android:text="@string/textView1"
                android:textSize="18sp" />

            <EditText
                android:id="@+id/editTextC1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:inputType="text"
                android:textSize="18sp" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <TextView
                android:layout_width="80dp"
                android:gravity="center_vertical"
                android:layout_height="match_parent"
                android:text="@string/textView4"
                android:textSize="18sp" />

            <EditText
                android:id="@+id/editTextC2"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:inputType="textPassword"
                android:textSize="18sp" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <TextView
                android:layout_width="80dp"
                android:gravity="center_vertical"
                android:layout_height="match_parent"
                android:text="@string/textView5"
                android:textSize="18sp" />

            <EditText
                android:id="@+id/editTextC3"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:inputType="textPassword"
                android:textSize="18sp" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <TextView
                android:layout_width="wrap_content"
                android:gravity="center_vertical"
                android:layout_height="match_parent"
                android:text="@string/textView3"
                android:textSize="18sp" />

            <EditText
                android:id="@+id/editTextC4"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:inputType="textPassword"
                android:textSize="18sp" />
        </LinearLayout>

        <Button
            android:id="@+id/buttonC1"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="@string/button1"
            android:textSize="18sp"
            android:textColor="@color/chuise"
            android:layout_gravity="center_horizontal"
            android:background="@drawable/buttonbg"
            android:layout_marginTop="10dp"/>
    </LinearLayout>

</LinearLayout>

4、TableActivity.java

代码布局,输出显示注册用户


package example.com.user_sqlite;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

import org.w3c.dom.Text;

import java.util.ArrayList;

import example.com.user_sqlite.dao.UserDao;
import example.com.user_sqlite.entity.User;

public class TableActivity extends Activity {
//数据库操作类
    private UserDao userDao;
    private LinearLayout linearLayout1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_table);

//代码布局,输出注册用户信息
        userDao = new UserDao(this);
        linearLayout1 = (LinearLayout) findViewById(R.id.linearLayout1);

        int userSize = userDao.dbGetUserSize();
        TextView textView1 = new TextView(this);
        textView1.setText("共有"+userSize+ "个用户");
        textView1.setTextSize(24);
        linearLayout1.addView(textView1,1);

        if (userSize>0){
            ArrayList<User> userList = userDao.dbQueryAll();
            TableLayout tableLayout1 = new TableLayout(this);
            tableLayout1.setStretchAllColumns(true);
            TableRow tableRow = new TableRow(this);
            TextView textView = new TextView(this);
            textView.setTextSize(24);
            textView.setText("用户名");
            tableRow.addView(textView);

            textView= new TextView(this);
            textView.setTextSize(24);
            textView.setText("密码");
            tableRow.addView(textView);

            textView = new TextView(this);
            textView.setTextSize(24);
            textView.setText("操作");
            tableRow.addView(textView);

//新建的TableRow添加到TableLayout
            tableLayout1.addView(tableRow,new TableLayout.LayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT,                    ViewGroup.LayoutParams.MATCH_PARENT));
            for (int i=0;i<userSize;i++){
                User user = userList.get(i);
//一个用户占据一行
                tableRow = new TableRow(this);
                textView = new TextView(this);
                textView.setTextSize(18);
                textView.setText(user.getUname());
                tableRow.addView(textView);

                textView = new TextView(this);
                textView.setTextSize(18);
                textView.setText(user.getUpwd());
                tableRow.addView(textView);

                Button button = new Button(this);
                button.setText("删除");
                button.setTextSize(18);
                button.setId(user.getId());
                button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    userDao.dbDeleteUser(v.getId());
                    finish();
                    Intent intent = new Intent(TableActivity.this,TableActivity.class);
                    startActivity(intent);
                    Toast.makeText(TableActivity.this,"删除成功!",Toast.LENGTH_SHORT).show();
                }
            });
                tableRow.addView(button);
//新建的TableRow添加到TableLayout
                tableLayout1.addView(tableRow,new TableLayout.LayoutParams(
                        ViewGroup.LayoutParams.WRAP_CONTENT,                        ViewGroup.LayoutParams.MATCH_PARENT));
            }
            linearLayout1.addView(tableLayout1,2);
        }

    }
//对物理按钮的监听
    public boolean onKeyDown(int keycode,KeyEvent event){
        switch (keycode){
            case KeyEvent.KEYCODE_BACK:
                finish(); //关闭这个Activity
                Intent intent = new Intent(TableActivity.this,MainActivity.class);
                startActivity(intent);
                break;
        }
                return super.onKeyDown(keycode,event);
    }
//登录跳转
    public void dengLu(View v){
        finish();
        Intent intent = new Intent(this,MainActivity.class);
        startActivity(intent);
    }

//注册跳转
    public void Login(View v){
        finish();
        Intent intent = new Intent(this,LoginActivity.class);
        startActivity(intent);
    }
    //修改跳转
    public void Alter(View v){
        finish();
        Intent intent = new Intent(this,AlterActivity.class);
        startActivity(intent);
    }
}

4.1、activity_table:xml

输出信息页面,因为是代码布局的,所以有点丑....


<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp"
    android:orientation="vertical">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
<!-- 注册用户显示 -->
    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <View
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:tag="1" />

        <View
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:tag="2" />

    </LinearLayout>

 <!-- 三个按钮 -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="@string/textViewA"
            android:textColor="@color/chuise"
            android:background="@drawable/buttonbg"
            android:onClick="dengLu" />

        <Button
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="@string/textViewB"
            android:textColor="@color/chuise"
            android:background="@drawable/buttonbg"
            android:layout_marginLeft="10dp"
            android:onClick="Login"/>
        <Button
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="@string/textViewC"
            android:textColor="@color/chuise"
            android:background="@drawable/buttonbg"
            android:layout_marginLeft="10dp"
            android:onClick="Alter"/>

    </LinearLayout>
</LinearLayout>
</ScrollView>

有时候我会用很多finish(); 大家自行决定要不要加吧.....


四、drwable资源文件

1、buttonbg.xml

按钮的样式

<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <!-- 描边 -->
    <stroke android:width="0.5dp"  android:color="@android:color/holo_blue_light"/>
    <!-- 填充 -->
    <!--<solid android:color=""/>-->

    <!-- 圆角 -->
    <corners android:radius="2dp"/>

</shape>

2、editbg.xml

输入框的样式:

<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <!-- 描边 -->
    <stroke android:width="0.5dp"  android:color="@android:color/holo_blue_light"/>
    <!-- 填充 -->
    <!--<solid android:color=""/>-->

    <!-- 圆角 -->
    <corners android:radius="5dp"/>

    <!-- 内容与边框间距 -->
    <padding android:left="10dp" android:top="5dp"
        android:right="0dp" android:bottom="0dp"/>

</shape>

至于图片啊....大家自己找吧:



五、values属性文件

colors自己写

strings.xml:

<resources>
    <string name="app_name">sqlite数据库的增删改查</string>
    <string name="action_settings">Settings</string>
    <string name="textViewA">用户登录</string>
    <string name="textViewB">用户注册</string>
    <string name="textViewC">修改密码</string>

    <string name="textView1">用户名</string>
    <string name="textView2">密  码 :</string>
    <string name="textView3">确认密码:</string>
    <string name="textView4">旧密码:</string>
    <string name="textView5">新密码:</string>

    <string name="button1">确定</string>
    <string name="button2">查看用户</string>
    <string name="button3">返回</string>

</resources>


对于这个简单的项目,其实还有个致命的缺陷呢!

就是非当前用户也可以修改密码,这里大家多做一个判断

原文链接:https://www.f2er.com/sqlite/198482.html

猜你在找的Sqlite相关文章