完成效果如图所示:
首先编写首页的样式:( activity_goods.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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:layout_width="180dp"
android:layout_height="50dp"
android:hint="商品名称"
android:id="@+id/et_name"/>
<EditText
android:layout_width="180dp"
android:layout_height="50dp"
android:inputType="number"
android:hint="金额"
android:id="@+id/et_price"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+"
android:textSize="30dp"
android:id="@+id/btn_add"/>
</LinearLayout>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/lv_goods"/>
</LinearLayout>
其次对ListView中item样式进行设置( activity_good_item.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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_marginTop="10dp">
<TextView
android:layout_width="125dp"
android:layout_height="80dp"
android:text="商品名"
android:textSize="36dp"
android:id="@+id/tv_detial1"/>
<TextView
android:layout_width="90dp"
android:layout_height="80dp"
android:text=" 金额"
android:textSize="36dp"
android:id="@+id/tv_detial2"/>
<EditText
android:layout_width="100dp"
android:layout_height="80dp"
android:hint="修改的金额"
android:inputType="number"
android:id="@+id/et_edit"/>
<Button
android:layout_width="80dp"
android:layout_height="80dp"
android:text="修改"
android:textSize="23dp"
android:id="@+id/btn_updategood"/>
<Button
android:layout_width="80dp"
android:layout_height="80dp"
android:text="删除"
android:textSize="23dp"
android:id="@+id/btn_deletegood"/>
</LinearLayout>
</LinearLayout>
编写适配器 MyAdapter
package com.example.sqlitedemo.adapter;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.example.sqlitedemo.GoodActivity;
import com.example.sqlitedemo.GoodBean;
import com.example.sqlitedemo.R;
import java.util.List;
public class MyAdapter extends BaseAdapter {
private List<GoodBean> goodlist;
private LayoutInflater layoutInflater;
private Context context;
private Callback mCallback;
public MyAdapter(List<GoodBean> goodlist, Context context,Callback callback) {
this.goodlist = goodlist;
this.context =context;
layoutInflater = LayoutInflater.from(context);
mCallback = callback;
}
public interface Callback {
void click(View v);
void click1(View v,String content);
}
@Override
public int getCount() {
return goodlist!=null?goodlist.size():0;
}
@Override
public Object getItem(int i) {
return goodlist.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(final int i, View view, ViewGroup parent) {
ViewHolder viewHolder = new ViewHolder();
if (view == null){
view =layoutInflater.inflate(R.layout.activity_good_item,null);
TextView etDetail1 = view.findViewById(R.id.tv_detial1);
TextView etDetail2 = view.findViewById(R.id.tv_detial2);
Button btn1 = view.findViewById(R.id.btn_updategood);
Button btn2 = view.findViewById(R.id.btn_deletegood);
EditText etedit = view.findViewById(R.id.et_edit);
viewHolder.etDetail1 = etDetail1;
viewHolder.etDetail2 = etDetail2;
viewHolder.btn1 = btn1;
viewHolder.btn2 = btn2;
viewHolder.etedit = etedit;
view.setTag(viewHolder);
}else {
viewHolder = (ViewHolder) view.getTag();
}
final GoodBean goodBean = goodlist.get(i);
viewHolder.etDetail1.setText(goodBean.getGoodname());
viewHolder.etDetail2.setText(String.valueOf(goodBean.getPrice()));
viewHolder.btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View view) {
AlertDialog alertDialog = new AlertDialog.Builder(view.getContext()).setMessage("确定删除 "+goodlist.get(i).getGoodname()+" 吗?").setIcon(R.mipmap.ic_launcher)
.setPositiveButton("确定", new DialogInterface.OnClickListener() {//添加"Yes"按钮
@Override
public void onClick(DialogInterface dialogInterface, int i) {
mCallback.click(view);
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {//添加取消
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(view.getContext(), "已取消删除", Toast.LENGTH_SHORT).show();
}
}).create();
alertDialog.show();
}
});
viewHolder.btn2.setTag(i);
final ViewHolder finalViewHolder = viewHolder;
viewHolder.btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View view) {
final String content = finalViewHolder.etedit.getText().toString();
AlertDialog alertDialog = new AlertDialog.Builder(view.getContext()).setMessage("确定将 "+goodlist.get(i).getGoodname()+" 的价格从 "+goodlist.get(i).getPrice()+" 更改为 "+content+" 吗?").setIcon(R.mipmap.ic_launcher)
.setPositiveButton("确定", new DialogInterface.OnClickListener() {//添加"Yes"按钮
@Override
public void onClick(DialogInterface dialogInterface, int i) {
mCallback.click1(view,content);
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {//添加取消
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(view.getContext(), "已取消修改", Toast.LENGTH_SHORT).show();
}
}).create();
alertDialog.show();
}
});
viewHolder.btn1.setTag(i);
return view;
}
class ViewHolder{
TextView etDetail1;
TextView etDetail2;
Button btn1;
Button btn2;
EditText etedit;
}
}
GoodActivity
package com.example.sqlitedemo;
import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.example.sqlitedemo.adapter.MyAdapter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
public class GoodActivity extends Activity implements AdapterView.OnItemClickListener, MyAdapter.Callback {
ListView lvGood;
Button btnAdd;
EditText etGname;
EditText etGprice;
GoodDao goodDao;
MyAdapter myAdapter;
List<GoodBean> goodlist;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_goods);
goodDao = new GoodDao(this);
btnAdd = findViewById(R.id.btn_add);
etGname = findViewById(R.id.et_name);
etGprice = findViewById(R.id.et_price);
lvGood = findViewById(R.id.lv_goods);
initView();
initDate();
initEvent();
btnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String goodname = etGname.getText().toString();
String price = etGprice.getText().toString();
if (!(TextUtils.isEmpty(goodname)||TextUtils.isEmpty(price))){
int goodprice = Integer.valueOf(price);
boolean flag = goodDao.insertGood(goodname,goodprice);
if(flag){
Toast.makeText(getApplicationContext(),"添加成功",Toast.LENGTH_SHORT).show();
initView();
initDate();
initEvent();
}else {
Toast.makeText(getApplicationContext(),"添加失败",Toast.LENGTH_SHORT).show();
}
}else {
Toast.makeText(getApplicationContext(),"请输入相关信息",Toast.LENGTH_SHORT).show();
}
etGname.setText("");
etGprice.setText("");
}
});
}
private void initEvent() {
myAdapter = new MyAdapter(goodlist,this,this);
lvGood.setAdapter(myAdapter);
lvGood.setOnItemClickListener(this);
}
private void initDate() {
goodlist = new ArrayList<>();
goodlist = goodDao.seleteAll();
}
private void initView() {
}
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(this, "listview的item被点击了!,点击的位置是-->" + i,Toast.LENGTH_SHORT).show();
}
@Override
public void click(View v) {
boolean flag = goodDao.deleteGood(goodlist.get((Integer)v.getTag()).getGoodname());
if(flag){
Toast.makeText(GoodActivity.this,"删除成功!",Toast.LENGTH_SHORT).show();
initView();
initDate();
initEvent();
}else {
Toast.makeText(GoodActivity.this,"删除失败!",Toast.LENGTH_SHORT).show();
}
}
@Override
public void click1(View v,String content) {
if (!TextUtils.isEmpty(content)){
Integer content1 = Integer.valueOf(content);
String name = goodlist.get((Integer) v.getTag()).getGoodname();
boolean flag = goodDao.updategood(name,content1);
if(flag){
Toast.makeText(GoodActivity.this,"修改成功 !",Toast.LENGTH_SHORT).show();
initView();
initDate();
initEvent();
}else {
Toast.makeText(GoodActivity.this,"修改失败 !",Toast.LENGTH_SHORT).show();
}
}else {
Toast.makeText(GoodActivity.this,"请输入修改金额",Toast.LENGTH_SHORT).show();
}
}
}
GoodBean
package com.example.sqlitedemo;
public class GoodBean {
private int id;
private String goodname;
@Override
public String toString() {
return "GoodBean{" +
"id=" + id +
", goodname='" + goodname + ''' +
", price=" + price +
'}';
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getGoodname() {
return goodname;
}
public void setGoodname(String goodname) {
this.goodname = goodname;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
private int price;
}
GoodDao
package com.example.sqlitedemo;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import java.util.ArrayList;
import java.util.List;
public class GoodDao {
private SQLiteDatabase sqLiteDatabase;
public GoodDao(Context context) {
//初始化刚刚写的MySQLiteHelper对象
MySQLiteHelper mySQLiteHelper = new MySQLiteHelper(context);
//获取sqLiteDatabase对象
sqLiteDatabase = mySQLiteHelper.getWritableDatabase();
}
public boolean insertGood(String goodname,int price){
ContentValues values = new ContentValues();
values.put("goodname",goodname);
values.put("price",price);
//第一个是表名,第二个null,第三个是相当于sql插入语句的values
//id用于判断是否插入成功: 如果大于0则表示插入了至少一条数据,否则插入失败
long id = sqLiteDatabase.insert("good",null,values);
return id>0?true:false;
}
public List<GoodBean> seleteAll(){
List<GoodBean> goodlist = new ArrayList<>();
Cursor cursor = sqLiteDatabase.rawQuery("select * from good",null);
while (cursor.moveToNext()) {
GoodBean goodBean = new GoodBean();
goodBean.setGoodname(cursor.getString(cursor
.getColumnIndex("goodname")));
goodBean.setPrice(cursor.getInt(cursor
.getColumnIndex("price")));
goodlist.add(goodBean);
}
return goodlist;
}
public boolean deleteGood(String goodname){
long id = sqLiteDatabase.delete("good","goodname = ?",new String[]{goodname});
return id>0?true:false;
}
public boolean updategood(String goodname,int price){
boolean flag;
ContentValues values = new ContentValues();
values.put("price",price);
long id = sqLiteDatabase.update("good",values,"goodname = ? ",new String[]{goodname});
flag = id > 0?true : false;
return flag;
}
}
相关MySQLiteHelper可参考 :Android 对 SQLite 的相关操作
原文地址:https://blog.csdn.net/qq_45969735/article/details/123420056
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_10607.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。