目录结构如下:
java代码>> Book.java BookActivity.java BookAdapter.java MainActivity.java MyDatabaseHelper.java UpdateBookActivity.javalayout>> activity_book.xml activity_main.xml activity_update_book.xml book_item.xml
Book.java
package com.example.p229;import java.io.Serializable;public class Book implements Serializable{ private int ID; private String author; private double price; private int pages; private String name; public Book(int ID,String author,double price,int pages,String name) { super(); this.ID = ID; this.author = author; this.price=price; this.pages = pages; this.name = name; } public Book(String author,String name) { super(); this.author = author; this.price=price; this.pages = pages; this.name = name; } public Book() { super(); } 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 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; } public double getPrice() { return price; } public voID setPrice(double price) { this.price = price; }}
BookActivity
package com.example.p229;import androID.content.DialogInterface;import androID.content.Intent;import androID.database.Cursor;import androID.database.sqlite.sqliteDatabase;import androID.os.Bundle;import androID.preference.DialogPreference;import androID.support.v7.app.AlertDialog;import androID.support.v7.app.AppCompatActivity;import androID.support.v7.Widget.buttonbarLayout;import androID.util.Log;import androID.vIEw.VIEw;import androID.Widget.AdapterVIEw;import androID.Widget.button;import androID.Widget.ListVIEw;import androID.Widget.Toast;import java.util.ArrayList;import java.util.List;public class BookActivity extends AppCompatActivity { private List<Book> bookList = new ArrayList<>(); private ListVIEw lv_book; private MyDatabaseHelper dbhelper; private button data_clear; private BookAdapter adapter; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_book); dbhelper = new MyDatabaseHelper(this,"BookStore.db",null,1); //给数据源赋值 initBooks(); //创建适配器 //获取列表并设置适配器 lv_book = (ListVIEw) findVIEwByID(R.ID.lv_book); if (adapter == null) { adapter = new BookAdapter(BookActivity.this,R.layout.book_item,bookList); lv_book.setAdapter(adapter); } else { adapter.notifyDataSetChanged(); } //短时间选中该书触发的事件———d出修改对话框 lv_book.setonItemClickListener(new AdapterVIEw.OnItemClickListener() { @OverrIDe public voID onItemClick(AdapterVIEw<?> parent,VIEw vIEw,int position,long ID) { Book book = bookList.get(position); showBookInfoDialog(book); // Toast.makeText(BookActivity.this,book.getname(),Toast.LENGTH_SHORT).show(); } }); //长时间选中该书触发的事件———d出删除对话框 lv_book.setonItemLongClickListener(new AdapterVIEw.OnItemLongClickListener() { @OverrIDe public boolean onItemLongClick(AdapterVIEw<?> parent,long ID) { Book book = bookList.get(position); //Toast.makeText(BookActivity.this,Toast.LENGTH_SHORT).show(); showDeleteInfoDialog(book); return false; } }); data_clear = (button) findVIEwByID(R.ID.data_clear); data_clear.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { clear(); } }); } @OverrIDe protected voID onResume() { super.onResume(); initBooks(); } protected voID showBookInfoDialog(final Book book) { AlertDialog.Builder dialog = new AlertDialog.Builder(this); dialog.setIcon(R.mipmap.ic_launcher); dialog.setTitle("图书信息"); dialog.setMessage("" + "ID:" + book.getID() + "\n作者:" + book.getAuthor() + "\n单价:" + book.getPrice() + "\n页数:" + book.getPages() + "\n书名:" + book.getname() ); dialog.setPositivebutton("修改",new DialogInterface.OnClickListener() { @OverrIDe public voID onClick(DialogInterface dialog,int which) { //由当前活动跳转到UpdateBookActivity活动,病传递数据book Intent intent=new Intent(BookActivity.this,UpdateBookActivity.class); intent.putExtra("book",book); startActivity(intent); } }); dialog.setNegativebutton("取消",null); dialog.show(); } protected voID showDeleteInfoDialog(final Book book) { AlertDialog.Builder dialog = new AlertDialog.Builder(this); dialog.setIcon(R.mipmap.ic_launcher); dialog.setTitle("提示信息"); dialog.setMessage("请问您要删除这条数据吗"); dialog.setPositivebutton("确认",new DialogInterface.OnClickListener() { @OverrIDe public voID onClick(DialogInterface dialog,int which) { //删除图书,首先从数据课表中删除,其次从集合中删除,目的就是让lv能同步显示删除后的状态或结果 sqliteDatabase db=dbhelper.getWritableDatabase(); int count=db.delete("Book","ID=?",new String[]{book.getID()+""}); if(count!=0){ Toast.makeText(BookActivity.this,"数据库中删除成功!!",Toast.LENGTH_SHORT).show(); bookList.remove(book); adapter.notifyDataSetChanged(); } else{ Toast.makeText(BookActivity.this,"没有删除!!",Toast.LENGTH_SHORT).show(); } db.close(); } }); dialog.setNegativebutton("取消",null); dialog.show(); } public voID clear() { sqliteDatabase db = dbhelper.getWritableDatabase(); int count = db.delete("Book",null); if (count!= 0) { Toast.makeText(this,"数据库表删除成功!!",Toast.LENGTH_SHORT).show(); bookList.clear(); adapter.notifyDataSetChanged(); } else { Toast.makeText(this,"没有内容可以删除!!",Toast.LENGTH_SHORT).show(); } db.close(); } private voID initBooks() { sqliteDatabase db = dbhelper.getWritableDatabase(); bookList.clear(); //给数据源赋值 Cursor cursor = db.@R_404_5962@("Book",null); //2.进行遍历 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"));// int ID=cursor.getInt(cursor.getColumnIndex("ID"));// double price=cursor.getDouble(cursor.getColumnIndex("price")); //ID列的索引号为0 int ID = cursor.getInt(0); String author = cursor.getString(cursor.getColumnIndex("author")); double price = cursor.getDouble(cursor.getColumnIndex("price")); int pages = cursor.getInt(cursor.getColumnIndex("pages")); String name = cursor.getString(cursor.getColumnIndex("name")); //Book book=new Book(ID,author,price,pages,name); Book book = new Book(); book.setID(ID); book.setname(name); book.setPages(pages); book.setPrice(price); book.setAuthor(author); bookList.add(book); } while (cursor.movetoNext()); } // 3.关闭游标 cursor.close(); db.close(); }}
BookAdapter
package com.example.p229;import androID.content.Context;import androID.vIEw.LayoutInflater;import androID.vIEw.VIEw;import androID.vIEw.VIEwGroup;import androID.Widget.ArrayAdapter;import androID.Widget.ImageVIEw;import androID.Widget.TextVIEw;import java.util.List;public class BookAdapter extends ArrayAdapter<Book> { private int resourceID; public BookAdapter(Context context,int resource,List<Book> objects) { super(context,resource,objects); resourceID = resource; } @OverrIDe public VIEw getVIEw(int position,VIEw convertVIEw,VIEwGroup parent) { Book book = getItem(position); VIEw vIEw; VIEwHolder vIEwHolder; if (convertVIEw == null) { //是否有布局缓存,否 vIEw = LayoutInflater.from(getContext()).inflate(resourceID,parent,false); //创建vIEwHolder vIEwHolder = new VIEwHolder(); //获取VIEwHolder的控件 vIEwHolder.bookID = (TextVIEw) vIEw.findVIEwByID(R.ID.book_ID); vIEwHolder.bookAuthor = (TextVIEw) vIEw.findVIEwByID(R.ID.book_author); vIEwHolder.bookPrice = (TextVIEw) vIEw.findVIEwByID(R.ID.book_price); vIEwHolder.bookPages = (TextVIEw) vIEw.findVIEwByID(R.ID.book_pages); vIEwHolder.bookname = (TextVIEw) vIEw.findVIEwByID(R.ID.book_name); //将vIEwHolder对象存储vIEw中 vIEw.setTag(vIEwHolder); } else {//有缓存 vIEw = convertVIEw; vIEwHolder = (VIEwHolder) vIEw.getTag(); } vIEwHolder.bookID.setText(book.getID() + ""); vIEwHolder.bookAuthor.setText(book.getAuthor() + ""); vIEwHolder.bookPrice.setText(book.getPrice() + ""); vIEwHolder.bookPages.setText(book.getPages() + ""); vIEwHolder.bookname.setText(book.getname() + ""); //fruitimage.setimageResource(fruit.getimageID()); // fruitname.setText(fruit.getname()); return vIEw; } class VIEwHolder { TextVIEw bookID; TextVIEw bookAuthor; TextVIEw bookPrice; TextVIEw bookPages; TextVIEw bookname; }}
MainActivity
package com.example.p229;import androID.content.ContentValues;import androID.content.Intent;import androID.database.sqlite.sqliteDatabase;import androID.support.v7.app.AppCompatActivity;import androID.os.Bundle;import androID.vIEw.VIEw;import androID.Widget.button;import androID.Widget.EditText;import androID.Widget.Toast;public class MainActivity extends AppCompatActivity implements VIEw.OnClickListener { private EditText author_et; private EditText price_et; private EditText pages_et; private EditText name_et; private button insert_data; private button @R_404_5962@_data; private MyDatabaseHelper dbhelper; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); dbhelper = new MyDatabaseHelper(this,1); dbhelper.getWritableDatabase(); author_et = (EditText) findVIEwByID(R.ID.author_et); price_et = (EditText) findVIEwByID(R.ID.price_et); pages_et = (EditText) findVIEwByID(R.ID.pages_et); name_et = (EditText) findVIEwByID(R.ID.name_et); insert_data = (button) findVIEwByID(R.ID.add_data); insert_data.setonClickListener(this); @R_404_5962@_data = (button) findVIEwByID(R.ID.@R_404_5962@_data); @R_404_5962@_data.setonClickListener(this); } @OverrIDe public voID onClick(VIEw v) { switch (v.getID()) { case R.ID.add_data: insertData(); break; case R.ID.@R_404_5962@_data: Intent intent = new Intent(MainActivity.this,BookActivity.class); startActivity(intent); break; } } private voID insertData() { sqliteDatabase db = dbhelper.getWritableDatabase(); ContentValues values = new ContentValues(); String author = author_et.getText().toString(); String name = name_et.getText().toString(); String price = price_et.getText().toString(); String pages = pages_et.getText().toString(); values.put("name",name); values.put("author",author); values.put("price",Double.parseDouble(price)); values.put("pages",Integer.parseInt(pages)); long count = db.insert("Book",null,values); if (count != -1) { Toast.makeText(this,"插入成功!!",Toast.LENGTH_SHORT).show(); author_et.setText(""); name_et.setText(""); price_et.setText(""); pages_et.setText(""); } else { Toast.makeText(this,"插入失败!!",Toast.LENGTH_SHORT).show(); } db.close(); }}
MyDatabaseHelper
package com.example.p229;import androID.content.Context;import androID.database.sqlite.sqliteDatabase;import androID.database.sqlite.sqliteOpenHelper;import androID.Widget.Toast;/** * Created by user on 2019/4/27. */public class MyDatabaseHelper extends sqliteOpenHelper { public static final String CREATE_BOOK="create table Book(" +"ID integer primary key autoincrement," + "author text," + "price real," + "pages integer," +"name text)"; 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 db) { db.execsql(CREATE_BOOK); Toast.makeText(mContext,"成功建表!!",Toast.LENGTH_SHORT).show(); } @OverrIDe public voID onUpgrade(sqliteDatabase db,int oldVersion,int newVersion) { }}
UpdateBookActivity
package com.example.p229;import androID.content.ContentValues;import androID.content.Intent;import androID.database.sqlite.sqliteDatabase;import androID.support.v7.app.AppCompatActivity;import androID.os.Bundle;import androID.text.TextUtils;import androID.vIEw.VIEw;import androID.Widget.button;import androID.Widget.EditText;import androID.Widget.Toast;public class UpdateBookActivity extends AppCompatActivity { private EditText update_author_et; private EditText update_price_et; private EditText update_pages_et; private EditText update_name_et; private MyDatabaseHelper dbhelper; private Book book; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_update_book); //第一步:获取上一个活动传递过来的序列化book Intent intent=getIntent(); book=(Book)intent.getSerializableExtra("book"); //第二步:获取控件并显示数据 update_author_et=(EditText) findVIEwByID(R.ID.update_author_et); update_price_et=(EditText) findVIEwByID(R.ID.update_price_et); update_pages_et=(EditText) findVIEwByID(R.ID.update_pages_et); update_name_et=(EditText) findVIEwByID(R.ID.update_name_et); update_author_et.setText(book.getAuthor()); update_price_et.setText(book.getPrice()+""); update_pages_et.setText(book.getPages()+""); update_name_et.setText(book.getname()); dbhelper=new MyDatabaseHelper(this,1); } //按照页面上重新输入的信息去库中修改该书 public voID updateInfo(VIEw vIEw){ //获取重新输入的信息 String authorStr=update_author_et.getText().toString(); String nameStr=update_name_et.getText().toString(); String priceStr=update_price_et.getText().toString(); String pagesstr=update_pages_et.getText().toString(); //按照获取的信息是否合法决定进行的 *** 作 if(TextUtils.isEmpty(authorStr)||TextUtils.isEmpty(nameStr) ||TextUtils.isEmpty(pagesstr)||TextUtils.isEmpty(priceStr)){ Toast.makeText(this,"数据不完整,重新输入!",Toast.LENGTH_SHORT).show(); } else { //打开数据库 sqliteDatabase db=dbhelper.getWritableDatabase(); ContentValues values=new ContentValues(); values.put("name",nameStr); values.put("author",authorStr); values.put("pages",Integer.parseInt(pagesstr)); values.put("price",Double.parseDouble(priceStr)); int count=db.update("Book",values,new String[]{book.getID()+""}); if(count!=0){ Toast.makeText(this,"修改成功!!",Toast.LENGTH_SHORT).show(); finish(); } else{ Toast.makeText(this,"修改失败!!",Toast.LENGTH_SHORT).show(); } db.close(); } } //关闭页面 public voID cancel(VIEw vIEw){ finish(); }}
activity_book.xml
<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:orIEntation="vertical" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" ><button androID:ID="@+ID/data_clear" androID:text="清空" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" /> <linearLayout androID:orIEntation="horizontal" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content"> <TextVIEw androID:text="ID" androID:layout_weight="1" androID:layout_wIDth="0dp" androID:layout_height="wrap_content" /> <TextVIEw androID:text="name" androID:layout_weight="1" androID:layout_wIDth="0dp" androID:layout_height="wrap_content" /> <TextVIEw androID:text="price" androID:layout_weight="1" androID:layout_wIDth="0dp" androID:layout_height="wrap_content" /> <TextVIEw androID:text="author" androID:layout_weight="1" androID:layout_wIDth="0dp" androID:layout_height="wrap_content" /> <TextVIEw androID:text="pages" androID:layout_weight="1" androID:layout_wIDth="0dp" androID:layout_height="wrap_content" /> </linearLayout> <ListVIEw androID:ID="@+ID/lv_book" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content"></ListVIEw></linearLayout>
activity_main.xml
<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" xmlns:tools="http://schemas.androID.com/tools" androID:ID="@+ID/activity_main" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:orIEntation="vertical" tools:context="com.example.p229.MainActivity"> <linearLayout androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:orIEntation="horizontal"> <TextVIEw androID:layout_wIDth="0dp" androID:layout_height="wrap_content" androID:layout_weight="1" androID:text="作者" androID:textSize="18dp" /> <EditText androID:ID="@+ID/author_et" androID:layout_wIDth="0dp" androID:layout_height="wrap_content" androID:layout_weight="4" androID:hint="输入作者" /> </linearLayout> <linearLayout androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:orIEntation="horizontal"> <TextVIEw androID:layout_wIDth="0dp" androID:layout_height="wrap_content" androID:layout_weight="1" androID:text="单价" androID:textSize="18dp" /> <EditText androID:ID="@+ID/price_et" androID:layout_wIDth="0dp" androID:layout_height="wrap_content" androID:layout_weight="4" androID:hint="输入单价" /> </linearLayout> <linearLayout androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:orIEntation="horizontal"> <TextVIEw androID:layout_wIDth="0dp" androID:layout_height="wrap_content" androID:layout_weight="1" androID:text="页数" androID:textSize="18dp" /> <EditText androID:ID="@+ID/pages_et" androID:layout_wIDth="0dp" androID:layout_height="wrap_content" androID:layout_weight="4" androID:hint="输入页数" /> </linearLayout> <linearLayout androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:orIEntation="horizontal"> <TextVIEw androID:layout_wIDth="0dp" androID:layout_height="wrap_content" androID:layout_weight="1" androID:text="书名" androID:textSize="18dp" /> <EditText androID:ID="@+ID/name_et" androID:layout_wIDth="0dp" androID:layout_height="wrap_content" androID:layout_weight="4" androID:hint="输入书名" /> </linearLayout> <button androID:ID="@+ID/add_data" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="添加记录" /> <button androID:ID="@+ID/@R_404_5962@_data" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="显示数据库信息" /></linearLayout>
activity_update_book.xml
<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" xmlns:tools="http://schemas.androID.com/tools" androID:ID="@+ID/activity_update_book" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:orIEntation="vertical" tools:context="com.example.p229.UpdateBookActivity"> <linearLayout androID:orIEntation="horizontal" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content"> <TextVIEw androID:text="作者" androID:textSize="18dp" androID:layout_wIDth="0dp" androID:layout_weight="1" androID:layout_height="wrap_content" /> <EditText androID:ID="@+ID/update_author_et" androID:layout_wIDth="0dp" androID:layout_weight="4" androID:layout_height="wrap_content" /> </linearLayout> <linearLayout androID:orIEntation="horizontal" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content"> <TextVIEw androID:text="单价" androID:textSize="18dp" androID:layout_wIDth="0dp" androID:layout_weight="1" androID:layout_height="wrap_content" /> <EditText androID:ID="@+ID/update_price_et" androID:layout_wIDth="0dp" androID:layout_weight="4" androID:layout_height="wrap_content" /> </linearLayout> <linearLayout androID:orIEntation="horizontal" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content"> <TextVIEw androID:text="页数" androID:textSize="18dp" androID:layout_wIDth="0dp" androID:layout_weight="1" androID:layout_height="wrap_content" /> <EditText androID:ID="@+ID/update_pages_et" androID:layout_wIDth="0dp" androID:layout_weight="4" androID:layout_height="wrap_content" /> </linearLayout> <linearLayout androID:orIEntation="horizontal" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content"> <TextVIEw androID:text="书名" androID:textSize="18dp" androID:layout_wIDth="0dp" androID:layout_weight="1" androID:layout_height="wrap_content" /> <EditText androID:ID="@+ID/update_name_et" androID:layout_wIDth="0dp" androID:layout_weight="4" androID:layout_height="wrap_content" /> </linearLayout> <button androID:onClick="updateInfo" androID:textSize="18sp" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="修改" /> <button androID:onClick="cancel" androID:textSize="18sp" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="取消" /></linearLayout>
book_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"><TextVIEw androID:ID="@+ID/book_ID" androID:layout_weight="1" androID:layout_wIDth="0dp" androID:layout_height="wrap_content" /> <TextVIEw androID:ID="@+ID/book_name" androID:layout_weight="1" androID:layout_wIDth="0dp" androID:layout_height="wrap_content" /> <TextVIEw androID:ID="@+ID/book_price" androID:layout_weight="1" androID:layout_wIDth="0dp" androID:layout_height="wrap_content" /> <TextVIEw androID:ID="@+ID/book_author" androID:layout_weight="1" androID:layout_wIDth="0dp" androID:layout_height="wrap_content" /> <TextVIEw androID:ID="@+ID/book_pages" androID:layout_weight="1" androID:layout_wIDth="0dp" androID:layout_height="wrap_content" /></linearLayout>
完!
总结以上是内存溢出为你收集整理的Android 图书管理实现增删改查全部内容,希望文章能够帮你解决Android 图书管理实现增删改查所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)