lundi 7 août 2017

how to checked items from recyclerview to show on another activity recyclerview on button click

Hi I am new android developer i have stuck in my project. Problem is i want to user select items by checked box and after they click on view items button then open a another activity in which shows all those selected items in recyclerview in that recyclerview item name, price, and quantity. This is my code

OderItems Activity, OderitemAdapter and OrderItemModel.

OderItems Activity

`public class OrderItems extends AppCompatActivity` {
public static final String KEY_ADSID="adsId";
public RecyclerView recyclerView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_order);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    recyclerView= (RecyclerView) findViewById(R.id.order_item_recyclerview);
    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
          startActivity(new Intent(OrderItems.this,ViewOrder.class));
        }
    });
    parseOrder();
}
public void parseOrder()
    {
        StringRequest stringRequest = new StringRequest(Request.Method.POST, Config.url_ORDERLISTING,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {

                        try {
                            JSONObject jsonObject = new JSONObject(response);

                            Log.d("Response",jsonObject.toString());
                            if (jsonObject.optString("status").equalsIgnoreCase("1"))
                            {
                                // String no_task= jsonObject.getString("error_msg");
                                Toast.makeText(OrderItems.this,"No Order List List !",Toast.LENGTH_LONG).show();
                                // txt_noTask.setText("No Task Is Available");
                                // progressDialog.dismiss();
                            }

                            if (jsonObject.optString("status").equalsIgnoreCase("0") || jsonObject.optString("message").equalsIgnoreCase("Success"))
                            {
                                // Toast.makeText(getActivity(),jsonObject.toString(),Toast.LENGTH_LONG).show();
                                JSONArray jsonArray= jsonObject.getJSONArray("ItemList");

                                ArrayList<OrderItemModel> list =new ArrayList<>();

                                for (int i=0;i<jsonArray.length();i++)
                                {
                                    // TodayTaskModel todayTask = new TodayTaskModel();
                                    OrderItemModel orderitemmodel = new OrderItemModel();

                                    JSONObject jsonObj= jsonArray.getJSONObject(i);
                                    String product_Name =jsonObj.getString("productName");
                                    String product_Price =jsonObj.getString("productPrice");
                                    String package_img =jsonObj.getString("productImg");


// For Set data
                                    orderitemmodel.setProductName(product_Name);
                                    orderitemmodel.setProductPrice(product_Price);
                                    orderitemmodel.setProductImg(package_img);


                                    list.add(orderitemmodel);
                                }
                                // Setup and Handover data to recyclerview
                                OrderItemAdapter adapter=new OrderItemAdapter(getApplicationContext(),list);
                                recyclerView.setAdapter(adapter);
                                recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
                                recyclerView.setHasFixedSize(true);




                            }



                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        //pDialog.dismiss();
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(OrderItems.this,"Server is not Responding !!! ", Toast.LENGTH_LONG).show();
                // Toast.makeText(SpamActivity.this,error.toString(),Toast.LENGTH_LONG).show();
            }
        }){
            @Override
            protected Map<String,String> getParams(){
                Map<String,String> params = new HashMap<String, String>();
                params.put(KEY_ADSID,"101");

                return params;
            }

        };

        RequestQueue requestQueue = 
        Volley.newRequestQueue(getApplicationContext());
        requestQueue.add(stringRequest);
    }

}

OderitemAdapter

public class OrderItemAdapter extends RecyclerView.Adapter<OrderItemAdapter.ViewHolder> {
    ArrayList<OrderItemModel> orderItemList;
    Context context;
    SqlHandler sqlHandler;
    static double summ;

    public OrderItemAdapter(Context c, ArrayList<OrderItemModel> orderItemList)
    {
        this.context=c;
        this.orderItemList=orderItemList;

    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.order_item_layout, parent, false);
        sqlHandler = new SqlHandler(context);
        return new OrderItemAdapter.ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int i) {
        final OrderItemModel position=orderItemList.get(i);
        holder.txt_ProductName.setText(position.getProductName());
        holder.txt_ProductPrice.setText(position.getProductPrice());
        holder.urlProductImg=position.getProductImg();
        holder.strRate = position.getProductPrice();
        holder.strQuantity=position.getProductQty();

        if (holder.urlProductImg.isEmpty()) { //url.isEmpty()
            Picasso.with(context)
                    .load(R.drawable.localbizlist)
                    .placeholder(R.drawable.localbizlist)
                    .error(R.drawable.localbizlist)
                    .into(holder.img_product);

        }else{
            Picasso.with(context)
                    .load(orderItemList.get(i).getProductImg())
                    .placeholder(R.drawable.localbizlist)
                    .error(R.drawable.localbizlist)
                    .into(holder.img_product); //this is your ImageView
        }

    }

    @Override
    public int getItemCount() {
        return orderItemList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener
    {
        CheckBox chkOrder;
        ImageView img_product;
        TextView txt_ProductName,txt_ProductPrice;
        Spinner itemSpinner;
        String urlProductImg,strRate,strQuantity,strProductName;


        public ViewHolder(View itemView) {
            super(itemView);
            context = itemView.getContext();
            img_product=(ImageView)itemView.findViewById(R.id.img_product);
            txt_ProductName=(TextView)itemView.findViewById(R.id.productName);
            txt_ProductPrice=(TextView)itemView.findViewById(R.id.productPrice);
            chkOrder=(CheckBox)itemView.findViewById(R.id.chk_order_status);
            itemSpinner = (Spinner)itemView.findViewById(R.id.item_spinner);
            List<String> itemQuuantity=new ArrayList<String>();
            itemQuuantity.add("1");
            itemQuuantity.add("2");
            itemQuuantity.add("3");
            itemQuuantity.add("4");
            itemQuuantity.add("5");

            ArrayAdapter<String> itemAdapter=new ArrayAdapter<String>(context,android.R.layout.simple_spinner_dropdown_item,itemQuuantity);

            itemAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            itemSpinner.setAdapter(itemAdapter);
            chkOrder.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            context = view.getContext();
            double TotalBill=0.00;
            if (view.getId()==chkOrder.getId())
            {


                if (chkOrder.isChecked())
                {


                    double spQuantity = Double.parseDouble(itemSpinner.getSelectedItem().toString());
                    double itemRate = Double.parseDouble(strRate);
                    double itemPrice = (spQuantity * itemRate);

                    TotalBill+=itemPrice;

                    String strQuatity=Double.toString(spQuantity);
                    String strTotalBill = Double.toString(TotalBill);
                    String strProductName= String.valueOf(txt_ProductName.getText());
                    String strProductPrice=String.valueOf(txt_ProductPrice.getText());

                    Toast.makeText(context,"Checked Item Bill"+TotalBill,Toast.LENGTH_LONG).show();

                    String query = "INSERT INTO ORDER_ITEMS(product_name,product_quantity,product_price) values ('"
                            + strProductName + "','" + strProductPrice + "','"+strQuatity+"')";
                    sqlHandler.executeQuery(query);

                    SumOrderBill(TotalBill);
                    Toast.makeText(context,"Record Save",Toast.LENGTH_LONG).show();
                   // showlist();
                   // strIDCheckBoxStatus=String.valueOf(chkOrder.getId());

                }else
                {
                    //saveInSp("Checked",false);
                    double spQuantity = Double.parseDouble(itemSpinner.getSelectedItem().toString());
                    double itemRate = Double.parseDouble(strRate);
                    double itemPrice = (spQuantity * itemRate);
                    TotalBill+=itemPrice;
                    String strIdProductName= String.valueOf(txt_ProductName.getText());
                    Toast.makeText(context,"UNCHECKED Item Bill"+TotalBill,Toast.LENGTH_LONG).show();
                    MinOrderBill(TotalBill);
                    String delQuery = "DELETE FROM PHONE_CONTACTS WHERE slno='"+strIdProductName+"' ";
                    sqlHandler.executeQuery(delQuery);
                 //   showlist();

                }

            }

        }

        public void SumOrderBill(double ftotal)
        {

            summ+=ftotal;
            String total2 = String.valueOf(summ);
            Toast.makeText(context, "Total Bill"+total2,Toast.LENGTH_LONG).show();
//            SharedPreferences myPrefsLogin=context.getSharedPreferences("biling", Context.MODE_PRIVATE);
//            SharedPreferences.Editor editor = myPrefsLogin.edit();
//            editor.putString("Total", total2);
//            editor.commit();

        }
        public void MinOrderBill(double ftotal)
        {

            summ-=ftotal;
            String total2 = String.valueOf(summ);
            Toast.makeText(context, "Detection"+summ,Toast.LENGTH_LONG).show();
//            SharedPreferences myPrefsLogin=context.getSharedPreferences("biling", Context.MODE_PRIVATE);
//            SharedPreferences.Editor editor = myPrefsLogin.edit();
//            editor.putString("Total", total2);
//            editor.commit();
        }
    }
}

public class OrderItemModel {
    private String status;
    private String message;
    private String productId;
    private String productName;
    private String productType;
    private String productQty;
    private String productImg;
    private String productPrice;

    public OrderItemModel()
    {

    }


    public String getProductPrice() {
        return productPrice;
    }

    public void setProductPrice(String productPrice) {
        this.productPrice = productPrice;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getProductId() {
        return productId;
    }

    public void setProductId(String productId) {
        this.productId = productId;
    }

    public String getProductName() {
        return productName;
    }

    public void setProductName(String productName) {
        this.productName = productName;
    }

    public String getProductType() {
        return productType;
    }

    public void setProductType(String productType) {
        this.productType = productType;
    }

    public String getProductQty() {
        return productQty;
    }

    public void setProductQty(String productQty) {
        this.productQty = productQty;
    }

    public String getProductImg() {
        return productImg;
    }

    public void setProductImg(String productImg) {
        this.productImg = productImg;
    }

}

Please help me for ViewOrder Activity




Aucun commentaire:

Enregistrer un commentaire