如何在Sqlite数据库中保存回收站视图的值

时间:2019-07-17 05:15:59

标签: android android-recyclerview android-sqlite

我在“回收站”视图中显示的项目名称,项目费率和项目数量分别通过+/-按钮增加和减少。现在,我想从Recycler视图的每个项目中获取所有值,并将其通过服务器发送或将其保存到本地数据库,以便实现这一目标。

TeaListAdapter.java

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

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);

            }
        }
        }

1 个答案:

答案 0 :(得分:2)

因此,您已经在适配器的构造函数中传递了arraylist(teaItemList)。由于引用是相同的,因此您可以使用相同的数组列表将其保存或发送到数据库。

由于商品的名称和价格相同,因此仅当用户单击+ = String.valueOf(count + 1)和(-)= String.valueOf(count-1)

因此在teaPOJO和 每当用户单击+时: 做

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));
                        }

                    });          

用户点击-:

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);