How to make Shopping Cart for E-commerce

This shopping cart Tutorial for this example code and this tutorial example we will create a simple shopping cart .l will involve several different Layout Our Mobile app Creator has been used for build your mobile shopping cart for android.

1. AddCart.java

package com.tutorialsee.database;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONObject;
import com.tutorialsee.cart.CartItem;
import com.tutorialsee.cart.CartItemAdapter;
import com.tutorialsee.http.BaseActivity;
import com.tutorialsee.http.Constant;
import com.tutorialsee.http.Network;
import com.tutorialsee.http.PrefernceSettings;

import android.content.Context;
import android.database.Cursor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class AddCart extends Fragment {
public static TextView txt_checkout;
public static TextView ssd;
@SuppressWarnings("unused")
private static final String TAG = "ShoppingCartActivity";
ArrayList<HashMap<String, String>> array1=new ArrayList<HashMap<String, String>>();
ListView lvCartItems;
ImageView bClear;
public static TextView tvTotalPrice;
Context context;
ListView listviews ;
ItemDetails mItems;
public static RelativeLayout _rel;
public static LinearLayout shopnow;
public static CartItemAdapter cartItemAdapter;
List<ItemDetails> itemListsys = new ArrayList<ItemDetails>();
ArrayList<HashMap<String, String>> arraylist;
List<ItemDetails> itemList = new ArrayList<ItemDetails>();

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MainActivity.lp = 2;
}

public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.add_cart_activity, container, false);
MainActivity.actionBar.show();
context = container.getContext();
PrefernceSettings.openDataBase(context);
String abc = PrefernceSettings.getUserId();
txt_checkout = (TextView) v.findViewById(R.id.txt_checkout);
ssd = (TextView) v.findViewById(R.id.ssd);
shopnow = (LinearLayout) v.findViewById(R.id.shopnow);
_rel = (RelativeLayout) v.findViewById(R.id._rel);
lvCartItems = (ListView) v.findViewById(R.id.ListViewCatalog);

txt_checkout.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
/*Fragment newContent = new HomeActivtiy();
if (newContent != null) {
switchFragment(newContent);
}*/
}
});



tvTotalPrice = (TextView) v.findViewById(R.id.img_totalprise);
//ssd = (LinearLayout) v. findViewById(R.id.ssd);


LayoutInflater layoutInflater = getLayoutInflater(savedInstanceState);
cartItemAdapter = new CartItemAdapter(getActivity(),R.layout.carddesign);
if (!abc.isEmpty()) {
MainActivity.dbHelpercart.deleteRecord();
Cartdetails ck1 = new Cartdetails();
ck1.execute();
} else {
cartItemAdapter.updateCartItems(getCartItems());
lvCartItems.setAdapter(cartItemAdapter);
MainActivity.tx.setText(MainActivity.dbHelpercart.countRecord() + "");
tvTotalPrice.setText(String.valueOf(BigDecimal.valueOf(MainActivity.dbHelpercart.gettotalprice())));
if( MainActivity.dbHelpercart.countRecord()>0){
ssd.setVisibility(View.GONE);
_rel.setVisibility(View.VISIBLE);
}else{

ssd.setVisibility(View.VISIBLE);
_rel.setVisibility(View.GONE);
}
}




shopnow.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
if (MainActivity._ordd.getText().toString().contains("Login")) {
if (MainActivity.dbHelpercart.countRecord() < 1) {
Toast toast = Toast.makeText(getActivity(),
"Your cart is Empty", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 90);
toast.show();
} else {
/*Fragment newContent = new CheckOutSecond();
if (newContent != null) {
switchFragment(newContent);
}*/
}
} else {
if (MainActivity.dbHelpercart.countRecord() < 1) {
Toast toast = Toast.makeText(getActivity(),
"Your cart is Empty", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 90);
toast.show();
} else {
/*Fragment newContent = new CheckOut();
if (newContent != null) {
switchFragment(newContent);
}*/
}
}
}
});


if(Network.isConnectionFast(context)){
String abcs = PrefernceSettings.getUserId();
Log.v("User Id", PrefernceSettings.getUserId());
String res = BaseActivity.getJsonnew(getActivity(),Constant.ADDRESSLIST() + abcs);
PrefernceSettings.setRESBILLINGSHIPPING(res);
try{
JSONArray jArray = new JSONArray(res);
int number =  jArray.length();
number = number-1 ;
for(int i =0 ;i<jArray.length(); i++){
JSONObject jObject = jArray.getJSONObject(i);
String avv =  jObject.getString("aid");

if(i==0){
PrefernceSettings.setBILLING(avv);
PrefernceSettings.setSHIPPING(avv);
}
}
}catch (Exception e) {
e.printStackTrace();
}
}else{
Toast toast = Toast.makeText(getActivity(), "Please check your internet connection", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}



return v;
}


private List<CartItem> getCartItems() {
  List<CartItem> cartItems = new ArrayList<CartItem>();
  Cursor allitem = MainActivity.dbHelpercart.selectRecords();
  if (allitem.moveToFirst()) {
   do {
    CartItem cartItem = new CartItem();
    ItemDetails k = new ItemDetails();
    k.setNid(allitem.getLong(1));
    k.setTitle(allitem.getString(2));
    k.setThumb_Images(allitem.getString(3));
    k.setQty(allitem.getInt(4));
    Log.v("", allitem.getString(5));
    k.setPrice(allitem.getString(5));
    k.setSizeselected(allitem.getString(6));
    k.setmax(allitem.getString(7));
    cartItem.setProduct(k);
    cartItem.setQuantity(allitem.getInt(4));
    Log.v("", allitem.getString(4));
    cartItems.add(cartItem);
   } while (allitem.moveToNext());
  }
  allitem.close();


  return cartItems;
}







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

}

@Override
protected String doInBackground(String... params) {
String abc = PrefernceSettings.getUserId();
String res = BaseActivity.getJsonnew(getActivity(), Constant.CartDetails() + abc);

try {
JSONArray jArray = new JSONArray(res);
int number = jArray.length();
number = number - 1;
for (int i = 0; i < jArray.length(); i++) {
JSONObject jObject = jArray.getJSONObject(i);

ItemDetails k = new ItemDetails();
k.setNid(Long.parseLong((String) jObject.getString("nid")));
String Title = jObject.getString("title");
if (Title.contains("GV")) {
Title = Title + "|" + jObject.getString("message")
+ "|" + jObject.getString("mail");
k.setTitle(Title);
} else {
k.setTitle(jObject.getString("title"));
}
Log.v("Title", k.getTitle());
String price = jObject.getString("price");
double f = Double.parseDouble(price);
k.setPrice(String.format("%.2f", new BigDecimal(f)));
k.setQty(Integer.parseInt((String) jObject.getString("qty")));
k.setThumb_Images(((String) jObject.getString("thumb_images")));
k.setSizeselected(((String) jObject.getString("size_id")));
k.setmax(jObject.getString("total_stock"));
Log.v("", jObject.getString("title"));
itemListsys.add(k);

}

} catch (Exception e) {
e.printStackTrace();
return "not";
}
return null;
}

protected void onPostExecute(String args) {

if (itemListsys.size() > 0) {

for (int i = 0; i < itemListsys.size(); i++) {
MainActivity.dbHelpercart.inserRecord(
((ItemDetails) itemListsys.get(i)).getNid(),
((ItemDetails) itemListsys.get(i)).getTitle(),
((ItemDetails) itemListsys.get(i)).getThumb_Images(),
((ItemDetails) itemListsys.get(i)).getQty(),
(String) ((ItemDetails) itemListsys.get(i)).getPrice(),
((ItemDetails) itemListsys.get(i)).getSizeselected(),
((ItemDetails) itemListsys.get(i)).getmax());

}

}

cartItemAdapter.updateCartItems(getCartItems());

lvCartItems.setAdapter(cartItemAdapter);
if( cartItemAdapter.getCount()>0){
ssd.setVisibility(View.GONE);
_rel.setVisibility(View.VISIBLE);
}else{
ssd.setVisibility(View.VISIBLE);
_rel.setVisibility(View.GONE);

}
MainActivity.tx.setText(MainActivity.dbHelpercart.countRecord() + "");
tvTotalPrice.setText(String.valueOf(BigDecimal.valueOf(MainActivity.dbHelpercart.gettotalprice())));
}
}
@Override
public void onResume(){
    super.onResume();

}

private void switchFragment(Fragment fragment) {
getActivity(). getSupportFragmentManager().beginTransaction()
.replace(R.id.content_frame, fragment)
.addToBackStack("my_fragment").commitAllowingStateLoss();
}

public void onBackPressed() {
if (getFragmentManager().getBackStackEntryCount() > 0) {
getFragmentManager().popBackStack();
}
}

}

2. ProductDetails.java

package com.tutorialsee.database;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.squareup.picasso.Picasso;
import com.tutorialsee.http.BaseActivity;
import com.tutorialsee.http.Constant;
import com.tutorialsee.http.Network;
import com.tutorialsee.http.PrefernceSettings;
import com.tutorialsee.http.TouchImageView;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Paint;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

@SuppressLint({ "SetJavaScriptEnabled", "SimpleDateFormat" })
public class ProductDetails extends Fragment implements OnClickListener,OnItemSelectedListener {
protected static final String ARG_DETIALS_ID = null;
ArrayList<HashMap<String, String>> array1;
List<ItemDetails> itemList = new ArrayList<ItemDetails>();
LinearLayout addtocart, confim;
ImageView _btn,  _img_product, imageviewnew;
ItemDetails item;
Dialog dialog;
ViewPager mViewPager;
private ImageView[] dots;
private int currentIndex;
TextView _txt_name, _txt_rs, txt_webviewtext, txt_costrs;
Boolean back = false;
static String msg = "";
ItemDetails mItems;
int flag = 1;
ImageView btn_search, btn_logo;
EditText edt_search;
String flg = "L";
Context context;
HashMap<String, String> sizee = new HashMap<String, String>();
private static String[] pics;
DateFormat df;
String date;
Spinner spinner;
private List<View> views;
private ViewPagerAdapter vpAdapter;
String[] spinnersArray = null;
String value;
ArrayAdapter<String> dataAdapters;
int pidtest;
String quatest;
TextView addtocarsst;
String size_name;
int showsixe = 0;
int showsixes = 0;

@SuppressLint({ "CutPasteId", "SetJavaScriptEnabled" })
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MainActivity.lp = 2;

if (getArguments().containsKey(ARG_DETIALS_ID)) {
mItems = (ItemDetails) getArguments().getSerializable(ARG_DETIALS_ID);

}
}

@SuppressLint("SetJavaScriptEnabled")
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.product_details, container, false);

MainActivity.actionBar.show();
context = container.getContext();
PrefernceSettings.openDataBase(context);

String tt = mItems.getImagePath1();
Log.v("tag", mItems.getImagePath1());
if ((mItems.getFullImages() != null)
&& !(mItems.getFullImages().contains("null"))) {
tt = tt + "," + mItems.getFullImages();
}
if ((mItems.getImagePath() != null)
&& !(mItems.getImagePath().contains("null"))) {
tt = tt + "," + mItems.getImagePath();
}
Log.v("tt", tt);
pics = tt.split(",");
Log.v("tt1", "" + pics.length);


addtocart = (LinearLayout) v.findViewById(R.id.addtocart);
addtocarsst = (TextView) v.findViewById(R.id.addtocarsst);
confim = (LinearLayout) v.findViewById(R.id.confim);
imageviewnew = (ImageView) v.findViewById(R.id.imageviewnew);
spinner = (Spinner) v.findViewById(R.id.spinner);
final Spinner spinners = (Spinner) v.findViewById(R.id.spinners);
spinner.setOnItemSelectedListener(this);
spinners.setOnItemSelectedListener(this);


confim.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
if (MainActivity._ordd.getText().toString().contains("Login")) {

String abc = PrefernceSettings.getUserId();
mItems.setmax(PrefernceSettings.getMax());
showsixe = 0;
try {
showsixe = MainActivity.dbHelpercart
.countRecordbynid(mItems.getNid());
} catch (Exception ex) {
}
MainActivity.dbHelpercart.inserRecord(mItems.getNid(),mItems.getTitle(),
                              mItems.getThumb_Images(),Integer.valueOf(PrefernceSettings.getQuantity()),
                              mItems.getPrice().toString(),mItems.getSizeselected(), mItems.getmax());

MainActivity.tx.setText(MainActivity.dbHelpercart.countRecord() + "");

if (!abc.isEmpty()) {
pidtest = (int) mItems.getNid();
quatest = PrefernceSettings.getQuantity();
size_name = (String) mItems.getSizeselected();
Cartcheck ck = new Cartcheck();
ck.execute();

} else {
if (Integer.parseInt(mItems.getmax()) > showsixe) {
Toast toast = Toast.makeText(context,"Added to Cart", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();

} else {
Toast toast = Toast.makeText(context,"There is only " + mItems.getmax()+ " items in stock",Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
}

} else {

String abc = PrefernceSettings.getUserId();
mItems.setmax(PrefernceSettings.getMax());
try {
showsixe = MainActivity.dbHelpercart.countRecordbynid(mItems.getNid());
} catch (Exception ex) {
}
MainActivity.dbHelpercart.inserRecord(mItems.getNid(),mItems.getTitle(),
                              mItems.getThumb_Images(),Integer.valueOf(PrefernceSettings.getQuantity()),
                              mItems.getPrice().toString(),mItems.getSizeselected(), mItems.getmax());

MainActivity.tx.setText(MainActivity.dbHelpercart.countRecord() + "");

if (!abc.isEmpty()) {
pidtest = (int) mItems.getNid();
quatest = PrefernceSettings.getQuantity();
size_name = (String) mItems.getSizeselected();
Cartchecks ck = new Cartchecks();
ck.execute();
} else {
if (Integer.parseInt(mItems.getmax()) > showsixe) {
Toast toast = Toast.makeText(context,"Added to Cart", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();

} else {
Toast toast = Toast.makeText(context,"There is only " + mItems.getmax()+ " items in stock",Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
}
}
}
});
imageviewnew.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {

ViewDialog alert = new ViewDialog();
alert.showDialog(getActivity(), "Error de conexión al servidor");

}
});


List<String> categories = new ArrayList<String>();
String sp = mItems.getSize();
String[] spinnerArray = null;

if (!sp.contains("null")) {

JSONObject jsObject;
try {
jsObject = new JSONObject(sp);
spinnerArray = new String[jsObject.length()];
Iterator<String> iter = jsObject.keys();
int t = 0;
while (iter.hasNext()) {
String key = iter.next();
try {
String value = (String) jsObject.get(key);
sizee.put(value, key);
spinnerArray[t] = value;
Log.v(value, key);
Log.v("AAAAAAAAAAAAAAAAAAAAAAA", value);
Log.v("BBBBBBBBBBBBBBBBBBBBBBB", key);
t++;

} catch (JSONException e) {
}

}
} catch (JSONException e1) {
e1.printStackTrace();
}
}

// Spinner1 Drop down elements
if (spinnerArray != null) {
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item,spinnerArray);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,int position, long id) {
String selected = parent.getItemAtPosition(position).toString();
String id1 = sizee.get(selected);
mItems.setSizeselected(id1);
String spp = mItems.getSize_Stock();
Log.v("spp", spp);

JSONObject jsObject;
try {
jsObject = new JSONObject(spp);
value = (String) jsObject.get(selected);
int t = 0;

spinnersArray = new String[Integer.parseInt(value)];
while (t < Integer.parseInt(value)) {
spinnersArray[t] = String.valueOf(t + 1);
t++;
dataAdapters = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_spinner_item,spinnersArray);
dataAdapters.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinners.setAdapter(dataAdapters);
dataAdapters.notifyDataSetChanged();
PrefernceSettings.setMax(value);
Log.v("Max Max ", PrefernceSettings.getMax());
}

Log.v("spinner value", value);
} catch (JSONException e1) {
e1.printStackTrace();
}


}

@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}

// Spinner2 Drop down elements
List<String> categoriess = new ArrayList<String>();
String spp = mItems.getSize_Stock();

spinners.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,int position, long id) {
String selecteds = parent.getItemAtPosition(position).toString();
PrefernceSettings.setQuantity(selecteds);
Log.v("Quantity count", selecteds);
}

@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});


mItems.getNid();
mItems.getThumb_Images();
mItems.getPrice();
mItems.getBody();
mItems.getCost_Price();
_txt_name = (TextView) v.findViewById(R.id.txt_name);
_txt_rs = (TextView) v.findViewById(R.id.txt_rs);
txt_costrs = (TextView) v.findViewById(R.id.txt_costrs);
txt_costrs.setPaintFlags(txt_costrs.getPaintFlags()| Paint.STRIKE_THRU_TEXT_FLAG);
_txt_name.setText("Felt Laptop Sleeve (Charcoal)");
_txt_rs.setText(mItems.getPrice());

if (mItems.getCost_Price() != null) {
if (mItems.getCost_Price().charAt(0) == '0') {
txt_costrs.setVisibility(View.GONE);
} else {
txt_costrs.setVisibility(View.VISIBLE);
txt_costrs.setText(mItems.getCost_Price());
}
}

//Picasso.with(getActivity()).load("http://homemonkey.com/timthumb.php?src="+ mItems.getThumb_Images().replace(" ", "%20")+ "&w=400&h=0").placeholder(R.drawable.load).into(imageviewnew);
Picasso.with(getActivity()).load(R.drawable.product).placeholder(R.drawable.load).into(imageviewnew);


String stringLitersOfPetrol = null;
df = new SimpleDateFormat("HH:mm");





addtocart.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String abc = PrefernceSettings.getUserId();
mItems.setmax(PrefernceSettings.getMax());
showsixe = 0;

try {
showsixe = MainActivity.dbHelpercart.countRecordbynid(mItems.getNid());
} catch (Exception ex) {
}

MainActivity.dbHelpercart.inserRecord(mItems.getNid(),mItems.getTitle(),
                              mItems.getThumb_Images(),Integer.valueOf(PrefernceSettings.getQuantity()),
                              mItems.getPrice().toString(), mItems.getSizeselected(),
                              mItems.getmax());

MainActivity.tx.setText(MainActivity.dbHelpercart.countRecord()+ "");

if (!abc.isEmpty()) {
pidtest = (int) mItems.getNid();
quatest = PrefernceSettings.getQuantity();
size_name = (String) mItems.getSizeselected();
Cartcheck ck = new Cartcheck();
ck.execute();
} else {
Log.v("aa", "" + showsixe);
Log.v("bb", "" + mItems.getmax());

if (Integer.parseInt(mItems.getmax()) > showsixe) {
Toast toast = Toast.makeText(context, "Added to Cart",Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
} else {
Toast toast = Toast.makeText(context, "There is only "+ Integer.parseInt(mItems.getmax())
+ " items in stock", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}

}
}
});



return v;
}

private String getTimingCategory() {
date = df.format(Calendar.getInstance().getTime());

return String.valueOf(date);
}



@Override
public void onClick(View v) {


}


//***************************  Product Image View & Touch ViewDialog Open  ***********************

public class ViewDialog implements OnClickListener, OnPageChangeListener {
@SuppressWarnings("deprecation")
public <AddListAddressd> void showDialog(Activity getActivity,String msg) {

dialog = new Dialog(getActivity);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(false);
dialog.setContentView(R.layout.imageviewfull);

ImageView gobtnnsssdd = (ImageView) dialog.findViewById(R.id.gobtnnsssdd);
mViewPager = (ViewPager) dialog.findViewById(R.id.view_pager);
mViewPager.setAdapter(new TouchImageAdapter());

gobtnnsssdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.cancel();
}
});
vpAdapter = new ViewPagerAdapter(views);

mViewPager.setOnPageChangeListener(new OnPageChangeListener() {
public void onPageScrollStateChanged(int state) {

}

public void onPageScrolled(int position, float positionOffset,int positionOffsetPixels) {

}

public void onPageSelected(int position) {
if (position < 0 || position > pics.length - 1 || currentIndex == position) {
return;
}
dots[position].setEnabled(false);
dots[currentIndex].setEnabled(true);
currentIndex = position;
}
});

initDots();

views = new ArrayList<View>();
LinearLayout.LayoutParams mParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);

for (int i = 0; i < pics.length; i++) {
if (pics[i] != null) {
if (!pics[i].toString().contains("null")) {
ImageView iv = new ImageView(getActivity());
iv.setLayoutParams(mParams);
Log.v("aaaaaaaaaaaaaaaaaaaaaaaaa", pics[i] + i + ","+ pics.length);
//Picasso.with(getActivity()).load(pics[i].toString().replace(" ", "%20")+ "&w=0&h=0").into(iv);
Picasso.with(getActivity()).load(R.drawable.product).placeholder(R.drawable.load).into(iv);

views.add(iv);
}
}
}

dialog.show();
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(dialog.getWindow().getAttributes());
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.MATCH_PARENT;
dialog.getWindow().setAttributes(lp);
}

class TouchImageAdapter extends PagerAdapter {

@Override
public int getCount() {
return pics.length;
}

@Override
public View instantiateItem(ViewGroup container, int position) {
if (pics[position] != null&& (!pics[position].toString().contains("null"))) {

TouchImageView img = new TouchImageView(container.getContext());
Picasso.with(getActivity()).load(R.drawable.product).placeholder(R.drawable.load).into(img);
//Picasso.with(container.getContext()).load(pics[position].replace(" ", "%20")).placeholder(R.drawable.load).into(img);
container.addView(img,LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);

return img;
} else {
return null;
}

}

@Override
public void destroyItem(ViewGroup container, int position,Object object) {
container.removeView((View) object);
}

@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}

}

// View Pager code
private void initDots() {
LinearLayout ll = (LinearLayout) dialog.findViewById(R.id.ll);
dots = new ImageView[pics.length];
for (int i = 0; i < 3; i++) {
ImageView ivvvv = (ImageView) ll.getChildAt(i);
ivvvv.setVisibility(View.GONE);
}
for (int i = 0; i < pics.length; i++) {
dots[i] = (ImageView) ll.getChildAt(i);
dots[i].setVisibility(View.VISIBLE);
dots[i].setEnabled(true);
dots[i].setOnClickListener((OnClickListener) context);
dots[i].setTag(i);
}

currentIndex = 0;
dots[currentIndex].setEnabled(false);
}

@Override
public void onPageScrollStateChanged(int arg0) {
}

@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}

@Override
public void onPageSelected(int arg0) {
}

@Override
public void onClick(View arg0) {
int position = (Integer) arg0.getTag();

}

}

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
}

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





//*******************************  Card Details  **********************************

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

@Override
protected void onPreExecute() {
super.onPreExecute();

}

@Override
protected String doInBackground(String... params) {
String abc = PrefernceSettings.getUserId();

String UERRR = Constant.Cart() + abc + "&pid=" + pidtest + ""
                             + "&qty=" + quatest + "&size_name=" + size_name
                             + "&action=add";

Log.v("New URL", UERRR);
String res = BaseActivity.getJsonnew(context, UERRR);
Log.v("URL", res);

try {
JSONObject jObject = new JSONObject(res);
msg = jObject.getString("response_message");
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

protected void onPostExecute(String args) {
    Toast.makeText(context, msg,Toast.LENGTH_LONG).show();
if (Network.isConnectionFast(context)) {
itemList.clear();
Cartdetails ck = new Cartdetails();
ck.execute();
} else {
Toast.makeText(context, "Person information is null",Toast.LENGTH_LONG).show();
}
}
}

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

@Override
protected String doInBackground(String... params) {
String abc = PrefernceSettings.getUserId();
String res = BaseActivity.getJsonnew(getActivity(),Constant.CartDetails() + abc);

try {
JSONArray jArray = new JSONArray(res);
int number = jArray.length();
number = number - 1;

for (int i = 0; i < jArray.length(); i++) {
JSONObject jObject = jArray.getJSONObject(i);

ItemDetails k = new ItemDetails();
k.setNid(Long.parseLong((String) jObject.getString("nid")));
k.setTitle(jObject.getString("title"));
String price = jObject.getString("price");
double f = Double.parseDouble(price);
k.setPrice(String.format("%.2f", new BigDecimal(f)));
k.setQty(Integer.parseInt((String) jObject.getString("qty")));
k.setThumb_Images(((String) jObject.getString("thumb_images")));
k.setSizeselected(((String) jObject.getString("size_id")));
k.setmax(jObject.getString("total_stock"));
Log.v("", jObject.getString("title"));
itemList.add(k);

}

} catch (Exception e) {
e.printStackTrace();
return null;
}
return null;
}

protected void onPostExecute(String args) {

if (itemList.size() > 0) {
for (int i = 0; i < itemList.size(); i++) {
MainActivity.dbHelpercart.inserRecord(((ItemDetails) itemList.get(i)).getNid(),
  ((ItemDetails) itemList.get(i)).getTitle(),
                      ((ItemDetails) itemList.get(i)).getThumb_Images(),
                          ((ItemDetails) itemList.get(i)).getQty(),
             (String) ((ItemDetails) itemList.get(i)).getPrice(),
                      ((ItemDetails) itemList.get(i)).getSizeselected(),
                      ((ItemDetails) itemList.get(i)).getmax());

}
MainActivity.tx.setText(MainActivity.dbHelpercart.countRecord()+ "");
}
}
}

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

@Override
protected void onPreExecute() {
super.onPreExecute();
}

@Override
protected String doInBackground(String... params) {
String abc = PrefernceSettings.getUserId();

String UERRR = Constant.Cart() + abc + "&pid=" + pidtest + ""
   + "&qty=" + quatest + "&size_name=" + size_name
                       + "&action=add";
Log.v("New URL", UERRR);
String res = BaseActivity.getJsonnew(context, UERRR);
Log.v("URL", res);

try {
JSONObject jObject = new JSONObject(res);
msg = jObject.getString("response_message");
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

protected void onPostExecute(String args) {
Toast.makeText(context, msg,Toast.LENGTH_LONG).show();
if (Network.isConnectionFast(context)) {
itemList.clear();
Cartdetailss ck = new Cartdetailss();
ck.execute();
} else {
Toast.makeText(context, "Person information is null",Toast.LENGTH_LONG).show();
}
}
}

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

@Override
protected String doInBackground(String... params) {
String abc = PrefernceSettings.getUserId();
String res = BaseActivity.getJsonnew(getActivity(),Constant.CartDetails() + abc);
try {
JSONArray jArray = new JSONArray(res);
int number = jArray.length();
number = number - 1;

for (int i = 0; i < jArray.length(); i++) {
JSONObject jObject = jArray.getJSONObject(i);

ItemDetails k = new ItemDetails();
k.setNid(Long.parseLong((String) jObject.getString("nid")));
k.setTitle(jObject.getString("title"));
String price = jObject.getString("price");
double f = Double.parseDouble(price);
k.setPrice(String.format("%.2f", new BigDecimal(f)));
k.setQty(Integer.parseInt((String) jObject.getString("qty")));
k.setThumb_Images(((String) jObject.getString("thumb_images")));
k.setSizeselected(((String) jObject.getString("size_id")));
k.setmax(jObject.getString("total_stock"));
Log.v("", jObject.getString("title"));
itemList.add(k);

}

} catch (Exception e) {
e.printStackTrace();
return null;
}
return null;
}

protected void onPostExecute(String args) {

if (itemList.size() > 0) {
for (int i = 0; i < itemList.size(); i++) {
MainActivity.dbHelpercart.inserRecord(((ItemDetails) itemList.get(i)).getNid(),
                      ((ItemDetails) itemList.get(i)).getTitle(),
                      ((ItemDetails) itemList.get(i)).getThumb_Images(),
                      ((ItemDetails) itemList.get(i)).getQty(),
             (String) ((ItemDetails) itemList.get(i)).getPrice(),
                      ((ItemDetails) itemList.get(i)).getSizeselected(),
                      ((ItemDetails) itemList.get(i)).getmax());

}
MainActivity.tx.setText(MainActivity.dbHelpercart.countRecord() + "");
}
}
}

private void switchFragment(Fragment fragment) {
getActivity().getSupportFragmentManager().beginTransaction()
.replace(R.id.content_frame, fragment)
.addToBackStack("my_fragment").commitAllowingStateLoss();
}

public void onBackPressed() {
if (getFragmentManager().getBackStackEntryCount() > 0) {
getFragmentManager().popBackStack();
}

}
}

3. carddesign.xml

<?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:orientation="vertical" >
      <LinearLayout
        android:id="@+id/ff"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="2dp"
        android:background="@color/kilol"
        android:orientation="horizontal" >

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="0.1"
             android:gravity="center"
            
            android:background="@color/white"
            android:orientation="horizontal" >

            <ImageView
                android:id="@+id/Img_product"
                android:layout_width="70dp"
                android:layout_height="50dp"
                android:layout_marginLeft="5dp"
                android:contentDescription="@drawable/load"
              
                android:src="@drawable/load" />
        </LinearLayout>

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

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

                <TextView
                    android:id="@+id/txt_name"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1.8"
                    android:ems="13"
                    android:padding="10dp"
                    android:text="Best Sales  sfdf I fdfd fsdf dffd df df dftems "
                    android:textColor="@color/black"
                    android:textSize="16sp" />

                <ImageView
                    android:id="@+id/Img_delete"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginLeft="15dp"
                    android:layout_weight="0.1"
                    android:background="#ffffff"
                    android:contentDescription="@drawable/closes"
                    android:src="@drawable/closes" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="10dp"
                android:layout_marginLeft="10dp"
                android:background="@color/white"
                android:orientation="horizontal" >

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

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

                        <TextView
                            android:id="@+id/txt_rupdde"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="Rs. "
                            android:textColor="@color/kilol"
                            android:textSize="16sp"
                            android:textStyle="bold" />

                        <TextView
                            android:id="@+id/txt_rupddcce"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="500"
                            android:textColor="@color/kilol"
                            android:textSize="16sp"
                            android:textStyle="bold" />
                         
                        
                        
                       
                    </LinearLayout>

                    <LinearLayout
                        android:layout_width="130dp"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="5dp"
                        android:background="@color/white"
                        android:orientation="horizontal" >

                        <TextView
                            android:id="@+id/txt_rupddde"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="Size: "
                            android:textColor="@color/kilol"
                            android:textSize="14sp" />

                      
                       <TextView
                            android:id="@+id/txt_size"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginLeft="3dp"
                            android:textColor="@color/kilol"
                            android:textSize="14sp" />
                    </LinearLayout>
                </LinearLayout>

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="30dp"
                    android:layout_marginTop="15dp"
                    android:layout_weight="1"
                    android:background="@color/white"
                    android:orientation="horizontal" >

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_margin="0.2dp"
                        android:background="@color/white"
                        android:orientation="horizontal" >

                        <Button
                            android:id="@+id/btn_minus"
                            android:layout_width="20dp"
                            android:layout_height="wrap_content"
                            android:layout_marginLeft="30dp"
                            android:background="@color/black"
                            android:text="-"
                            android:textColor="@color/white" />

                        <TextView
                            android:id="@+id/edit_text"
                            android:layout_width="45dp"
                            android:layout_height="match_parent"
                            android:background="#EEE2E2"
                            android:focusable="false"
                            android:gravity="center"
                            android:inputType="number"
                            android:text="0" />

                        <Button
                            android:id="@+id/btn_plus"
                            android:layout_width="20dp"
                            android:layout_height="wrap_content"
                            android:background="@color/black"
                            android:text="+"
                            android:textColor="@color/white" />
                    </LinearLayout>
                </LinearLayout>
            </LinearLayout>
        </LinearLayout>
</LinearLayout>
</LinearLayout>

Comments

Email Address: 

Cannot open in android studio. build.gradle is missing. How to open in android studio. Please help me?

Email Address: 

I have download your code from github. But it shows error in gradle. It cannot run in android studio. Please solve the issue fast.

Email Address: 

hey @t.mfazil ...Sorry for my disappearance this link download android studio cart project i hope you are enjoying:)
http://www.tutorialsee.com/DownloadFile.zip

Email Address: 

Thanks it helps me a lot

Email Address: 

Nice work, but whenever i click for loading products it's show the toast message (check for the internet connection)

Email Address: 

Please check Network.java class file. Then use isConnectionFast() method your issue is solved

Email Address: 

Online shopping is increasing rapidly. Once a time, when nobody trust on eCommerce but now we used eCommerce for shopping because it has many advantages like time and money saving. Till today, there are much lacks in eCommerce field but if we compare to yourself to third world countries than we come to know that eCommerce industry has developed. People earned handsome amount from eCommerce in form of commissions. Most of companies used this business like affiliate cms designers ileaddigital(.)com/services/web-design/affiliate-cms-development. Traditional enterprises who want to move to e-business need to invest huge resources in developing a new game plan. But as soon as this investment is made, E-business provides many advantages for the investor. Some of these include: Reduced overhead costs, while the transition from traditional to electronic business requires a lot of cost, but once the process is completed, the cost of doing business decreases.

Email Address: 

Yes you are right

Email Address: 

Hello, kilol_activity shows no internet connection.
How can I fix this issue?

Thanks for your reply

Email Address: 

Please check Network.java class file. Then use isConnectionFast() method your issue is solved

Email Address: 

The items are not displayed.i am getting "no records found" as soon as I open.... What's the reason?
Please answer....

Email Address: 

put the URL then no records found problem solved thank you