0
  • I working on one Shopping Cart application.
    • In which i show list of products and in that every raw there is "Add to Cart" button, Whenever user clicks on it then that button goes to disable and another view which holds add and subtract quantity should be display.
    • But when i clicks on any single product "Add to Cart" button then that view is going to be update, But when i scroll listview then another view gets update automatically (Not all).

Adapter

public class CategoryProductListAdapter extends BaseAdapter {
Context context;
ArrayList<ProductDetailsModel> productDetailsModels;
FragmentManager fragmentManager;
LayoutInflater inflater;

public CategoryProductListAdapter(Context context, ArrayList<ProductDetailsModel> productDetailsModels) {
    this.context = context;
    this.productDetailsModels = productDetailsModels;
    inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public CategoryProductListAdapter(Context context, ArrayList<ProductDetailsModel> productDetailsModels,FragmentManager fragmentManager) {
    this.context = context;
    this.productDetailsModels = productDetailsModels;
    this.fragmentManager = fragmentManager;
    inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {
    return productDetailsModels.size();
}

@Override
public Object getItem(int i) {
    return productDetailsModels.get(i);
}

@Override
public long getItemId(int i) {
    return 0;
}

public class Holder {
    LinearLayout llProductRawMain, llProductQty;
    ImageView imgProduct;
    TextView txtProductTitle, txtProductUnit;
    Spinner spProductUnits;
    Button btnAddProduct, btnSubProduct;
    EditText etNoOfProduct;
    TextView txtProductPrice;
    Button btnAddToCart;
}

@Override
public View getView(final int i, View view, ViewGroup viewGroup) {
    final Holder holder;
    if (view == null) {
        holder = new Holder();
        view = inflater.inflate(R.layout.category_product_list_raw, viewGroup, false);
        holder.llProductRawMain = (LinearLayout) view.findViewById(R.id.llProductRawMain);
        holder.llProductQty = (LinearLayout) view.findViewById(R.id.llProductQty);
        holder.imgProduct = (ImageView) view.findViewById(R.id.imgProduct);
        holder.txtProductTitle = (TextView) view.findViewById(R.id.txtProductTitle);
        holder.txtProductUnit = (TextView) view.findViewById(R.id.txtProductUnit);
        holder.spProductUnits = (Spinner) view.findViewById(R.id.spProductUnits);
        holder.btnAddProduct = (Button) view.findViewById(R.id.btnAddProduct);
        holder.btnSubProduct = (Button) view.findViewById(R.id.btnSubProduct);
        holder.etNoOfProduct = (EditText) view.findViewById(R.id.etNoOfProduct);
        holder.txtProductPrice = (TextView) view.findViewById(R.id.txtProductPrice);
        holder.btnAddToCart = (Button) view.findViewById(R.id.btnAddToCart);

        view.setTag(holder);
    } else {
        holder = (Holder) view.getTag();
    }

    holder.txtProductTitle.setText(Html.fromHtml(productDetailsModels.get(i).getName()));
    if (productDetailsModels.get(i).getUnits().size() > 0) {
        holder.txtProductUnit.setVisibility(View.GONE);
        holder.spProductUnits.setVisibility(View.VISIBLE);
        ImageLoader.getInstance().displayImage(productDetailsModels.get(i).getUnits().get(0).getThumb(), holder.imgProduct, DisplayImageOption.getDisplayImage(), new AnimateFirstDisplayListener());
        holder.txtProductPrice.setText(context.getString(R.string.rupee) + " " + productDetailsModels.get(i).getUnits().get(0).getPrice());

        ArrayList<ProductUnitModel> units = new ArrayList<>();
        units.clear();
        units.addAll(productDetailsModels.get(i).getUnits());

        ProductUnitsListAdapter productUnitsListAdapter = new ProductUnitsListAdapter(context, units);
        holder.spProductUnits.setAdapter(productUnitsListAdapter);

        holder.spProductUnits.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int j, long l) {
                holder.etNoOfProduct.setText("1");
                ImageLoader.getInstance().displayImage(productDetailsModels.get(i).getUnits().get(j).getThumb(), holder.imgProduct, DisplayImageOption.getDisplayImage(), new AnimateFirstDisplayListener());
                holder.txtProductPrice.setText(context.getString(R.string.rupee) + " " + productDetailsModels.get(i).getUnits().get(j).getPrice());
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });

    } else {
        holder.spProductUnits.setVisibility(View.GONE);
        holder.txtProductUnit.setVisibility(View.VISIBLE);
        holder.txtProductUnit.setText(productDetailsModels.get(i).getUnit());
        ImageLoader.getInstance().displayImage(productDetailsModels.get(i).getImage(), holder.imgProduct, DisplayImageOption.getDisplayImage(), new AnimateFirstDisplayListener());
        holder.txtProductPrice.setText(context.getString(R.string.rupee) + " " + productDetailsModels.get(i).getPrice());
    }

    holder.llProductRawMain.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (productDetailsModels.get(i).getUnits().size() > 0) {
                /*Intent intent = new Intent(context, ProductDetailActivity.class);
                intent.putExtra("productDetailModel", productDetailsModels.get(i));
                intent.putExtra("productUnits", true);
                intent.putExtra("productUnitPos", holder.spProductUnits.getSelectedItemPosition());
                context.startActivity(intent);*/

                //FragmentManager fragmentManager = fra;
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                ProductDetailFragment productDetailFragment = new ProductDetailFragment();

                Bundle bundles = new Bundle();
                ProductDetailsModel productInfoModel = productDetailsModels.get(i);
                boolean productUnits = true;
                int productUnitPos = holder.spProductUnits.getSelectedItemPosition();
                bundles.putSerializable("productDetailModel", productInfoModel);
                bundles.putBoolean("productUnits",productUnits);
                bundles.putInt("productUnitPos",productUnitPos);
                productDetailFragment.setArguments(bundles);

                fragmentTransaction.replace(R.id.frameContainer, productDetailFragment);
                fragmentTransaction.addToBackStack(productDetailFragment.getClass().getName());
                fragmentTransaction.commit();

            } else {
                /*Intent intent = new Intent(context, ProductDetailActivity.class);
                intent.putExtra("productDetailModel", productDetailsModels.get(i));
                intent.putExtra("productUnits", false);
                intent.putExtra("productUnitPos", 0);
                context.startActivity(intent);*/

                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                ProductDetailFragment productDetailFragment = new ProductDetailFragment();

                Bundle bundles = new Bundle();
                ProductDetailsModel productInfoModel = productDetailsModels.get(i);
                boolean productUnits = false;
                int productUnitPos = 0;
                bundles.putSerializable("productDetailModel", productInfoModel);
                bundles.putBoolean("productUnits",productUnits);
                bundles.putInt("productUnitPos",productUnitPos);
                productDetailFragment.setArguments(bundles);

                fragmentTransaction.replace(R.id.frameContainer, productDetailFragment);
                fragmentTransaction.addToBackStack(productDetailFragment.getClass().getName());
                fragmentTransaction.commit();
            }
        }
    });
    holder.btnAddProduct.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String product_id;
            String variation_id;

            int qty = Integer.parseInt(holder.etNoOfProduct.getText().toString());
            qty++;
            holder.etNoOfProduct.setText(String.valueOf(qty));
            double qtyWisePrice = qty * Double.parseDouble(productDetailsModels.get(i).getPrice());
            holder.txtProductPrice.setText(context.getString(R.string.rupee) + " " + String.valueOf(qtyWisePrice));

            if (productDetailsModels.get(i).getUnits().size() > 0) {
                product_id = productDetailsModels.get(i).getUnits().get(holder.spProductUnits.getSelectedItemPosition()).getProduct_id();
                variation_id = productDetailsModels.get(i).getUnits().get(holder.spProductUnits.getSelectedItemPosition()).getVariation_id();
            } else {
                product_id = productDetailsModels.get(i).getProduct_id();
                variation_id = productDetailsModels.get(i).getVariation_id();
            }

            updateCart(product_id, variation_id, qty);
        }
    });

    holder.btnSubProduct.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String product_id;
            String variation_id;

            int qty = Integer.parseInt(holder.etNoOfProduct.getText().toString());
            qty--;
            if (qty > 0) {
                holder.etNoOfProduct.setText(String.valueOf(qty));
                double qtyWisePrice = qty * Double.parseDouble(productDetailsModels.get(i).getPrice());
                holder.txtProductPrice.setText(context.getString(R.string.rupee) + " " + String.valueOf(qtyWisePrice));

                if (productDetailsModels.get(i).getUnits().size() > 0) {
                    product_id = productDetailsModels.get(i).getUnits().get(holder.spProductUnits.getSelectedItemPosition()).getProduct_id();
                    variation_id = productDetailsModels.get(i).getUnits().get(holder.spProductUnits.getSelectedItemPosition()).getVariation_id();
                } else {
                    product_id = productDetailsModels.get(i).getProduct_id();
                    variation_id = productDetailsModels.get(i).getVariation_id();
                }

                updateCart(product_id, variation_id, qty);
            } else {
                Toast.makeText(context, "Quntity can not be zero!", Toast.LENGTH_SHORT).show();
            }
        }
    });

    holder.btnAddToCart.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            final boolean isUserLogin = AppMethod.getBooleanPreference((Activity) context, AppConstant.PREF_IS_LOGGED_IN);
            if (isUserLogin) {
                holder.llProductQty.setVisibility(View.VISIBLE);
                holder.btnAddToCart.setVisibility(View.GONE);
                String product_id;
                String variation_id;
                if (productDetailsModels.get(i).getUnits().size() > 0) {
                    product_id = productDetailsModels.get(i).getUnits().get(holder.spProductUnits.getSelectedItemPosition()).getProduct_id();
                    variation_id = productDetailsModels.get(i).getUnits().get(holder.spProductUnits.getSelectedItemPosition()).getVariation_id();
                } else {
                    product_id = productDetailsModels.get(i).getProduct_id();
                    variation_id = productDetailsModels.get(i).getVariation_id();
                }
                updateCart(product_id, variation_id, 1);
            } else {
                Toast.makeText(context, "Please Login for Add to Cart !", Toast.LENGTH_SHORT).show();
            }
        }
    });

    return view;
}

private void updateCart(String product_id, String variation_id, int qty) {
    String customer_id = AppMethod.getStringPreference((Activity) context, AppConstant.PREF_USER_ID);

    if (AppMethod.isNetworkConnected((Activity) context)) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("product_id", product_id);
            jsonObject.put("variation_id", variation_id);
            jsonObject.put("qty", qty);
            jsonObject.put("customer_id", customer_id);

            WsHttpPostJson wsHttpPostJson = new WsHttpPostJson(context, AppConstant.ADD_CART_WS, jsonObject.toString());
            wsHttpPostJson.execute(AppConstant.ADD_CART_WS_URL);

        } catch (JSONException e) {
            e.printStackTrace();
        }
    } else {
        Toast.makeText(context, AppConstant.NO_INTERNET_CONNECTION, Toast.LENGTH_SHORT).show();
    }
}

XML of Adapter

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">

<LinearLayout
    android:id="@+id/llProductRawMain"
    android:layout_width="175dp"
    android:layout_height="wrap_content"
    android:background="@drawable/product_grid_item_bg"
    android:orientation="horizontal"
    android:padding="2dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <ImageView
                android:id="@+id/imgProduct"
                android:layout_width="match_parent"
                android:layout_height="@dimen/_80sdp"
                android:padding="@dimen/_5sdp"
                android:src="@mipmap/ic_launcher" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical"
            android:padding="@dimen/view_margin">

            <TextView
                android:id="@+id/txtProductTitle"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:maxLines="2"
                android:minLines="2"
                android:textSize="@dimen/_11sdp"
                android:text="Product Title"
                android:textColor="#000000" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/txtProductUnit"
                    android:layout_width="match_parent"
                    android:layout_height="@dimen/_25sdp"
                    android:gravity="center_vertical"
                    android:padding="5dp"
                    android:textSize="@dimen/_11sdp"
                    android:text="Product Unit" />

                <Spinner
                    android:id="@+id/spProductUnits"
                    android:layout_width="match_parent"
                    android:layout_height="@dimen/_25sdp"
                    android:gravity="center"
                    android:background="@drawable/icon_dropdown"
                    android:visibility="gone" />
            </LinearLayout>

            <TextView
                android:id="@+id/txtProductPrice"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:text="\u20B9 200"
                android:textStyle="bold"
                android:textSize="@dimen/_13sdp"
                android:textColor="#185401" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <LinearLayout
                android:id="@+id/llProductQty"
                android:layout_width="match_parent"
                android:layout_height="@dimen/_30sdp"
                android:background="@color/app_color"
                android:orientation="horizontal"
                android:visibility="gone">

                <Button
                    android:id="@+id/btnSubProduct"
                    android:layout_width="@dimen/_25sdp"
                    android:layout_height="@dimen/_25sdp"
                    android:layout_gravity="center_vertical"
                    android:background="@drawable/icon_minus"
                    android:gravity="center" />
                <EditText
                    android:id="@+id/etNoOfProduct"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_gravity="center_vertical"
                    android:layout_weight="1"
                    android:background="@null"
                    android:ems="10"
                    android:enabled="false"
                    android:textSize="@dimen/_10sdp"
                    android:gravity="center"
                    android:inputType="number"
                    android:text="1"
                    android:textColor="@color/white" />
                <Button
                    android:id="@+id/btnAddProduct"
                    android:layout_width="@dimen/_25sdp"
                    android:layout_height="@dimen/_25sdp"
                    android:layout_gravity="center_vertical"
                    android:background="@drawable/icon_plus"
                    android:gravity="center" />
            </LinearLayout>
            <Button
                android:id="@+id/btnAddToCart"
                android:layout_width="match_parent"
                android:layout_height="@dimen/_30sdp"
                android:background="@color/add_to_cart_btn_bg"
                android:text="ADD TO CART"
                android:textSize="@dimen/_12sdp"
                android:textColor="@color/app_color" />
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

Sam
  • 462
  • 2
  • 13

1 Answers1

0

I suffer from this also and realize that i think this links are help you.

Answer-1

Answer-2

Thanks

Community
  • 1
  • 1
DDN
  • 58
  • 1
  • 4