If you think the Android project hubblog 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 2012 Kevin Sawicki <kevinsawicki@gmail.com>
*/*fromwww.java2s.com*/
* 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.kevinsawicki.wishlist;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
/**
* Adapter for lists where only a single view type is used
*
* @param <V>
*/publicabstractclass SingleTypeAdapter<V> extends TypeAdapter {
privatestaticfinal Object[] EMPTY = new Object[0];
privatefinal LayoutInflater inflater;
privatefinalint layout;
privatefinalint[] children;
private Object[] items;
/**
* Create adapter
*
* @param activity
* @param layoutResourceId
*/public SingleTypeAdapter(final Activity activity, finalint layoutResourceId) {
this(activity.getLayoutInflater(), layoutResourceId);
}
/**
* Create adapter
*
* @param context
* @param layoutResourceId
*/public SingleTypeAdapter(final Context context, finalint layoutResourceId) {
this(LayoutInflater.from(context), layoutResourceId);
}
/**
* Create adapter
*
* @param inflater
* @param layoutResourceId
*/public SingleTypeAdapter(final LayoutInflater inflater,
finalint layoutResourceId) {
this.inflater = inflater;
this.layout = layoutResourceId;
items = EMPTY;
int[] childIds = getChildViewIds();
if (childIds == null)
childIds = newint[0];
children = childIds;
}
/**
* Get a list of all items
*
* @return list of all items
*/
@SuppressWarnings("unchecked")
protected List<V> getItems() {
List<? extends Object> objList = Arrays.asList(items);
return (List<V>) objList;
}
/**
* Set items to display
*
* @param items
*/publicvoid setItems(final Collection<?> items) {
if (items != null && !items.isEmpty())
setItems(items.toArray());
else
setItems(EMPTY);
}
/**
* Set items to display
*
* @param items
*/publicvoid setItems(final Object[] items) {
if (items != null)
this.items = items;
else
this.items = EMPTY;
notifyDataSetChanged();
}
@Override
publicint getCount() {
return items.length;
}
@SuppressWarnings("unchecked")
public V getItem(finalint position) {
return (V) items[position];
}
@Override
publiclong getItemId(finalint position) {
return items[position].hashCode();
}
/**
* Get child view ids to store
* <p>
* The index of each id in the returned array should be used when using the
* helpers to update a specific child view
*
* @return ids
*/protectedabstractint[] getChildViewIds();
/**
* Initialize view
*
* @param view
* @return view
*/protected View initialize(final View view) {
return super.initialize(view, children);
}
/**
* Update view for item
*
* @param position
* @param view
* @param item
*/protectedvoid update(int position, View view, V item) {
setCurrentView(view);
update(position, item);
}
/**
* Update item
*
* @param position
* @param item
*/protectedabstractvoid update(int position, V item);
@Override
public View getView(finalint position, View convertView,
final ViewGroup parent) {
if (convertView == null)
convertView = initialize(inflater.inflate(layout, null));
update(position, convertView, getItem(position));
return convertView;
}
}