How to Save the Values of recycler view in Sqlite Database
我有 Recycler 视图,我在其中显示项目名称、项目比率和项目数量,这些项目分别通过 /- 按钮增加和减少。现在,我想从 Recycler 视图的每个项目中获取所有值并将其发送到服务器或将其保存到本地数据库,那么如何实现这一点。?
TeaListAdapter.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 | public class TeaListAdapter extends RecyclerView.Adapter<TeaListAdapter.MyViewHolder> { PlaceOrderActivity.DataTransferInterface dtInterface; //private int num=0; private List<TeaListPOJO> teaItemList; private Context mContext; private Cursor cursor; public class MyViewHolder extends RecyclerView.ViewHolder { public TextView tvitemName, tvitemRate,tvcount; //number public ImageView ivItemImg,ivPlus,ivMinus; public Button btnIncrease,btnDecrease; RecyclerView.ViewHolder holder; public MyViewHolder(View view) { super(view); tvitemName = (TextView) view.findViewById(R.id.txt_item_name); tvitemRate = (TextView) view.findViewById(R.id.txt_item_price); ivItemImg= (ImageView) view.findViewById (R.id.iv_item); ivPlus=(ImageView) view.findViewById (R.id.row_view_final_order_iv_plus); ivMinus=(ImageView) view.findViewById (R.id.row_view_final_order_iv_minus); tvcount=(TextView) view.findViewById (R.id.row_view_final_order_tv_count); } } public TeaListAdapter(List<TeaListPOJO> teaItemList) { this.mContext=mContext; this.cursor=cursor; this.teaItemList = teaItemList; } @Override public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View itemView = LayoutInflater.from(parent.getContext()) .inflate(R.layout.rv_placeorder_items, parent, false); return new MyViewHolder (itemView); } @Override public void onBindViewHolder(final MyViewHolder holder, final int position) { final TeaListPOJO tealist = teaItemList.get(position); holder.tvitemName.setText(tealist.getItemName ()); holder.tvitemRate.setText(AppConstants.INDIAN_RUPEE_SIGN.concat (tealist.getItemRate ())); holder.ivPlus.setOnClickListener (new View.OnClickListener () { @Override public void onClick(View view) { int count=0; try{ count = Integer.parseInt(holder.tvcount.getText().toString()); } catch(Exception e) { count = 0; } //count++; count = Integer.parseInt(holder.tvcount.getText().toString()); holder.tvcount.setText(String.valueOf(count+ 1)); tealist.setCount (count); Log.e ("count", String.valueOf (count)); } }); holder.ivMinus.setOnClickListener (new View.OnClickListener () { @Override public void onClick(View view) { int count=0; try{ count = Integer.parseInt(holder.tvcount.getText().toString()); } catch(Exception e) { count = 0; } if(count>0) { //count--; count = Integer.parseInt (holder.tvcount.getText ().toString ()); holder.tvcount.setText (String.valueOf (count - 1)); tealist.setCount (count); } } }); byte[] decodedString = new byte[0]; try { decodedString = Base64.decode(tealist.getImageStr(), Base64.DEFAULT); // tenantModelPOJO.setLogo(decodedString); Bitmap bmp = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length); holder.ivItemImg.setImageBitmap(Bitmap.createScaledBitmap(bmp, 50, 50,false)); } catch (Exception e) { e.printStackTrace(); } } @Override public int getItemCount() { return teaItemList.size(); } } |
PlaceOrderActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | public class PlaceOrderActivity extends AppCompatActivity implements AppConstants, View.OnClickListener, WLAPIcalls.OnAPICallCompleteListener { private List<TeaListPOJO> teaList = new ArrayList<> (); private RecyclerView recyclerView; private TeaListAdapter mAdapter; private View view; private Button btnPlaceorder; EditText edtmsg; public String str; private Context mContext = PlaceOrderActivity.this; private int itemCount; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate (savedInstanceState); setContentView (R.layout.activity_place_order); setRecyclerView (view); getallTeaItems (); } List<TeaListPOJO> getTeaItemList(String str) { Gson gson = new Gson (); Type listType = new TypeToken<List<TeaListPOJO>> () { }.getType (); List<TeaListPOJO> myModelList = gson.fromJson (str, listType); return myModelList; } private List<TeaListPOJO> getallTeaItems() { if (new AppCommonMethods (mContext).isNetworkAvailable ()) { WLAPIcalls mAPIcall = new WLAPIcalls (mContext, getString (R.string.getTeaItem), this); mAPIcall.GetTeaItemList (); } else { Toast.makeText (mContext, R.string.no_internet, Toast.LENGTH_SHORT).show (); } return null; } void setRecyclerView(View view) { recyclerView = (RecyclerView) findViewById (R.id.recycler_view); mAdapter = new TeaListAdapter (teaList); RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager (getApplicationContext ()); recyclerView.setLayoutManager (mLayoutManager); recyclerView.setItemAnimator (new DefaultItemAnimator ()); recyclerView.setAdapter (mAdapter); } @Override public void onClick(View view) { } @Override public void onAPICallCompleteListner(Object item, String flag, String result) throws JSONException { if (getString (R.string.getTeaItem).equals (flag)) { Log.e ("Result", result); teaList = getTeaItemList (result); setRecyclerView (view); } } } |
因此,您已经在适配器的构造函数中传递了数组列表 (teaItemList)。由于引用相同,您可以使用相同的数组列表将其保存或发送到数据库。
由于商品的名称和价格相同,您只需要获取值,例如 if user clicks = String.valueOf(count 1) 和 for (-) = String.valueOf(count - 1)
所以在 teaPOJO 中再添加一个字段,例如 (int count) 和
每当用户点击:
这样做
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | holder.ivPlus.setOnClickListener (new View.OnClickListener () { @Override public void onClick(View view) { int count=0; TeaPojo teaPojo = teaItemList.get(getAdapterPosition); try{ count = Integer.parseInt(holder.tvcount.getText().toString()); } catch(Exception e) { count = 0; } //count++; count = Integer.parseInt(holder.tvcount.getText().toString()); holder.tvcount.setText(String.valueOf(count+ 1)); teaPojo.setCount(count); Log.e ("count", String.valueOf (count)); } }); |
用户点击 - :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | holder.ivMinus.setOnClickListener (new View.OnClickListener () { @Override public void onClick(View view) { int count=0; TeaPojo teaPojo = teaItemList.get(getAdapterPosition); try{ count = Integer.parseInt(holder.tvcount.getText().toString()); } catch(Exception e) { count = 0; } if(count>0) { //count--; count = Integer.parseInt(holder.tvcount.getText().toString ()); holder.tvcount.setText (String.valueOf (count - 1)); teaPojo.setCount(count); tealist.setCount (count); }enter code here } }); // if you want to save in db from activity itself only saveToDb(teaItemList); // if you want to send to server from activity itself only handleSendToServer(teaItemList); |