马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
修改Book.javapackage com.example.xinwei.mylitepal;
import org.litepal.crud.DataSupport;
/**
* Created by xinwei on 2017/10/21.
*/
public class Book extends DataSupport{
private int id;
private String author;
private double price;
private int pages;
private String name;
private String press;
public String getPress() {
return press;
}
public void setPress(String press) {
this.press = press;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getPages() {
return pages;
}
public void setPages(int pages) {
this.pages = pages;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
修改这个是因为前面有点字母拼错了
然后修改mainactivity.javapackage com.example.xinwei.mylitepal;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import org.litepal.crud.DataSupport;
import org.litepal.tablemanager.Connector;
import java.util.List;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button createDatabase = (Button)findViewById(R.id.create_database);
createDatabase.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Connector.getDatabase();
}
});
addData();
updateData();
deleteData();
queryData();
}
private void queryData() {
Button queryData=(Button)findViewById(R.id.query_data);
queryData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
List<Book> books = DataSupport.findAll(Book.class);
for (Book book:books){
Log.d("MainActivity","Book's name is"+book.getName());
Log.d("MainActivity","Book's author is"+book.getAuthor());
Log.d("MainActivity","Book's pages is"+book.getPages());
Log.d("MainActivity","Book's price is"+book.getPrice());
Log.d("MainActivity","Book's press is"+book.getPress());
}
}
});
}
private void deleteData() {
Button deleteData = (Button)findViewById(R.id.delete_data);
deleteData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
DataSupport.deleteAll(Book.class,"price<?","15");
}
});
}
private void updateData() {
Button updateData = (Button)findViewById(R.id.update_data);
updateData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Book book = new Book();
book.setPrice(14.95);
book.setPress("Anchor");
book.updateAll("name=?","The Lost Symbol");
}
});
}
private void addData() {
Button addData = (Button)findViewById(R.id.add_data);
addData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Book book = new Book();
book.setName("The Da Vinci Code");
book.setAuthor("Dan Brown");
book.setPages(454);
book.setPrice(16.96);
book.setPress("Unknow");
book.save();
}
});
}
}
updateData(),deleteData(),queryData()这3个函数分别实现了更新数据,删除数据和查询数据的功能,都非常简单。 |