Back to project page Grocery-Mate.
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.
/* * Copyright (C) 2011 iMellon//from w w w . j a v a2 s . c om * * 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.model.Product; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; /** * @author Christos Papazafeiropoulos, Dimitris Makris */ public class ResultsAdapter extends BaseAdapter { private LayoutInflater mInflater; private List<Product> data; private Context mContext; public ResultsAdapter(Context context, List<Product> mList) { mContext = context; mInflater = LayoutInflater.from(context); data = mList; } public ResultsAdapter(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_product_search_list_item, null); holder = new ViewHolder(); holder.product_title = (TextView) convertView .findViewById(R.id.product_title); holder.product_basket = (ImageView) convertView .findViewById(R.id.product_basket); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } holder.product_title.setText(data.get(position).Name); if (MemoryCache.getInstance().getProduct(data.get(position).ProductId) != null) { holder.product_basket.setImageResource(android.R.drawable.ic_menu_delete); } else { holder.product_basket.setImageResource(android.R.drawable.ic_menu_add); } holder.product_basket.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (MemoryCache.getInstance().getProduct(data.get(position).ProductId) != null) { MemoryCache.getInstance().removeProduct( data.get(position).ProductId); Toast.makeText(mContext, R.string.success_remove, Toast.LENGTH_SHORT) .show(); ((ImageView) v).setImageResource(android.R.drawable.ic_menu_add); } else { MemoryCache.getInstance().putProduct( data.get(position).ProductId, data.get(position)); Toast.makeText(mContext, R.string.success_add, Toast.LENGTH_SHORT) .show(); ((ImageView) v).setImageResource(android.R.drawable.ic_menu_delete); } } }); return convertView; } class ViewHolder { TextView product_title; ImageView product_basket; } @Override public long getItemId(int position) { return Long.parseLong(data.get(position).ProductId); } }