如何增加购物车计数器然后按钮点击它

时间:2016-12-21 05:09:46

标签: android android-asynctask

这是我的适配器类,我有一个添加到购物车按钮

public class ServiceSelectionOptionsListAdapter extends ArrayAdapter<ServiceSelectionOptionsDataModel> {

    private Context context;
    private int layoutResourceId;
    ListView list;
    static final String TAG = "LISTT";
    HashMap<Integer, Integer> hashMap;
    String response;
    ProgressDialog pdilog;
    SharedPreferences prefs;
    String serverresponse1;
    private List<ServiceSelectionOptionsDataModel> data;

    String custid;
    JSONArray _jsonarray;
    JSONObject jsonObject;
    String subsrvceopid;
    String quantity = "1";
    String cartcounter;
    TextView counter;


    public ServiceSelectionOptionsListAdapter(Context context, int layoutResourceId,
                                              List<ServiceSelectionOptionsDataModel> data) {
        super(context, R.layout.serviceselectionoptionslist, data);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.data = data;
        hashMap = new HashMap<Integer, Integer>();

        prefs = context.getSharedPreferences(AppConstants.VERIFICATION,
                Context.MODE_PRIVATE);

        custid = prefs.getString(AppConstants.CUSTOMERID, "");

        cartcounter = prefs.getString(AppConstants.CARTITEMCOUNTER, "");

    }

    @Override
    public View getView(final int position, final View convertView, ViewGroup parent) {
        View row = convertView;
        final ViewHolder holder;

        if (row == null) {
            LayoutInflater inflater = ((Activity) context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);
            holder = new ViewHolder();
            holder.optionname = (TextView) row.findViewById(R.id.tvsubserviceoptionname);
            holder.addtocart = (Button) row.findViewById(R.id.btnaddtocart);
            row.setTag(holder);
        } else {
            holder = (ViewHolder) row.getTag();
        }


        final ServiceSelectionOptionsDataModel item = data.get(position);
        holder.optionname.setText(item.getOptionname());
        holder.addtocart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Integer index = (Integer) view.getTag();
                subsrvceopid = data.get(position).getId();
                new addtocart().execute(custid, subsrvceopid, quantity);
                new cartitemsdetails().execute(custid);
                SharedPreferences.Editor editor = context.getSharedPreferences(AppConstants.VERIFICATION, context.MODE_PRIVATE).edit();
                editor.putString(AppConstants.SUBSERVIEOPTIONID, subsrvceopid);
                editor.commit();
                notifyDataSetChanged();
            }
        });

        return row;
    }

    static class ViewHolder {
        TextView optionname;
        //  TextView charges;
        Button addtocart;
        TextView counter;

    }

这是我的活动类,我获取购物车商品清单的大小

class cartitemsdetails extends AsyncTask<String, Void, Void> {

        @Override
        protected void onPreExecute() {
            pdilog = new ProgressDialog(ServiceSelectionOptionsListActivity.this);
            pdilog.setMessage("Please Wait....");
            pdilog.show();
            super.onPreExecute();
        }
        @Override
        protected Void doInBackground(String... params) {
            String response = JSONFunctions.getJSONfromURL("http://cpanel.smartindiaservice.com/api/cartdetails?customerid=" + params[0]);
            try {
                jsonarray = new JSONArray(response);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return null;
        }
        @Override
        protected void onPostExecute(Void aVoid) {

            size = jsonarray.length();
            counter.setText(String.valueOf(size));

            if (size == 0 )
            {
                viewcart.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(ServiceSelectionOptionsListActivity.this, NoItemsInTheCart.class);
                        startActivity(intent);
                    }
                });
                cart.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(ServiceSelectionOptionsListActivity.this, NoItemsInTheCart.class);
                        startActivity(intent);
                    }
                });
            }
            if(size>0) {
                viewcart.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(ServiceSelectionOptionsListActivity.this, MyCart.class);
                        startActivity(intent);
                    }
                });
                cart.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(ServiceSelectionOptionsListActivity.this, MyCart.class);
                        startActivity(intent);
                    }
                });
            }



            pdilog.dismiss();
            super.onPostExecute(aVoid);
        }
    }


    class OptionsSelection extends AsyncTask<String, Void, Void> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected Void doInBackground(String... params) {
            response = JSONFunctions.getJSONfromURL("http://cpanel.smartindiaservice.com/api/subserviceoptions?subserviceid=" + params[0]);
            try {
                _jsonarray = new JSONArray(response);
                for (int i = 0; i < _jsonarray.length(); i++) {
                    ServiceSelectionOptionsDataModel datamodel = new ServiceSelectionOptionsDataModel();
                    jsonObject = _jsonarray.getJSONObject(i);
                    optionname = jsonObject.getString("OptionName");
                    datamodel.setOptionname(optionname);
                    charge = jsonObject.getString("Charges");
                    datamodel.setCharge(charge);
                    subserviceoptionid = jsonObject.getString("SubServiceOptionID");
                    datamodel.setId(subserviceoptionid);
                    lstDataModel.add(datamodel);

                }

            } catch (Exception e) {
                System.out.println(e);
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            ServiceSelectionOptionsListAdapter adapter = new ServiceSelectionOptionsListAdapter(ServiceSelectionOptionsListActivity.this, R.layout.serviceselectionoptionslist, lstDataModel);
            options.setAdapter(adapter);
            adapter.notifyDataSetChanged();

            super.onPostExecute(result);
        }
    }

0 个答案:

没有答案