Skip to content

Instantly share code, notes, and snippets.

@abombss
Forked from thevery/RoboBaseAdapterButton.java
Created January 30, 2011 02:59
Show Gist options
  • Save abombss/802475 to your computer and use it in GitHub Desktop.
Save abombss/802475 to your computer and use it in GitHub Desktop.
package roboguice.astroboy;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import com.google.inject.internal.Nullable;
import roboguice.inject.InjectView;
import java.lang.reflect.Field;
import java.util.ArrayList;
/**
* @author Ildar Karimov
*/
public abstract class RoboBaseAdapter<THolder> extends BaseAdapter {
protected ArrayList<ViewMembersInjector> viewsForInjection = new ArrayList<ViewMembersInjector>();
private Context context;
private int layoutId;
private LayoutInflater inflater;
private void init() {
inflater = LayoutInflater.from(context);
prepareFields(holderType);
}
public RoboBaseAdapter(Context context, int layoutId, Class<? extends Object> holderType) {
this.context = context;
this.layoutId = layoutId;
init(holderType);
}
protected abstract THolder createNewViewHolder(int position, View convertView, ViewGroup parent);
protected abstract void bindView(int position, View convertView, ViewGroup parent, THolder holder);
protected View createNewView(int position, View convertView, ViewGroup parent) {
return inflater.inflate(layoutId, parent, false);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
THolder holder;
if (convertView == null) {
convertView = createNewView(position, convertView, parent);
holder = createNewViewHolder(position, convertView, parent);
convertView.setTag(holder);
injectViews(convertView, holder);
} else {
holder = convertView.getTag();
}
bindView(position, convertView, parent, holder);
return convertView;
}
public void injectViews(View view) {
for (ViewMembersInjector viewMembersInjector : viewsForInjection) {
viewMembersInjector.reallyInjectMembers(this, view);
}
}
private void prepareFields(Class clazz) {
for (Field field : clazz.getDeclaredFields()) {
if (field.isAnnotationPresent(InjectView.class)) {
field.setAccessible(true);
viewsForInjection.add(new ViewMembersInjector(field, field.getAnnotation(InjectView.class)));
}
}
}
class ViewMembersInjector {
protected Field field;
protected InjectView annotation;
public ViewMembersInjector(Field field, InjectView annotation) {
this.field = field;
this.annotation = annotation;
}
public void reallyInjectMembers(Object holder, View view) {
Object value = null;
try {
value = view.findViewById(annotation.value());
if (value == null && field.getAnnotation(Nullable.class) == null) {
throw new NullPointerException(String.format("Can't inject null value into %s.%s when field is not @Nullable", field.getDeclaringClass(), field
.getName()));
}
field.set(holder, value);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
} catch (IllegalArgumentException f) {
throw new IllegalArgumentException(String.format("Can't assign %s value %s to %s field %s", value != null ? value.getClass() : "(null)", value,
field.getType(), field.getName()));
}
}
}
}
package roboguice.astroboy;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import roboguice.inject.InjectView;
import java.util.List;
/**
* @author Ildar Karimov
* @author Adam Tybor
*/
public class RoboBaseAdapterButton extends RoboBaseAdapter<ButtonHolder> {
static class ButtonHolder {
@InjectView(R.id.buton)
Button button;
}
private List<String> items;
public RoboBaseAdapterButton(Context context, int layoutId, List<String> items, ButtonHolder.class) {
super(context, layoutId);
this.items = items;
}
@Override
protected ButtonHolder createNewViewHolder(int position, View convertView, ViewGroup parent) {
return new ButtonHolder();
}
@Override
protected void bindView(int position, View convertView, ViewGroup parent, ButtonHolder holder) {
holder.button.setText(getItem(position));
}
public int getCount() {
return items.size();
}
public String getItem(int position) {
return items.get(position);
}
public long getItemId(int i) {
return i;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment