Can someone give a simple snippet code with a custom adapter, document seems to be so complex with little to no explanation
Opened this issue · 1 comments
gregcabrera422 commented
Can someone give a simple snippet code with a custom adapter, document seems to be so complex with little to no explanation
mudassirzulfiqar commented
Model Class
`public class ImageModel implements AsymmetricItem {
private int columnSpan;
private int rowSpan;
private int position;
private String img;
public ImageModel(String img) {
this(1, 1, 0);
this.img = img;
}
public ImageModel(int columnSpan, int rowSpan, int position) {
this.columnSpan = columnSpan;
this.rowSpan = rowSpan;
this.position = position;
}
public ImageModel(Parcel in) {
readFromParcel(in);
}
@Override
public int getColumnSpan() {
return columnSpan;
}
@Override
public int getRowSpan() {
return rowSpan;
}
public int getPosition() {
return position;
}
@Override
public String toString() {
return String.format("%s: %sx%s", position, rowSpan, columnSpan);
}
@Override
public int describeContents() {
return 0;
}
private void readFromParcel(Parcel in) {
columnSpan = in.readInt();
rowSpan = in.readInt();
position = in.readInt();
}
@Override
public void writeToParcel(@NonNull Parcel dest, int flags) {
dest.writeInt(columnSpan);
dest.writeInt(rowSpan);
dest.writeInt(position);
}
/* Parcelable interface implementation */
public final Parcelable.Creator<ImageModel> CREATOR = new Parcelable.Creator<ImageModel>() {
@Override
public ImageModel createFromParcel(@NonNull Parcel in) {
return new ImageModel(in);
}
@Override
@NonNull
public ImageModel[] newArray(int size) {
return new ImageModel[size];
}
};
}`
My Adapter class
`public class DefaultListAdapter extends BaseAdapter {
private List<ImageModel> items;
public DefaultListAdapter(Context context, List<ImageModel> items) {
this.items = items;
}
@Override
public int getCount() {
return items.size();
}
@Override
public Object getItem(int position) {
return items.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View view, @NonNull ViewGroup parent) {
if (view == null) {
LayoutInflater layoutInflater = LayoutInflater.from(getContext());
view = layoutInflater.inflate(R.layout.gallery_view_event_creation, null);
}
ImageView mImage = (ImageView) view.findViewById(R.id.img);
ImageUtil.LoadImage(getContext(), items.get(position).img, mImage);
return view;
}
public void appendItem(ImageModel newItems) {
this.items.add(newItems);
notifyDataSetChanged();
}
}
`