Back to project page ShoppingList.
The source code is released under:
GNU General Public License
If you think the Android project ShoppingList listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package net.sledzdev.shoppinglist.adapter; //from w w w .ja v a2s. c o m import android.content.Context; import android.view.LayoutInflater; import android.widget.BaseAdapter; import com.google.common.base.Optional; /** * Created by Mariusz on 22.12.13. */ public abstract class DataModelAdapter<T extends ElementWithId> extends BaseAdapter { protected DataModel<T> model; protected Context context; public DataModelAdapter(Context context, DataModel<T> model) { this.context = context; this.model = model; } @Override public int getCount() { return model.size(); } @Override public boolean hasStableIds() { return true; } @Override public Object getItem(int position) { return model.getAtPosition(position).get(); } @Override public long getItemId(int position) { Optional<T> maybeItem = model.getAtPosition(position); if (maybeItem.isPresent()) { return maybeItem.get().getId(); } return -1; } public void deleteItem(long id) { Optional<T> maybeItem = model.getAtId(id); if (maybeItem.isPresent()) { model.removeElement(maybeItem.get()); } notifyDataSetInvalidated(); } public void addItem(T item) { model.addElement(item); notifyDataSetInvalidated(); } protected LayoutInflater getInflater() { return (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); } public DataModel<T> getModel() { return model; } }