1、建数据库 MyDatabaseHelper.java
public class MyDatabaseHelper extends SQLiteOpenHelper {
/*integer 整型,real 浮点型,primary key 主键,autoincrement 自增长,text 文本类型,blob 二进制数,*/
public static final String CREATE_BOOK="create table Book("
+"id integer primary key autoincrement,"
+"author text,"
+"price real,"
+"pages integer,"+
"name text)";
public static final String CREATE_CATEGORY="create table Category("
+"id integer primary key autoincrement,"
+"category_name text,"
+"category_code integer)";
private Context mContext;
public MyDatabaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
mContext=context;
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL(CREATE_BOOK);
sqLiteDatabase.execSQL(CREATE_CATEGORY);
Toast.makeText(mContext,"create success!",Toast.LENGTH_SHORT).show();
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
sqLiteDatabase.execSQL("drop table if exists Book");
sqLiteDatabase.execSQL("drop table if exists Category");
onCreate(sqLiteDatabase);
}
}
2、启动页面+布局文件 MainAtivity.java+activity_main.xml
public class MainActivity extends AppCompatActivity {
private Button bt1,bt2,bt3,bt4,bt5;
private MyDatabaseHelper databaseHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
databaseHelper=new MyDatabaseHelper(this,"BookStore.db",null,2);
setContentView(R.layout.activity_main);
bt1=(Button)findViewById(R.id.Create_button);
bt1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
databaseHelper.getWritableDatabase();//先检测程序中有无这个数据库,如果没有,则创建
}
});
bt2=(Button)findViewById(R.id.Add_button);
bt2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SQLiteDatabase db=databaseHelper.getWritableDatabase();
ContentValues values=new ContentValues();//获取ContentValues对象
values.put("name","Code1");
values.put("author","Dan");
values.put("pages",1);
values.put("price",46.69);
db.insert("Book",null,values);
values.clear();
values.put("name","Code2");
values.put("author","Dan");
values.put("pages",2);
values.put("price",46.69);
db.insert("Book",null,values);
values.clear();
values.put("name","Symbol1");
values.put("author","Lost");
values.put("pages",5);
values.put("price",82.65);
db.insert("Book",null,values);
values.clear();
values.put("name","Symbol2");
values.put("author","Lost");
values.put("pages",6);
values.put("price",82.65);
db.insert("Book",null,values);
values.clear();
values.put("name","Symbol3");
values.put("author","Lost");
values.put("pages",7);
values.put("price",82.65);
db.insert("Book",null,values);
values.clear();
values.put("category_name","computer");
values.put("category_code",156);
db.insert("Category",null,values);
values.clear();
values.put("category_name","animal");
values.put("category_code",456);
db.insert("Category",null,values);
}
});
bt3=(Button)findViewById(R.id.Update_button);
bt3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//修改价格
SQLiteDatabase db=databaseHelper.getWritableDatabase();
ContentValues values=new ContentValues();
values.put("price",10.99);
db.update("Book",values,"name=?",new String[]{"The Da Code"});
}
});
bt4=(Button)findViewById(R.id.Delete_button);
bt4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//删除页数>500的书
SQLiteDatabase db=databaseHelper.getWritableDatabase();
db.delete("Book","pages > ?",new String[]{"500"});//删除页数超过500的书
}
});
bt5=(Button)findViewById(R.id.Query_button);
bt5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// SQLiteDatabase db=databaseHelper.getWritableDatabase();//获取数据库对象
// Cursor cursor=db.query("Book",null,null,null,null,null,null);//查询Book表中的所有数据
// if(cursor.moveToFirst()){
// do{
// String name=cursor.getString(cursor.getColumnIndex("name"));
// String author=cursor.getString(cursor.getColumnIndex("author"));
// int pages=cursor.getInt(cursor.getColumnIndex("pages"));
// double price=cursor.getDouble(cursor.getColumnIndex("price"));
// Log.d("MainActivity","Book name is:"+name);
// Log.d("MainActivity","Book author is:"+author);
// Log.d("MainActivity","Book page is:"+pages);
// Log.d("MainActivity","Book price is:"+price);
// }while(cursor.moveToNext());
// }
// cursor.close();
Intent intent=new Intent(MainActivity.this,ShowDataActivity.class);
startActivity(intent);
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/Create_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="创建数据库"
/>
<Button
android:id="@+id/Add_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="添加数据"
/>
<Button
android:id="@+id/Update_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="更新数据"/>
<Button
android:id="@+id/Delete_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="删除数据"/>
<Button
android:id="@+id/Query_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="查询数据"/>
</LinearLayout>
3、javaBean BookStore.java
public class BookStore {
private int id;
private String name;
private String author;
private int pages;
private double price;
private String category_name;
private int category_code;
public BookStore(int id, String name, String author, int pages, double price,String category_name,int category_code){
this.id=id;
this.name=name;
this.author=author;
this.pages=pages;
this.price=price;
this.category_name=category_name;
this.category_code=category_code;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public String getAuthor() {
return author;
}
public int getPages() {
return pages;
}
public double getPrice() {
return price;
}
public String getCategory_name() {
return category_name;
}
public int getCategory_code() {
return category_code;
}
}
ListViewAdapter和item BookAdapter.java和book_item.xml
public class BookAdapter extends ArrayAdapter<BookStore> {
private int resourceId;
private List<BookStore> bookList;
// private Context context;
// int VIEW_COUNT = 7;//每页显示个数
// int index = 0;//用于显示页号的索引
public BookAdapter(Context context, int resource,List<BookStore> objects) {
super(context, resource,objects);
// this.context=context;
this.resourceId=resource;
this.bookList=objects;
}
public View getView(int position, View convertView,ViewGroup parent) {
BookStore bs=getItem(position);//获取当前项的book实例
View view;
ViewHolder viewHolder;
if(convertView == null){
view = LayoutInflater.from(getContext()).inflate(resourceId,parent,false);
viewHolder=new ViewHolder();
viewHolder.book_id=(TextView)view.findViewById(R.id.book_id);
viewHolder.book_name=(TextView)view.findViewById(R.id.book_name);
viewHolder.book_author=(TextView)view.findViewById(R.id.book_author);
viewHolder.book_pages=(TextView)view.findViewById(R.id.book_pages);
viewHolder.book_price=(TextView)view.findViewById(R.id.book_price);
viewHolder.category_name=(TextView)view.findViewById(R.id.category_name);
viewHolder.category_code=(TextView)view.findViewById(R.id.category_code);
view.setTag(viewHolder);//将ViewHolder存储在View中
}else{
view = convertView;
viewHolder=(ViewHolder) view.getTag();//重新获取ViewHolder
}
// viewHolder.book_id.setText(String.valueOf(bs.getId()));
// viewHolder.book_name.setText(bs.getName());
// viewHolder.book_author.setText(bs.getAuthor());
// viewHolder.book_pages.setText(String.valueOf(bs.getPages()));
// viewHolder.book_price.setText(String.valueOf(bs.getPrice()));
viewHolder.book_id.setText((String.valueOf(bookList.get(position + index * VIEW_COUNT).getId())));
viewHolder.book_name.setText(bookList.get(position + index * VIEW_COUNT).getName());
viewHolder.book_author.setText(bookList.get(position + index * VIEW_COUNT).getAuthor());
viewHolder.book_pages.setText((String.valueOf(bookList.get(position + index * VIEW_COUNT).getPages())));
viewHolder.book_price.setText((String.valueOf(bookList.get(position + index * VIEW_COUNT).getPrice())));
viewHolder.category_name.setText(bookList.get(position + index * VIEW_COUNT).getCategory_name());
viewHolder.category_code.setText((String.valueOf(bookList.get(position + index * VIEW_COUNT).getCategory_code())));
return view;
}
class ViewHolder{
TextView book_id;
TextView book_name;
TextView book_author;
TextView book_pages;
TextView book_price;
TextView category_name;
TextView category_code;
}
//设置每行个数
public int getCount() {
int ori = VIEW_COUNT*index;
if(bookList.size()-ori<VIEW_COUNT){
return bookList.size()-ori;
}else{
return VIEW_COUNT;
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<!--ListView Adapter布局文件-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<TextView
android:id="@+id/book_id"
android:gravity="center"
android:layout_weight="1"
android:text="编号"
android:layout_width="0dp"
android:layout_height="50dp" />
<TextView
android:id="@+id/book_name"
android:gravity="center"
android:layout_weight="1"
android:text="书名"
android:layout_width="0dp"
android:layout_height="50dp" />
<TextView
android:id="@+id/book_author"
android:gravity="center"
android:layout_weight="1"
android:text="作者"
android:layout_width="0dp"
android:layout_height="50dp" />
<TextView
android:id="@+id/book_pages"
android:gravity="center"
android:layout_weight="1"
android:text="页数"
android:layout_width="0dp"
android:layout_height="50dp" />
<TextView
android:id="@+id/book_price"
android:gravity="center"
android:layout_weight="1"
android:text="价格"
android:layout_width="0dp"
android:layout_height="50dp" />
<TextView
android:id="@+id/category_name"
android:gravity="center"
android:layout_weight="1.5"
android:text="种类"
android:layout_width="0dp"
android:layout_height="50dp" />
<TextView
android:id="@+id/category_code"
android:gravity="center"
android:layout_weight="1"
android:text="编号"
android:layout_width="0dp"
android:layout_height="50dp" />
</LinearLayout>
数据显示活动界面+布局文件 ShowDataActivity.java和datalayout.xml
public class ShowDataActivity extends AppCompatActivity implements View.OnClickListener{
List<BookStore> bookList;
Button bt1,bt2;
static int VIEW_COUNT = 7;//每页显示个数
static int index = 0;//用于显示页号的索引
BookAdapter adapter;
ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.datalayout);
listView = (ListView) findViewById(R.id.book_lv);
initBooks();//添加数据
adapter = new BookAdapter(ShowDataActivity.this, R.layout.book_item, bookList);
listView.setAdapter(adapter);
bt1=(Button)findViewById(R.id.back_button);
bt2=(Button)findViewById(R.id.next_button);
bt1.setOnClickListener(this);
bt2.setOnClickListener(this);
checkButton();
}
//按钮监听
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.back_button:
leftView();
break;
case R.id.next_button:
rightView();
break;
}
}
//右边按钮点击事件
private void rightView(){
index=index+1;
adapter.notifyDataSetChanged();
checkButton();
}
//左边按钮点击事件
private void leftView(){
index=index-1;
adapter.notifyDataSetChanged();
checkButton();
}
//判断按钮是否可用
private void checkButton(){
if(index <= 0){
bt1.setEnabled(false);
}else if(bookList.size()-index * VIEW_COUNT <= VIEW_COUNT){
bt2.setEnabled(false);
}else{
bt1.setEnabled(true);
bt2.setEnabled(true);
}
}
扫描数据库,将信息放入bookList
private void initBooks() {
String sqlSelect="SELECT * FROM Book INNER JOIN Category ON Book.id=Category.id ";
bookList=new ArrayList<>();
bookList.clear();
MyDatabaseHelper mdb = new MyDatabaseHelper(this, "BookStore.db", null, 2);//打开数据库
SQLiteDatabase sd = mdb.getReadableDatabase();//获取数据库
Cursor cursor=sd.rawQuery(sqlSelect,new String[]{});//查询Book表中的所有数据
if(cursor.moveToFirst()){
do{
int id=cursor.getInt(cursor.getColumnIndex("id"));
String name=cursor.getString(cursor.getColumnIndex("name"));
String author=cursor.getString(cursor.getColumnIndex("author"));
int pages=cursor.getInt(cursor.getColumnIndex("pages"));
double price=cursor.getDouble(cursor.getColumnIndex("price"));
String category_name=cursor.getString(cursor.getColumnIndex("category_name"));
int category_code=cursor.getInt(cursor.getColumnIndex("category_code"));
BookStore bookStore=new BookStore(id,name,author,pages,price,category_name,category_code);
bookList.add(bookStore);
}while (cursor.moveToNext());
}
cursor.close();
}
}
<?xml version="1.0" encoding="utf-8"?>
<!--ListView布局文件-->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_alignParentTop="true"
android:gravity="center_horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<include layout="@layout/book_item"/>
<TextView
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@android:color/background_dark"/>
<ListView
android:id="@+id/book_lv"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:gravity="center_horizontal"
android:layout_alignParentBottom="true">
<Button
android:id="@+id/back_button"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="上一页" />
<Button
android:id="@+id/next_button"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="下一页" />
</LinearLayout>
</RelativeLayout>
运行界面: