0

I have Recycler view where I am displaying the items with Item name, Item rate and quantity of the items which is increased and decreased by +/- buttons respectively. now, i want to get all the values from each item of the Recycler view and send it over the server or save it to local database so how to achieve this.?

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

            }
        }
        }
Mohsin kazi
  • 532
  • 1
  • 8
  • 15
  • It can be anything, not just `RecyclerView`. With each +/- update, update your `List>` and when you need to save it in the database, just pass this to a method and process it. Read the POJO's one by one and keep saving them – bradley101 Jul 17 '19 at 05:51
  • @Shantanu Can i get elaborated explanation with some pseudo code as I am new to android not getting proper idea – Shivaraj Patil Jul 17 '19 at 05:57
  • create a method in your `TeaListAdapter` named `getList()` which will return the array list of the recycler view. Then whenever you want to retrieve the items just call `mAdapter.getList()` and do the database thing as you want – bradley101 Jul 17 '19 at 06:25

1 Answers1

1

So you are already passing the arraylist (teaItemList) in your constructor of your adapter. Since the reference is same you can use the same array list for saving or sending it to the database.

Since name and price of items are same, You need to get the value only like for if user clicks + = String.valueOf(count+ 1) and for (-) = String.valueOf(count - 1)

So add one more field like (int count) in teaPOJO and whenever user clicks + : Do this

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

                    });          

User clicks - :

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);
Mohamed Niyaz
  • 228
  • 1
  • 9
  • How can i add it to database and also want to send it to server – Shivaraj Patil Jul 17 '19 at 06:56
  • @ShivarajPatil you want the code for adding it to database and sending to the server ? – Mohamed Niyaz Jul 17 '19 at 07:00
  • Yes bro.. I am new to android if you share the code i will be helpful to understand – Shivaraj Patil Jul 17 '19 at 07:03
  • @ShivarajPatil you can follow this link : https://stackoverflow.com/questions/5703330/saving-arraylists-in-sqlite-databases for saving arraylist in db and this link for server :: https://stackoverflow.com/questions/25582199/sending-arraylist-from-android-to-php-script-using-json. – Mohamed Niyaz Jul 17 '19 at 07:10
  • Where should i do this.? I m getting confused whether should i do it in activity or in adapter – Shivaraj Patil Jul 17 '19 at 07:26
  • @ShivarajPatil you should do this in activity only and not in adapter. – Mohamed Niyaz Jul 17 '19 at 07:27
  • Okay.. then i have to make an insertdb method in my DBhelper class and then insert the values of list with the JSON object and then using insertdb method i have to store it in datbase is it right.? – Shivaraj Patil Jul 17 '19 at 07:31
  • public boolean insertCusOrder(String uniqueArrays) { List items = new ArrayList (); SQLiteDatabase db = this.getWritableDatabase(); JSONObject json = new JSONObject (); try { json.put("uniqueArrays", new JSONArray (items)); } catch (JSONException e) { e.printStackTrace (); } String arrayList = json.toString(); return true; } Like this i tried but not worked please give the solution – Shivaraj Patil Jul 17 '19 at 08:02