LitePal has written a previous article. It uses the version 1.6 of LitePal. I saw version 2.0 these days. In fact, there is no big change in version 2.0. Its basic usage is similar to that of 1.6. If you want to know the usage of version 1.6, you can visit: https://blog.csdn.net/qq_40205116/article/details/88741724 . After that, the data displayed by listview has not been collated before, so it will be put together here.
Let's talk about the basic usage of listpal 2.0.
Newly added:
Student model = new Student(); model.setUsername(username1); model.setName(name1); model.setAge(age1); //Add to model.save();
Delete:
LitePal.delete(Student.class, id); //Class name, id value
Search:
Student stu = LitePal.find(Student.class, model.getId()); //Class name, find id
Amendment:
//First query the data, then change the data, Student stu = LitePal.find(Student.class, 1); stu.setUsername("admin"); stu.save(); //Update data for the specified id Student stu = new Student(); stu.setUsername("admin"); stu.update(id); //Update all data Student stu = new Student(); stu.setUsername("admin"); stu.updateAll("name = ?", "Zhang San");
Here is a specific example:
Let's take a look at the renderings:
Basic functions, add, delete, modify, click student information, jump to the modify delete page.
Project directory:
Let's take a look at the code (with comments):
android studio can directly add dependency in build.gradl
dependencies { compile 'org.litepal.android:core:2.0.0' }
eclipse can be downloaded at the following URL.
Litepal 2.0 download address: https://github.com/LitePalFramework/LitePal/tree/master/downloads
To configure the literal.xml file:
<?xml version="1.0" encoding="UTF-8"?> <litepal> <!-- Database name --> <dbname value="data"></dbname> <!-- Version number --> <version value="1"></version> <!-- Table name, entity class name --> <list> <mapping class="com.example.listviewdemo01.model.Student"></mapping> </list> </litepal>
Register in Android manifest.xml
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/LinearLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:layout_margin="20dp" tools:context="${relativePackage}.${activityClass}" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="4" android:gravity="center" android:text="Student ID:" /> <EditText android:id="@+id/username" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Student ID" android:inputType="number" android:layout_weight="1"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="4" android:gravity="center" android:text="Full name:" /> <EditText android:id="@+id/name" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Full name" android:inputType="text" android:layout_weight="1"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="4" android:gravity="center" android:text="Age:" /> <EditText android:id="@+id/age" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Age" android:inputType="number" android:layout_weight="1"/> </LinearLayout> <Button android:id="@+id/sub" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="18dp" android:text="Add to"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:textSize="20dp" android:layout_marginTop="20dp" android:text="All students"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:id="@+id/username1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:textSize="20dp" android:text="Student ID"/> <TextView android:id="@+id/name1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:textSize="20dp" android:text="Full name"/> <TextView android:id="@+id/age1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:textSize="20dp" android:text="Age"/> </LinearLayout> <ListView android:id="@+id/findAll" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:layout_marginBottom="10dp"/> </LinearLayout>
Design sketch:
item.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <TextView android:id="@+id/tv_username" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:textSize="15dp"/> <TextView android:id="@+id/tv_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:textSize="15dp"/> <TextView android:id="@+id/tv_age" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:textSize="15dp"/> <!-- <Button android:id="@+id/del" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:text="delete"/> --> </LinearLayout>
Student.java inherits litepalmsupport and implements the Serializable class.
package com.example.listviewdemo01.model; import java.io.Serializable; import org.litepal.crud.LitePalSupport; public class Student extends LitePalSupport implements Serializable { private Integer id; private String username; private String name; private Integer age; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Student(Integer id, String username, String name, Integer age) { super(); this.id = id; this.username = username; this.name = name; this.age = age; } public Student() { super(); } }
activity_edit.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/LinearLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:layout_margin="20dp" tools:context="${relativePackage}.${activityClass}" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="4" android:gravity="center" android:text="Student ID:" /> <EditText android:id="@+id/username" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Student ID" android:inputType="number" android:layout_weight="1"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="4" android:gravity="center" android:text="Full name:" /> <EditText android:id="@+id/name" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Full name" android:inputType="text" android:layout_weight="1"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="4" android:gravity="center" android:text="Age:" /> <EditText android:id="@+id/age" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Age" android:inputType="number" android:layout_weight="1"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="Preservation" /> <Button android:id="@+id/button2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="delete" /> </LinearLayout> </LinearLayout>
EditActivity.java
package com.example.listviewdemo01; import org.litepal.LitePal; import com.example.listviewdemo01.model.Student; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; public class EditActivity extends Activity implements OnClickListener { private EditText username; private EditText name; private EditText age; private Button button1; private Button button2; private Student model = new Student(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_edit); username = (EditText) findViewById(R.id.username); name = (EditText) findViewById(R.id.name); age = (EditText) findViewById(R.id.age); button1 = (Button) findViewById(R.id.button1); button2 = (Button) findViewById(R.id.button2); button1.setOnClickListener(this); button2.setOnClickListener(this); Intent intent = getIntent(); //Take out the parameters of the request binding Bundle bundle = intent.getExtras(); model = (Student) bundle.get("model"); name.setText(model.getName()); username.setText(model.getUsername()); try { age.setText(model.getAge()+""); } catch (Exception e) { age.setText(""); } } @Override public void onClick(View v) { switch (v.getId()) { case R.id.button1: //Focus of attention //edit1.requestFocus(); String name1 = name.getText().toString(); String username1 = username.getText().toString(); Integer age1 = Integer.parseInt(age.getText().toString()); if(model.getId() != null){ System.out.println(model.getId()); model = LitePal.find(Student.class, model.getId()); } model.setName(name1); model.setUsername(username1); model.setAge(age1); model.save(); finish(); break; case R.id.button2: Integer id = model.getId(); //delete LitePal.delete(Student.class, id); finish(); break; default: break; } } }
MainActivity.java
package com.example.listviewdemo01; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import org.litepal.LitePal; import com.example.listviewdemo01.model.Student; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.ListView; import android.widget.SimpleAdapter; public class MainActivity extends Activity implements OnClickListener, OnItemClickListener { private Button sub; private ListView list; private EditText username; private EditText name; private EditText age; private Student model = new Student(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //New database LitePal.getDatabase(); list = (ListView) findViewById(R.id.findAll); sub = (Button) findViewById(R.id.sub); username = (EditText) findViewById(R.id.username); name = (EditText) findViewById(R.id.name); age = (EditText) findViewById(R.id.age); sub.setOnClickListener(this); list.setOnItemClickListener(this); fun(); } @Override protected void onResume() { fun(); super.onResume(); } private void fun() { //Linked database SimpleAdapter adapter = getAdapter(); list.setAdapter(adapter); } private SimpleAdapter getAdapter() { List<Student> stu = LitePal.findAll(Student.class); List<HashMap<String, Object>> list1 = new ArrayList<HashMap<String, Object>>(); //Put the data in the map for(Student s : stu){ HashMap<String, Object> m = new HashMap<String, Object>(); m.put("id", s.getId()); m.put("username", s.getUsername()); m.put("name", s.getName()); m.put("age", s.getAge()); list1.add(m); } SimpleAdapter adapter = new SimpleAdapter( //container this, //data list1, //xml file location R.layout.item, //Field name new String[]{"username", "name", "age"}, //Field name in xml file new int[]{R.id.tv_username, R.id.tv_name, R.id.tv_age} ); return adapter; } @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { HashMap<String, Object> m = (HashMap<String, Object>) parent.getItemAtPosition(position); //Remove id value Integer pid = (Integer) m.get("id"); Intent intent = new Intent(this, EditActivity.class); //Find by id Student model = LitePal.find(Student.class, pid); intent.putExtra("model", model); startActivity(intent); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.sub: String username1 = username.getText().toString(); String name1 = name.getText().toString(); Integer age1 = Integer.parseInt(age.getText().toString()); model.setUsername(username1); model.setName(name1); model.setAge(age1); username.setText(""); name.setText(""); age.setText(""); //Add to model.save(); model = new Student(); break; } fun(); } }