这里依据上次写的SQLite数据库-增删改查语句来实现该项目
项目结构
XML布局
1.activity_main.xml
<?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"
tools:context=".MainActivity"
android:padding="10dp"
android:orientation="vertical"
>
<ListView
android:id="@+id/foods"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button
android:id="@+id/add_btn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="添加"
android:onClick="myclick"
android:layout_weight="1"/>
<Button
android:id="@+id/remove_btn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="删除"
android:onClick="myclick"
android:layout_weight="1"/>
<Button
android:id="@+id/modify_btn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="修改"
android:onClick="myclick"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
2.activity_operate.xml
<?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"
tools:context=".OperateActivity"
android:padding="10dp"
android:orientation="vertical"
>
<EditText
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="60dp"
android:hint="请输入书名..."
android:textSize="28sp"
android:layout_margin="5dp"
/>
<EditText
android:id="@+id/steps"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入版本或介绍..."
android:textSize="28sp"
android:maxLines="3"
android:minLines="3"
android:gravity="top"
android:layout_margin="5dp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="请选择图片:"
/>
<GridLayout
android:id="@+id/img_gl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="5"
android:layout_margin="5dp"
>
<ImageView
android:layout_width="0dp"
android:layout_height="90dp"
android:layout_columnWeight="1"
android:src="@mipmap/b1"
/>
<ImageView
android:layout_width="0dp"
android:layout_height="90dp"
android:layout_columnWeight="1"
android:src="@mipmap/b2"
/>
<ImageView
android:layout_width="0dp"
android:layout_height="90dp"
android:layout_columnWeight="1"
android:src="@mipmap/b3"
/>
<ImageView
android:layout_width="0dp"
android:layout_height="90dp"
android:layout_columnWeight="1"
android:src="@mipmap/b4"
/>
<ImageView
android:layout_width="0dp"
android:layout_height="90dp"
android:layout_columnWeight="1"
android:src="@mipmap/b5"
/>
<ImageView
android:layout_width="0dp"
android:layout_height="90dp"
android:layout_columnWeight="1"
android:src="@mipmap/b6"
/>
</GridLayout>
<Button
android:id="@+id/upload_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="提交"
/>
</LinearLayout>
3.item.xml
<?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"
tools:context=".OperateActivity"
android:padding="10dp"
android:orientation="vertical"
>
<EditText
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="60dp"
android:hint="请输入书名..."
android:textSize="28sp"
android:layout_margin="5dp"
/>
<EditText
android:id="@+id/steps"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入版本或介绍..."
android:textSize="28sp"
android:maxLines="3"
android:minLines="3"
android:gravity="top"
android:layout_margin="5dp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="请选择图片:"
/>
<GridLayout
android:id="@+id/img_gl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="5"
android:layout_margin="5dp"
>
<ImageView
android:layout_width="0dp"
android:layout_height="90dp"
android:layout_columnWeight="1"
android:src="@mipmap/b1"
/>
<ImageView
android:layout_width="0dp"
android:layout_height="90dp"
android:layout_columnWeight="1"
android:src="@mipmap/b2"
/>
<ImageView
android:layout_width="0dp"
android:layout_height="90dp"
android:layout_columnWeight="1"
android:src="@mipmap/b3"
/>
<ImageView
android:layout_width="0dp"
android:layout_height="90dp"
android:layout_columnWeight="1"
android:src="@mipmap/b4"
/>
<ImageView
android:layout_width="0dp"
android:layout_height="90dp"
android:layout_columnWeight="1"
android:src="@mipmap/b5"
/>
<ImageView
android:layout_width="0dp"
android:layout_height="90dp"
android:layout_columnWeight="1"
android:src="@mipmap/b6"
/>
</GridLayout>
<Button
android:id="@+id/upload_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="提交"
/>
</LinearLayout>
首先写两个类作为工具类
1.BookDao
private SQLiteDatabase db;
public BookDao(Context context){
//存储到自身路径
String path = context.getFilesDir().getAbsolutePath()+"/Book.db";
//得到值
//实例化辅助类
//参数1:上下文环境
//参数2:数据库路径
//参数3:游标 一般 null
//参数4:版本[决定是否会调用升级方法]
SQLiteOpenHelper helper = new SQLiteOpenHelper(context,path,null,1) {
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
//创建
Log.e("Tag","创建");
//创建表
String sql = "create table Book ("+
"_id integer primary key autoincrement,"+
"name varchar(20),"+
"steps varchar(500),"+
"img integer)";
//数据库执行
sqLiteDatabase.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldV, int newV) {
//更新 ,升级
Log.e("Tags","升级");
}
};
//获取数据库对象
/*
* 1.数据库如果不存在,则先创建在打开
* 2.如果数据库存在,直接打开
* */
db = helper.getReadableDatabase();
}
1.1在BookDao类中实现SQLite的增删改查操作
//在public BookDao(Context context){....}外写
//增
public void add(Book book){
String sql = "insert into Book(name,steps,img)values(?,?,?)";
Object[] objargs = {book.getName(),book.getSteps(),book.getImg()};
db.execSQL(sql,objargs);
}
//删
public void delete(String _id){
String sql = "delete from Book where _id ="+_id;
db.execSQL(sql);
}
//改
public void update(Book books){
String sql = "update Book set name=?,steps=?,img=? where _id=?";
Object[] objargs = {books.getName(),books.getSteps(),books.getImg(),books.get_id()};
db.execSQL(sql,objargs);
}
//查
public Cursor getAllBooks(){
/*String sql = "select * from Book where name=?and img =?";*/
String sql = "select * from Book";
//使用游标
Cursor cursor = db.rawQuery(sql,null);
return cursor;
}
2.Book类(没什么好说的和数据库相对应就好了)
public class Book implements Serializable {
//与数据库类型对应
private int _id;
private String name;
private String steps;
private int img;
public Book() {
}
public Book(int _id, String name, String steps, int img) {
this._id = _id;
this.name = name;
this.steps = steps;
this.img = img;
}
public int get_id() {
return _id;
}
public void set_id(int _id) {
this._id = _id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSteps() {
return steps;
}
public void setSteps(String steps) {
this.steps = steps;
}
public int getImg() {
return img;
}
public void setImg(int img) {
this.img = img;
}
@Override
public String toString() {
return "Book{" +
"_id=" + _id +
", name='" + name + '\'' +
", steps='" + steps + '\'' +
", img=" + img +
'}';
}
}
实现添加操作
1.MainActivity
实现个跳转到OperateActivity即可
public void myclick(View v){
switch (v.getId()){
case R.id.add_btn:
startActivity(new Intent(this,OperateActivity.class));
break;
}
}
2.OperateActivity方法
这里就讲一下思路,然后附上完整代码
首先获得到文本框的信息和你选择的图片然后点击提交就会保存到数据库中
选择图片中会将图片放在数组里面遍历,这样方便后面的UI显示和数据库的操作
当用于修改方法时接受上个页面传入的值
完整代码
public class OperateActivity extends AppCompatActivity {
private EditText name,steps;
private GridLayout gl;
//定义整型
private int imgid;
//半透明状态
//判定度
private int lastindex = -1;
//装入数组内
private int[] imgs = new int[]{R.mipmap.b1,R.mipmap.b2,R.mipmap.b3,R.mipmap.b4,R.mipmap.b5
,R.mipmap.b6
};
private BookDao dao;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_operate);
dao = new BookDao(this);
name = findViewById(R.id.name);
steps = findViewById(R.id.steps);
//获取到上一个界面传过来对象
final Book books = (Book) getIntent().getSerializableExtra("book");
//图片
int imgIndex = -1;
if (books != null){
name.setText(books.getName());
steps.setText(books.getSteps());
imgIndex = books.getImg();
}
gl = findViewById(R.id.img_gl);
//二.遍历子控件
for (int i = 0;i< gl.getChildCount();i++){
final ImageView img =(ImageView)gl.getChildAt(i);
//修改图片
if (imgs[i] == imgIndex){
img.setAlpha(0.5f);
lastindex = i;
}
final int index = i;
img.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
imgid = imgs[index];//获取被点击图片的资源索引
//半透明状态
//判定度
img.setAlpha(0.5f);
if (lastindex != -1 && lastindex != index){
//点击后其他的变正常
gl.getChildAt(lastindex).setAlpha(1f);
}
lastindex = index;
}
});
}
findViewById(R.id.upload_btn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//一.拿到输入框内容
String nameTexe = name.getText().toString();
String stepsTexe = steps.getText().toString();
//2.1图片索引 imgid
//实例化实体类
Book book = new Book(0,nameTexe,stepsTexe,imgid);
//修改操作判断
if (books == null){
//数据库添加-->先创建实体类
dao.add(book);
//添加成功提醒
Toast.makeText(getBaseContext(),"添加成功",Toast.LENGTH_LONG).show();
}else {
//修改
book.set_id(books.get_id());
dao.update(book);
Toast.makeText(getBaseContext(),"修改成功",Toast.LENGTH_LONG).show();
}
//添加成功后输入框为空
name.setText("");
steps.setText("");
//还原图片样式
gl.getChildAt(lastindex).setAlpha(1f);
}
});
}
}
实现查询操作[页面显示]删除类似
1.MainActivity
这里也是讲一下思路,然后附上代码
首先将存储的数据取出,调用查询语句
最主要的是适配器的使用,能让数据显示在页面上
其次使用游标定位显示的内容
剩下的就是UI样式了
删除的思路也差不多(使用游标)
public class MainActivity extends AppCompatActivity {
private Button add;
private BookDao dao;
private Cursor cursor;
private ListView foods;
private SimpleCursorAdapter adapter;
//显示的的内容
String[]from = {"name","img"};
//显示的控件
int[] to = {R.id.item_name,R.id.item_img};
//点击事件
private String _id;
private int lastposition = -1;
//修改
private Book book;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
add = findViewById(R.id.add_btn);
foods = findViewById(R.id.foods);
dao = new BookDao(this);
//为ListView添加长按事件
foods.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, int position, long id) {
//return false;//在长按后-->点击事件
//int position 和子项游标对应
cursor.moveToPosition(position);
String step = cursor.getString(2);
//可以换样式
Toast.makeText(MainActivity.this,step,Toast.LENGTH_LONG).show();
return true;
}
});
//为ListView添加子项点击事件
foods.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
cursor.moveToPosition(position);
//要设置为全局_id
_id = cursor.getString(0);
//修改(找到想对应的游标)
book = new Book(cursor.getInt(0),cursor.getString(1),cursor.getString(2),cursor.getInt(3));
//设置点击事件
view.setBackgroundColor(Color.GRAY);
//
if (lastposition != -1 && lastposition != position){
foods.getChildAt(lastposition).setBackgroundColor(Color.WHITE);
}
//重新赋值
lastposition = position;
}
});
}
@Override
protected void onResume() {
super.onResume();
cursor = dao.getAllBooks();
//适配器[简单游标适配器]SimpleCursorAdapter(参数1:this,参数2:布局,参数3:游标,参数4.5:数据源,参数6:游标模式[为观察者模式])
adapter = new SimpleCursorAdapter(this,R.layout.item,cursor,from,to, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
foods.setAdapter(adapter);
}
public void myclick(View v){
switch (v.getId()){
case R.id.add_btn:
startActivity(new Intent(this,OperateActivity.class));
break;
case R.id.remove_btn:
//提醒用户删除成功
//Toast
dao.delete(_id);
//清除页面
cursor.requery();
//通知适配器更新
adapter.notifyDataSetChanged();
//恢复初值,否则报错
lastposition = -1;
break;
//修改
case R.id.modify_btn:
Intent intent = new Intent(MainActivity.this,OperateActivity.class);
//必须使Book实体类继承序列化接口Serializable
intent.putExtra("book",book);
startActivity(intent);
break;
}
}
}
成果
1.初始页面
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/a72c3f4cda61c7d5c54d29bbc431dc52.jpeg)
2.添加&修改页面
3.添加后页面
点击图片可以实现删除和修改,长按可以显示输入的版本号或者介绍
感谢看到这里,Android萌新一枚,有问题望大佬指出谢谢