From 148c22a247765a8cfa211b7cbfaed8dc8d855866 Mon Sep 17 00:00:00 2001 From: fyf <11621@LAPTOP-04QQU0AO> Date: Tue, 30 May 2023 11:37:07 +0800 Subject: [PATCH] =?UTF-8?q?=E7=8E=B0=E5=9C=BA=E8=B0=83=E8=AF=95=E6=94=B9?= =?UTF-8?q?=E5=8A=A899999?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/example/bpa/config/DataBus.java | 5 +- .../main/java/com/example/bpa/db/QueryDB.java | 55 +++++++++++-------- .../WrapContentLinearLayoutManager.java | 30 ++++++++++ .../bpa/view/fragment/HomeFragment.java | 3 +- .../bpa/view/from/add_pf_activity.java | 11 +++- 5 files changed, 78 insertions(+), 26 deletions(-) create mode 100644 app/src/main/java/com/example/bpa/helper/WrapContentLinearLayoutManager.java diff --git a/app/src/main/java/com/example/bpa/config/DataBus.java b/app/src/main/java/com/example/bpa/config/DataBus.java index 9fcb412..534babe 100644 --- a/app/src/main/java/com/example/bpa/config/DataBus.java +++ b/app/src/main/java/com/example/bpa/config/DataBus.java @@ -1,5 +1,7 @@ package com.example.bpa.config; +import android.app.Activity; + import com.example.bpa.Model.ResultA; import com.example.bpa.R; import com.example.bpa.db.mode.BPA_SUBORDER; @@ -135,10 +137,11 @@ public class DataBus { QueryDB.UpdateSubOrder(item.subOrder); good=item; } + } if(good!=null) { - GoodsMakeOver.add(good); + //GoodsMakeOver.add(good); GoodsMake.remove(good); goodmakeadapter.refresh(); } diff --git a/app/src/main/java/com/example/bpa/db/QueryDB.java b/app/src/main/java/com/example/bpa/db/QueryDB.java index d1e782a..2038ecc 100644 --- a/app/src/main/java/com/example/bpa/db/QueryDB.java +++ b/app/src/main/java/com/example/bpa/db/QueryDB.java @@ -1661,35 +1661,40 @@ public class QueryDB { * @return */ private static boolean Add(Class c, Object data) { - ContentValues cv = new ContentValues(); - Map map = Tools.getObjValue(data); - if (map.get("id").toString().isEmpty()) - return false; - for (String key : map.keySet()) { - Object value = map.get(key); - if (value instanceof String) { - cv.put(key, (String) value); - } else if (value instanceof Integer) { - cv.put(key, ((Integer) value).intValue()); - } else if (value instanceof Double) { - cv.put(key, ((Double) value).doubleValue()); - } else if (value instanceof Float) { - cv.put(key, ((Float) value).floatValue()); - } else if (value instanceof Long) { - cv.put(key, ((Long) value).longValue()); - } else if (value instanceof Boolean) { - cv.put(key, ((Boolean) value).booleanValue()); - } + try { + ContentValues cv = new ContentValues(); + Map map = Tools.getObjValue(data); + if (map.get("id").toString().isEmpty()) + return false; + for (String key : map.keySet()) { + Object value = map.get(key); + if (value instanceof String) { + cv.put(key, (String) value); + } else if (value instanceof Integer) { + cv.put(key, ((Integer) value).intValue()); + } else if (value instanceof Double) { + cv.put(key, ((Double) value).doubleValue()); + } else if (value instanceof Float) { + cv.put(key, ((Float) value).floatValue()); + } else if (value instanceof Long) { + cv.put(key, ((Long) value).longValue()); + } else if (value instanceof Boolean) { + cv.put(key, ((Boolean) value).booleanValue()); + } // else if (value instanceof Date) // { // cv.put(key, (Date) value); // } + } + + SQLiteDatabase db = helper.getWritableDatabase(); + long insert = db.insert(c.getSimpleName(), null, cv); + db.close(); + return insert > 0; + } catch (Exception e) { + return false; } - SQLiteDatabase db = helper.getWritableDatabase(); - long insert = db.insert(c.getSimpleName(), null, cv); - db.close(); - return insert > 0; } /** @@ -1700,6 +1705,7 @@ public class QueryDB { * @return */ private static boolean Update(Class c, Object data) { + try { ContentValues cv = new ContentValues(); Map map = Tools.getObjValue(data); String id = map.get("id").toString(); @@ -1731,6 +1737,9 @@ public class QueryDB { db.update(c.getSimpleName(), cv, "id = ?", new String[]{id}); db.close(); return true; + } catch (Exception e) { + return false; + } } /** diff --git a/app/src/main/java/com/example/bpa/helper/WrapContentLinearLayoutManager.java b/app/src/main/java/com/example/bpa/helper/WrapContentLinearLayoutManager.java new file mode 100644 index 0000000..a9125d9 --- /dev/null +++ b/app/src/main/java/com/example/bpa/helper/WrapContentLinearLayoutManager.java @@ -0,0 +1,30 @@ +package com.example.bpa.helper; + +import android.content.Context; +import android.util.AttributeSet; + +import androidx.recyclerview.widget.LinearLayoutManager; +import androidx.recyclerview.widget.RecyclerView; + +public class WrapContentLinearLayoutManager extends LinearLayoutManager { + public WrapContentLinearLayoutManager(Context context) { + super(context); + } + + public WrapContentLinearLayoutManager(Context context, int orientation, boolean reverseLayout) { + super(context, orientation, reverseLayout); + } + + public WrapContentLinearLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { + super(context, attrs, defStyleAttr, defStyleRes); + } + + @Override + public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) { + try { + super.onLayoutChildren(recycler, state); + } catch (IndexOutOfBoundsException e) { + e.printStackTrace(); + } + } +} diff --git a/app/src/main/java/com/example/bpa/view/fragment/HomeFragment.java b/app/src/main/java/com/example/bpa/view/fragment/HomeFragment.java index 3a5980d..a187275 100644 --- a/app/src/main/java/com/example/bpa/view/fragment/HomeFragment.java +++ b/app/src/main/java/com/example/bpa/view/fragment/HomeFragment.java @@ -13,6 +13,7 @@ import android.view.ViewGroup; import com.example.bpa.R; import com.example.bpa.config.DataBus; +import com.example.bpa.helper.WrapContentLinearLayoutManager; import com.example.bpa.view.adapter.maingoods_adapter; import com.example.bpa.view.adapter.makegood_adapter; import com.example.bpa.view.control.MyLayoutManager; @@ -79,7 +80,7 @@ public class HomeFragment extends Fragment implements View.OnClickListener, MyCl DataBus.getInstance().UpdateMainGoods();//更新商品 - MakeGood.setLayoutManager(new LinearLayoutManager(view.getContext(), LinearLayoutManager.HORIZONTAL,false)); + MakeGood.setLayoutManager(new WrapContentLinearLayoutManager(view.getContext(), LinearLayoutManager.HORIZONTAL,false)); DataBus.getInstance().goodmakeadapter = new makegood_adapter(getContext()); MakeGood.setAdapter(DataBus.getInstance().goodmakeadapter); }catch(Exception e){ diff --git a/app/src/main/java/com/example/bpa/view/from/add_pf_activity.java b/app/src/main/java/com/example/bpa/view/from/add_pf_activity.java index fbb970b..1a20804 100644 --- a/app/src/main/java/com/example/bpa/view/from/add_pf_activity.java +++ b/app/src/main/java/com/example/bpa/view/from/add_pf_activity.java @@ -228,9 +228,9 @@ public class add_pf_activity extends AppCompatActivity implements View.OnClickLi T.show(this, "配方信息不能为空!"); return; } + String name = edittext.getText().toString(); if (good == null)//新增 { - String name = edittext.getText().toString(); if (name.isEmpty()) { T.show(this, "商品名称不能为空!"); return; @@ -249,6 +249,15 @@ public class add_pf_activity extends AppCompatActivity implements View.OnClickLi good=goodx; }else //修改 { + if (name.isEmpty()) { + T.show(this, "商品名称不能为空!"); + return; + } + if (!name.equals(good.name) && QueryDB.GetGoodsIs(name)) { + T.show(this, "商品名称重复,请重新输入后重试!"); + return; + } + good.name=name; QueryDB.UpdateGoods(good); QueryDB.DeleteGoodsSrecipeList(good.id); }