Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can someone give a simple snippet code with a custom adapter, document seems to be so complex with little to no explanation #79

Open
gregcabrera422 opened this issue Jul 30, 2018 · 1 comment

Comments

@gregcabrera422
Copy link

No description provided.

@mudassirzulfiqar
Copy link

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();

    }


}

`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants