Back to project page AndroidShoppingList.
The source code is released under:
Apache License
If you think the Android project AndroidShoppingList 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 2012 C. A. Fitzgerald// www .j a v a2 s. co m * * Licensed 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.github.riotopsys.shoppinglist.adapter; import java.util.Collections; import java.util.Comparator; import java.util.List; import com.github.riotopsys.shoppinglist.R; import com.github.riotopsys.shoppinglist.comparator.ListItemNameComparator; import com.github.riotopsys.shoppinglist.listener.CheckedChangeListener; import com.github.riotopsys.shoppinglist.model.ShoppingList; import com.github.riotopsys.shoppinglist.model.ShoppingListItem; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.CheckBox; import android.widget.TextView; public class ShoppingListAdapter extends BaseAdapter { private static final String TAG = ShoppingListAdapter.class.getSimpleName(); private ShoppingList mShoppingList; private List<ShoppingListItem> mItems; private Comparator<ShoppingListItem> mComparator = new ListItemNameComparator(); public ShoppingListAdapter(Context ctx, ShoppingList shoppingList) { mShoppingList = shoppingList; mItems = shoppingList.getItems(ctx); Collections.sort(mItems, mComparator); } @Override public int getCount() { return mItems.size(); } @Override public Object getItem(int position) { return mItems.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { if (convertView == null) { Context context = parent.getContext(); LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); convertView = inflater.inflate(R.layout.shopping_list_item, null); } ShoppingListItem item = mItems.get(position); ((TextView) convertView.findViewById(R.id.item_name)).setText(item.getName()); CheckBox cb = ((CheckBox) convertView.findViewById(R.id.resolved)); cb.setChecked(item.getResolved()); cb.setOnCheckedChangeListener(new CheckedChangeListener(item)); ((TextView) convertView.findViewById(R.id.item_quantity)).setText(Integer.toString(item.getQuantity())); return convertView; } @Override public void notifyDataSetChanged() { throw new RuntimeException(TAG + " needs a context!"); } public void notifyDataSetChanged(Context ctx) { mItems = mShoppingList.getItems(ctx); Collections.sort(mItems, mComparator); super.notifyDataSetChanged(); } public ShoppingListItem getListItem(int currentItem) { return mItems.get(currentItem); } }