Android-LitePal详细介绍

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

运行截图:

Android-LitePal详细介绍_第1张图片

 

Android-LitePal详细介绍_第2张图片

Android-LitePal详细介绍_第3张图片

 关键代码:

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 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

你可能感兴趣的:(Android)