Android-LitePal详细介绍

官方文档:https://github.com/LitePalFramework/LitePal

运行截图:

 

 关键代码:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button button1;
    private Button button2;
    private Button button3;
    private Button button4;
    private Button button5;

    private TextView textView;

    private List<Student> list;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        LitePal.initialize(this);
        init();
    }

    private void init() {
        button1 = findViewById(R.id.button_1);
        button2 = findViewById(R.id.button_2);
        button3 = findViewById(R.id.button_3);
        button4 = findViewById(R.id.button_4);
        button5 = findViewById(R.id.button_5);
        button1.setOnClickListener(this);
        button2.setOnClickListener(this);
        button3.setOnClickListener(this);
        button4.setOnClickListener(this);
        button5.setOnClickListener(this);
        textView = findViewById(R.id.text);
        list = new ArrayList<>();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //创建数据库
            case R.id.button_1:
                LitePal.getDatabase();
                break;

            //增加数据
            case R.id.button_2:
                Student student1 = new Student("Leo_1", "123", "456");
                Student student2 = new Student("Leo_2", "321", "654");
                student1.save();
                student2.save();
                break;

            //删除数据
            case R.id.button_3:
                LitePal.deleteAll(Student.class, "phone = ?", "321");
                break;

            //查询数据
            case R.id.button_4:
                list = LitePal.findAll(Student.class);
                disPlay();
                break;

            //修改数据
            case R.id.button_5:
                Student student3 = new Student();
                student3.setName("修改后的名字");
                student3.updateAll("name = ?", "Leo_1");
                break;
        }
    }

    //    将查询到的数据显示在TextView
    private void disPlay() {
        StringBuffer stringBuffer = new StringBuffer();
        for (Student student : list) {
            stringBuffer.append(student.getName() + "    " + student.getPhone() + "    " + student.getQq() + "\n");
        }
        textView.setText(stringBuffer.toString());
    }
}

源码:https://download.csdn.net/download/qq_41113081/10940796

  • 0
    点赞
  • 0
    评论
  • 1
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

打赏
文章很值,打赏犒劳作者一下
相关推荐
©️2020 CSDN 皮肤主题: 点我我会动 设计师:白松林 返回首页

打赏

小菜粥

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者