Omkar
Omkar

Reputation: 1

Cart item Count increment/decrement & add to cart

Hi everyone i am using custom listview for getting data from server and show in listview.. I am able to get data and show it in listview, but I don't know to implement the click event of button inside listitem. There are two buttons to increement and decrement qty. My clicklistener is working but its not working in right manner. When I click increment or decrement a product it should be added to cart. Please help me correcting this issue. I did search too many posts in , but was unable to understand it...

Here my Adapter Class code:

public class ProductsAdapter extends BaseAdapter {
private ImageLoadingListener animateFirstListener = new AnimateFirstDisplayListener();

private Context context;
private ArrayList<HashMap<String, String>> postItems;
public SharedPreferences settings;
public final String PREFS_NAME = "Products";

DisplayImageOptions options;
ImageLoaderConfiguration imgconfig;

private static int _counter = 0;
private String _stringVal;

public ProductsAdapter(Context context, ArrayList<HashMap<String, String>> arraylist){
    this.context = context;

    File cacheDir = StorageUtils.getCacheDirectory(context);
    options = new DisplayImageOptions.Builder()
    .showImageOnLoading(R.drawable.loading)
    .showImageForEmptyUri(R.drawable.loading)
    .showImageOnFail(R.drawable.loading)
    .cacheInMemory(true)
    .cacheOnDisk(true)
    .considerExifParams(true)
    .displayer(new SimpleBitmapDisplayer())
    .imageScaleType(ImageScaleType.EXACTLY)
    .build();

    imgconfig = new ImageLoaderConfiguration.Builder(context)
    .build();
    ImageLoader.getInstance().init(imgconfig);

    postItems = arraylist;
    settings = context.getSharedPreferences(PREFS_NAME, 0);     

}

@Override
public int getCount() {
    return postItems.size();
}
@Override
public Object getItem(int position) {       
    return postItems.get(position);
}

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

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            LayoutInflater mInflater = (LayoutInflater)
            context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            convertView = mInflater.inflate(R.layout.row_products, null);   
        }
        final HashMap<String, String> map = postItems.get(position);

        ImageView imgProduct = (ImageView)convertView.findViewById(R.id.proimage);
        ImageLoader.getInstance().displayImage(ConstValue.PRO_IMAGE_BIG_PATH+map.get("image"), imgProduct, options, animateFirstListener);

        TextView txtTitle = (TextView)convertView.findViewById(R.id.proTitle);
        txtTitle.setText(map.get("title"));

        TextView txtPrice = (TextView)convertView.findViewById(R.id.txtprice);
        txtPrice.setText(map.get("price"));

        TextView textDiscount = (TextView)convertView.findViewById(R.id.textDiscount);
        textDiscount.setVisibility(View.GONE);

        TextView txtDiscountFlag = (TextView)convertView.findViewById(R.id.textDiscountFlag);
        txtDiscountFlag.setVisibility(View.GONE);

        TextView textCurrency = (TextView)convertView.findViewById(R.id.textCurrency);
        textCurrency.setText(map.get("currency"));

        final TextView value = (TextView)convertView.findViewById(R.id.textView3);

        Button txtminus = (Button) convertView.findViewById(R.id.minus);

        txtminus.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Log.d("src", "Decreasing value...");
            _counter--;
            _stringVal = Integer.toString(_counter);
            value.setText(_stringVal);

            if (_counter < 0)
            {
                value.setText("0");
            }

        }
        });

        Button txtplus  = (Button) convertView.findViewById(R.id.plus);
        txtplus.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d("src", "Increasing value...");
            _counter++;
            _stringVal = Integer.toString(_counter);
            value.setText(_stringVal);
        }
        });


        if(!map.get("discount").equalsIgnoreCase("") && !map.get("discount").equalsIgnoreCase("0")){
            Double discount = Double.parseDouble(map.get("discount"));
            Double price = Double.parseDouble(map.get("price"));
            Double discount_amount =  discount * price / 100;

            Double effected_price = price - discount_amount ; 
            //txtPrice.setBackgroundResource(R.drawable.strike_trough);
            txtPrice.setPaintFlags(txtPrice.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
            textDiscount.setVisibility(View.VISIBLE);
            textDiscount.setText(effected_price.toString());

            txtDiscountFlag.setVisibility(View.VISIBLE);
            txtDiscountFlag.setText(discount+"% off");
        }

        TextView txtUnit = (TextView)convertView.findViewById(R.id.txtunit);
        txtUnit.setText(map.get("gmqty"));

        TextView txtgm = (TextView)convertView.findViewById(R.id.txtgm);
        txtgm.setText(map.get("unit"));

        int a = Integer.parseInt(map.get("total_qty_stock").toString());
        int b = Integer.parseInt(map.get("consume_qty_stock").toString());

        int result = a - b;

        TextView txtstock = (TextView)convertView.findViewById(R.id.stock);
        txtstock.setText(String.valueOf(result)+" "+map.get("type")+" In Stock");
        //txtstock.setText(map.get("stock")+" "+map.get("type")+" In Stock");


    return convertView;
}

}

result

Here my ProductActivity Code

public class ProductsActivity extends ActionBarActivity {

public SharedPreferences settings;
public ConnectionDetector cd;
static ArrayList<HashMap<String, String>> products_array;
ProductsAdapter adapter;
ListView products_listview;
DisplayImageOptions options;
ImageLoaderConfiguration imgconfig;
ProgressDialog dialog;

TextView txtcount;
Button btn;

private ImageLoadingListener animateFirstListener = new AnimateFirstDisplayListener();
HashMap<String, String>  catMap;
@SuppressWarnings("unchecked")
@Override
protected void onCreate(Bundle savedInstanceState) {
    settings = getSharedPreferences(ConstValue.MAIN_PREF, 0);
    cd = new ConnectionDetector(getApplicationContext());

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_products);

/*  ActionBar mActionBar = getActionBar();
    mActionBar.setDisplayShowHomeEnabled(true);
    mActionBar.setDisplayShowTitleEnabled(true);
    LayoutInflater mInflater = LayoutInflater.from(this);

    View mCustomView = mInflater.inflate(R.layout.custom_actionbar, null);
    TextView mTitleTextView = (TextView) mCustomView.findViewById(R.id.title_text);
    mTitleTextView.setText("My Own Title");

    mActionBar.setCustomView(mCustomView);
    mActionBar.setDisplayShowCustomEnabled(true); */

    settings = getSharedPreferences(ConstValue.MAIN_PREF, 0);
    cd=new ConnectionDetector(this);

    File cacheDir = StorageUtils.getCacheDirectory(this);
    options = new DisplayImageOptions.Builder()
    .showImageOnLoading(R.drawable.loading)
    .showImageForEmptyUri(R.drawable.loading)
    .showImageOnFail(R.drawable.loading)
    .cacheInMemory(true)
    .cacheOnDisk(true)
    .considerExifParams(true)
    .displayer(new SimpleBitmapDisplayer())
    .imageScaleType(ImageScaleType.NONE)
    .build();

    imgconfig = new ImageLoaderConfiguration.Builder(this)
    .build();
    ImageLoader.getInstance().init(imgconfig);

    ArrayList<HashMap<String,String>> categoryArray = new ArrayList<HashMap<String,String>>();
    try {
        categoryArray = (ArrayList<HashMap<String,String>>) ObjectSerializer.deserialize(settings.getString("categoryname", ObjectSerializer.serialize(new ArrayList<HashMap<String,String>>())));
    }catch (IOException e) {
            e.printStackTrace();
    }

    catMap = new HashMap<String, String>();
    catMap = categoryArray.get(getIntent().getExtras().getInt("position"));

    products_array = new ArrayList<HashMap<String,String>>();
    try {
        products_array = (ArrayList<HashMap<String,String>>) ObjectSerializer.deserialize(settings.getString("products_"+catMap.get("id"), ObjectSerializer.serialize(new ArrayList<HashMap<String,String>>())));
    }catch (IOException e) {
            e.printStackTrace();
    }

    products_listview = (ListView)findViewById(R.id.listView1);
    adapter = new ProductsAdapter(getApplicationContext(), products_array);
    products_listview.setAdapter(adapter);

    TextView txtTitle = (TextView)findViewById(R.id.catname);
    txtTitle.setText(catMap.get("name"));

    txtcount = (TextView)findViewById(R.id.textcount);

    btn = (Button) findViewById(R.id.button);

    products_listview.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, final int position,
                                long id) {
            // TODO Auto-generated method stub

            try {

                settings.edit().putString("selected_product", ObjectSerializer.serialize(products_array.get(position))).commit();



            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            Intent intent = new Intent(ProductsActivity.this, ProductdetailActivity.class);
            intent.putExtra("position", position);
            startActivity(intent);

        }
    });


    new loadProductsTask().execute(true);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}




public class loadProductsTask extends AsyncTask<Boolean, Void, ArrayList<HashMap<String, String>>> {

    JSONParser jParser;
    JSONObject json;
    String count;
    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();
    }

    @Override
    protected void onPostExecute(ArrayList<HashMap<String, String>> result) {
        // TODO Auto-generated method stub
        if (result!=null) {
            //adapter.notifyDataSetChanged();
        }   
        try {
            settings.edit().putString("products_"+catMap.get("id"), ObjectSerializer.serialize(products_array)).commit();

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        txtcount.setText(count);
        adapter.notifyDataSetChanged();
        super.onPostExecute(result);
    }

    @Override
    protected void onProgressUpdate(Void... values) {
        // TODO Auto-generated method stub
        super.onProgressUpdate(values);
    }

    @Override
    protected void onCancelled(ArrayList<HashMap<String, String>> result) {
        // TODO Auto-generated method stub
        super.onCancelled(result);
    }


    @Override
    protected ArrayList<HashMap<String, String>> doInBackground(
            Boolean... params) {
        // TODO Auto-generated method stub

        try {
            jParser = new JSONParser();

            if(cd.isConnectingToInternet())
            {
                String urlstring = ConstValue.JSON_PRODUCTS+"&id="+catMap.get("id");
                json = jParser.getJSONFromUrl(urlstring);
                count = json.getString("count");
                if (json.has("data")) {
                    if(json.get("data") instanceof JSONArray){
                        JSONArray jsonDrList = json.getJSONArray("data");
                        products_array.clear();
                        for (int i = 0; i < jsonDrList.length(); i++) {
                            JSONObject obj = jsonDrList.getJSONObject(i);
                            put_object(obj);
                        }
                    }else if(json.get("data") instanceof JSONObject){
                        put_object(json.getJSONObject("data"));
                    }
                }
            }else
            {
                Toast.makeText(ProductsActivity.this, "Please connect mobile with working Internet", Toast.LENGTH_LONG).show();
            }

            jParser = null;
            json = null;

            } catch (Exception e) {
                // TODO: handle exception

                return null;
            }
        return null;
    }

    public void put_object(JSONObject obj){
        HashMap<String, String> map = new HashMap<String, String>();


        try {
        map.put("id", obj.getString("id"));
        map.put("title", obj.getString("title"));
        map.put("slug", obj.getString("slug"));
        map.put("description", obj.getString("description"));       
        map.put("image", obj.getString("image"));

        map.put("price", obj.getString("price"));
        map.put("currency", obj.getString("currency"));
        map.put("discount", obj.getString("discount"));
        map.put("cod", obj.getString("cod"));       
        map.put("emi", obj.getString("emi"));       
        map.put("status", obj.getString("status"));     

        map.put("gmqty", obj.getString("gmqty"));       
        map.put("unit", obj.getString("unit"));     
        map.put("deliverycharge", obj.getString("deliverycharge"));     
        map.put("tax", obj.getString("tax"));       
        map.put("category_id", obj.getString("category_id"));

        map.put("on_date", obj.getString("on_date"));

        map.put("stock", obj.getString("stock"));
        map.put("type", obj.getString("type"));
        map.put("total_qty_stock", obj.getString("total_qty_stock"));
        map.put("consume_qty_stock", obj.getString("consume_qty_stock"));

        products_array.add(map);
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

}

Upvotes: 0

Views: 8622

Answers (3)

faranjit
faranjit

Reputation: 1637

Create an interface such as;

interface QuantityChangeListener {
    void onQuantityChanged(HashMap<String, String> map);
}

Implement this interface where you create adapter like above.

ProductsAdapter adapter = new ProductAdapter(this, "your list",
    new QuantityChangeListener() {
         @Override
         public void onQuantityChanged(HashMap<String, String> map) {
              // add item to cart
         }
     }
);

Pass this interface to constructor of ProductsAdapter class.

private QuantityChangeListener listener;
public ProductsAdapter(Context context, ArrayList<HashMap<String, String>> arraylist, QuantityChangeListener listener){
    this.listener = listener;
    // do other stuff
}

txtplus.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Log.d("src", "Increasing value...");
        _counter++;
        _stringVal = Integer.toString(_counter);
        value.setText(_stringVal);
        listener.onQuantityChanged(map);
    }
});

Upvotes: 0

Jeevanandhan
Jeevanandhan

Reputation: 1073

you can implement a interface in your activity class with the interface override method and pass the interface as argument to the adapter constructor. Let us create a interface named increment.

public interface Increment {
  void addCount(boolean ADD_FLAG);
}

Implement this in your activity.

Class CartActivity implements Increment {

@Override
public void addCount(boolean ADD_FLAG) {
if (ADD_FLAG) {
//If true increment..
} else{
//If false decrement..
}
}

Pass the interface as argument to the adapter constructor like below with your other argument.

Increment increment = null;

public ProductsAdapter(Context context, ArrayList<HashMap<String, String>> arraylist, Increment increment) {
  this.increment = increment;
}

And finally when in your click listener.

//Add this line to increment..
increment.addCount(true);

//Add this line to decrement..
    increment.addCount(false);

Hope this helps:)

Upvotes: 0

OneCricketeer
OneCricketeer

Reputation: 191884

This line...

private static int _counter = 0;

So when you click on any item in the list, you keep track of only one number for all items in the adapter, not each individual item.

I suggest you define some object oriented design to get a Cart and Product classes. That would allow you to keep track of items in a list with their details and quantity in a cleaner fashion than a Hashmap into a ListView.

Once you have a Product class, I recommend you look into how to implement an ArrayAdapter<Product>

Upvotes: 0

Related Questions