Android Open Source - Grocery-Mate Basket Adapter






From Project

Back to project page Grocery-Mate.

License

The source code is released under:

Apache License

If you think the Android project Grocery-Mate listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
* Copyright (C) 2011 iMellon// w  w w .j  a  va 2  s  .  c o  m
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package com.imellon.android.grocerymate.util;

import java.util.List;

import com.imellon.android.grocerymate.R;
import com.imellon.android.grocerymate.cache.MemoryCache;
import com.imellon.android.grocerymate.io.DownloadAsyncTask;
import com.imellon.android.grocerymate.io.DownloadAsyncTaskIface;
import com.imellon.android.grocerymate.io.JsonProcessorAsyncTask;
import com.imellon.android.grocerymate.io.JsonProcessorAsyncTaskInterface;
import com.imellon.android.grocerymate.io.DownloadAsyncTask.COM_ERROR;
import com.imellon.android.grocerymate.io.JsonProcessorAsyncTask.DATA_HANDLER_ERROR;
import com.imellon.android.grocerymate.model.AbstractObjectType;
import com.imellon.android.grocerymate.model.CompanyPoiPrice;
import com.imellon.android.grocerymate.model.CompanyPoiPriceList;
import com.imellon.android.grocerymate.model.Product;
import com.imellon.android.grocerymate.model.AbstractObjectType.DataType;

import android.app.ProgressDialog;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

/**
* @author Christos Papazafeiropoulos, Dimitris Makris
*/

public class BasketAdapter extends BaseAdapter {

  private LayoutInflater mInflater;
  private List<Product> data;
  private Context mContext;

  private ProgressDialog dialog;

  public BasketAdapter(Context context, List<Product> mList) {
    mContext = context;
    mInflater = LayoutInflater.from(context);
    data = mList;
  }

  public BasketAdapter(Context context) {
    mInflater = LayoutInflater.from(context);
  }

  public void setData(List<Product> mList) {
    data = mList;
  }

  public List<Product> getData() {
    return data;
  }

  public void addData(List<Product> mList) {
    data.addAll(mList);
  }

  public int getCount() {
    return data.size();
  }

  public Object getItem(int position) {
    return position;
  }

  public View getView(final int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    if (convertView == null) {
      convertView = mInflater.inflate(
          R.layout.activity_shopping_basket_list_item, null);

      holder = new ViewHolder();
      holder.product_title = (TextView) convertView
          .findViewById(R.id.product_title);
      holder.product_details = (ImageView) convertView
          .findViewById(R.id.product_details);
      holder.product_basket = (ImageView) convertView
          .findViewById(R.id.product_basket);
      holder.product_prices = (Gallery) convertView
          .findViewById(R.id.product_prices);

      convertView.setTag(holder);
    } else {
      holder = (ViewHolder) convertView.getTag();
    }

    holder.product_title.setText(data.get(position).Name);

    holder.product_details.setOnClickListener(new OnClickListener() {

      @Override
      public void onClick(View v) {

        if (MemoryCache.getInstance().getCompanyPoiPriceList(
            data.get(position).ProductId) == null)
          new DownloadAsyncTask(new DownloadAsyncTaskIface() {

            @Override
            public void onTaskStart() {
              dialog = ProgressDialog.show(mContext, "",
                  mContext.getString(R.string.loading), true);
            }

            @Override
            public void onTaskFinished(String result,
                String... newParam) {
              new JsonProcessorAsyncTask(
                  new JsonProcessorAsyncTaskInterface() {

                    @Override
                    public void onTaskFinished(
                        AbstractObjectType hamo) {
                      dialog.dismiss();
                      if (hamo instanceof CompanyPoiPriceList) {
                        MemoryCache
                            .getInstance()
                            .putCompanyPoiPriceList(
                                data.get(position).ProductId,
                                (CompanyPoiPriceList) hamo);
                        notifyDataSetChanged();
                      }
                    }

                    @Override
                    public void onErrorOccured(
                        DATA_HANDLER_ERROR error) {
                      dialog.dismiss();
                    }

                    @Override
                    public void onTaskFinished(
                        AbstractObjectType hamo,
                        TextView t) {
                      dialog.dismiss();
                    }
                  }, null).execute(result,
                  DataType.PRODUCT_PRICES_COMPANY_BASED
                      .toString());
            }

            @Override
            public void onErrorOccured(COM_ERROR error,
                String reason) {
              dialog.dismiss();
              Log.d("BasketAdapter",
                  "Error is: " + error.toString()
                      + " reason: " + reason);

            }
          }).execute(GorceryMateUriHandler
              .getCompanyPoiPriceListFormatedURL(MemoryCache
                  .getInstance().getAllProducts()
                  .get(position).ProductId));
      }
    });

    holder.product_basket.setOnClickListener(new OnClickListener() {

      @Override
      public void onClick(View v) {
        MemoryCache.getInstance().removeProduct(
            data.get(position).ProductId);
        Toast.makeText(mContext, R.string.success_remove,
            Toast.LENGTH_SHORT).show();

        setData(MemoryCache.getInstance().getAllProducts());
        notifyDataSetChanged();
      }
    });

    if (MemoryCache.getInstance().getCompanyPoiPriceList(
        data.get(position).ProductId) == null) {
      holder.product_prices.setVisibility(View.GONE);
    } else {
      holder.product_prices.setVisibility(View.VISIBLE);

      final List<CompanyPoiPrice> list = MemoryCache.getInstance()
          .getCompanyPoiPriceList(data.get(position).ProductId).list;
      String[] text = new String[list.size()];

      for (int x = 0; x < text.length; x++) {
        text[x] = list.get(x).company_name + ": " + list.get(x).price
            + mContext.getString(R.string.euro);
      }

      ArrayAdapter<String> arr = new ArrayAdapter<String>(mContext,
          R.layout.activity_shopping_basket_gallery_item, text);
      holder.product_prices.setAdapter(arr);
    }

    return convertView;
  }

  class ViewHolder {
    TextView product_title;
    ImageView product_details;
    ImageView product_basket;
    Gallery product_prices;
  }

  @Override
  public long getItemId(int position) {
    return Long.parseLong(data.get(position).ProductId);
  }
}




Java Source Code List

com.imellon.android.grocerymate.GroceryMateApp.java
com.imellon.android.grocerymate.GroceryMatePreferences.java
com.imellon.android.grocerymate.activity.HomeActivity.java
com.imellon.android.grocerymate.activity.ProductSearchActivity.java
com.imellon.android.grocerymate.activity.ShoppingBasketActivity.java
com.imellon.android.grocerymate.cache.MemoryCache.java
com.imellon.android.grocerymate.io.DownloadAsyncTaskIface.java
com.imellon.android.grocerymate.io.DownloadAsyncTask.java
com.imellon.android.grocerymate.io.JsonParser.java
com.imellon.android.grocerymate.io.JsonProcessorAsyncTaskInterface.java
com.imellon.android.grocerymate.io.JsonProcessorAsyncTask.java
com.imellon.android.grocerymate.model.AbstractObjectType.java
com.imellon.android.grocerymate.model.CompanyPoiPriceList.java
com.imellon.android.grocerymate.model.CompanyPoiPrice.java
com.imellon.android.grocerymate.model.PoiList.java
com.imellon.android.grocerymate.model.PoiProductPriceList.java
com.imellon.android.grocerymate.model.PoiProductPrice.java
com.imellon.android.grocerymate.model.Poi.java
com.imellon.android.grocerymate.model.ProductList.java
com.imellon.android.grocerymate.model.Product.java
com.imellon.android.grocerymate.model.TerritoryList.java
com.imellon.android.grocerymate.util.BasketAdapter.java
com.imellon.android.grocerymate.util.GorceryMateUriHandler.java
com.imellon.android.grocerymate.util.HttpManager.java
com.imellon.android.grocerymate.util.ResultsAdapter.java
com.imellon.android.grocerymate.util.UserTask.java