如何从Recyclerview获取每个项目值并将其存储在本地数据库中

时间:2019-07-10 05:08:36

标签: android android-sqlite

1。我拥有像电子购物车/食品订购商品这样的recyclerview,其中我使用(+)和(-)按钮添加每个商品的数量,然后如何获取带有商品名称的每个商品的价值,项目费率和数量。

2。如何从“回收者”视图中将获取的值存储在本地数据库中。

//这是我的适配器类

package com.myapplication.chaiwalacustomer.adapter;

import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.myapplication.chaiwalacustomer.R;
import com.myapplication.chaiwalacustomer.model.TeaListPOJO;
import com.myapplication.chaiwalacustomer.utils.AppConstants;
import com.myapplication.chaiwalacustomer.utils.Base64;

import java.util.List;


public class TeaListAdapter extends RecyclerView.Adapter<TeaListAdapter.MyViewHolder> {

        //private int num=0;
        private List<TeaListPOJO> teaItemList;
        private Context mContext;
        private Cursor cursor;

        public class MyViewHolder extends RecyclerView.ViewHolder {
            public TextView tvitemName, tvitemRate,number;
            public ImageView ivItemImg;
            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);
                number=(TextView) view.findViewById (R.id.integer_number);
                btnIncrease =view.findViewById(R.id.btn_increase);
                btnDecrease = view.findViewById(R.id.btn_decrease);

            }
        }


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

            TeaListPOJO tealist = teaItemList.get(position);
            holder.tvitemName.setText(tealist.getItemName ());
            holder.tvitemRate.setText(AppConstants.INDIAN_RUPEE_SIGN.concat (tealist.getItemRate ()));
           // holder.number.setText (tealist.getQty ());
            //num=Integer.parseInt (tealist.getQty ());

            //String title = ((TextView) recyclerView.findViewHolderForAdapterPosition(position).itemView.findViewById(R.id.title)).getText().toString();

            holder.btnIncrease.setOnClickListener (new View.OnClickListener () {
                @Override
                public void onClick(View view) {
                    int num=0;
                    try{

                        num = Integer.parseInt(holder.number.getText().toString());
                    }catch(Exception e) {
                        num = 0;
                    }
                    num++;
                    holder.number.setText ((num+""));

                    notifyDataSetChanged();
                }
            });
            holder.btnDecrease.setOnClickListener (new View.OnClickListener () {
                @Override
                public void onClick(View view) {
                    int num=0;
                    try{

                        num = Integer.parseInt(holder.number.getText().toString());
                    }catch(Exception e) {
                        num = 0;
                    }
                    if(num>0)
                        num--;
                    holder.number.setText ((num+""));
                    notifyDataSetChanged();
                }
            });

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

//这是我的活动

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;

    int count=0;


    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();
        // prepareMovieData();
    }



    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) {
        for(int i=0;i<itemCount;i++)
        {
            view=recyclerView.getChildAt(i); // This will give you entire row(child) from RecyclerView
            if(view!=null)
            {
                EditText editText= (EditText) view.findViewById(R.id.et_time);
                String text=editText.getText().toString();
                if (text.equals(i)){
                    // Do your stuff, after comparison
                }
            }
        }

    }

    @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 :(得分:0)

创建一个变量,例如HashMap<Object,Integer>类型的selectedMap。一旦增加计数,然后使用条件

HashMap<Object,Integer> selectedMap = new HashMap();

        //In increase count onClick
        if(selectedMap.containsKey(teaList)) {
            int prevCount = selectedMap.get(teaList);
            selectedMap.put(teaList, prevCount++);
        } else {
            selectedMap.put(teaList, 1);
        }

        //In decrease count onClick
        if(selectedMap.containsKey(teaList)) {
            int prevCount = selectedMap.get(teaList);
            if(prevCount == 1)
                selectedMap.remove(teaList);
            else
                selectedMap.put(teaList, prevCount--);
        }

现在此地图具有所有选定的项目。如果要将项目存储在数据库中,则只需迭代此HashMap,每个项目的值就会为您提供项目计数。

关于将数据存储到数据库中,首先确定要使用的数据库类型,然后寻求帮助,以根据您的要求如何插入和提取数据。