1

I am learning to use FastAdapter with Realm. Here is my model and this is how I implement OnClick in a Fragment:

fastAdapter.withOnClickListener(new FastAdapter.OnClickListener<ProductsModel>() {
    @Override
    public boolean onClick(View v, IAdapter<ProductsModel> adapter, ProductsModel item, int position) {
        Toast.makeText(getActivity(), "got it", Toast.LENGTH_SHORT).show();
        return false;
    }
});

But I don't get the Toast appearing. Can anybody please tell me what am I missing?

Update: Here is my model

public class ProductsModel extends RealmObject implements IItem<ProductsModel, ProductsModel.ViewHolder>{
    @PrimaryKey
    private String code;

    private String name, generic, packSize;
    private int quantity, status;


    //variables needed for adapter
    protected boolean isSelected = false; // defines if the item is selected

    @Ignore
    protected Object tag;// defines if this item is isSelectable

    @Ignore
    protected boolean isSelectable = true;


    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPackSize() {
        return packSize;
    }

    public void setPackSize(String packSize) {
        this.packSize = packSize;
    }

    public int getQuantity() {
        return quantity;
    }

    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public String getGeneric() {
        return generic;
    }

    public void setGeneric(String generic) {
        this.generic = generic;
    }

    @Override
    public Object getTag() {
        return tag;
    }

    @Override
    public ProductsModel withTag(Object tag) {
        this.tag = tag;
        return this;
    }

    @Override
    public boolean isEnabled() {
        return false;
    }

    @Override
    public ProductsModel withEnabled(boolean enabled) {
        return null;
    }

    @Override
    public boolean isSelected() {
        return isSelected;
    }

    @Override
    public ProductsModel withSetSelected(boolean selected) {
        return null;
    }

    @Override
    public boolean isSelectable() {
        return isSelectable;
    }

    @Override
    public ProductsModel withSelectable(boolean selectable) {
        this.isSelectable = selectable;
        return this;
    }

    @Override
    public int getType() {
        return R.id.pwdsList;
    }

    @Override
    public int getLayoutRes() {
        return R.layout.item_product;
    }

    @Override
    public View generateView(Context ctx) {
        ViewHolder viewHolder = getViewHolder(LayoutInflater.from(ctx).inflate(getLayoutRes(), null, false));
        bindView(viewHolder, Collections.EMPTY_LIST);
        return viewHolder.itemView;
    }

    @Override
    public View generateView(Context ctx, ViewGroup parent) {
        ViewHolder viewHolder = getViewHolder(LayoutInflater.from(ctx).inflate(getLayoutRes(), parent, false));
        bindView(viewHolder, Collections.EMPTY_LIST);
        return null;
    }


    private ViewHolder getViewHolder(View view) {
        return new ViewHolder(view);
    }

    @Override
    public ViewHolder getViewHolder(ViewGroup parent) {
        return getViewHolder(LayoutInflater.from(parent.getContext()).inflate(getLayoutRes(), parent, false));
    }

    @Override
    public void bindView(ViewHolder holder, List<Object> payloads) {
        holder.name.setText(name + " " + packSize + " (" + quantity + ")");
        holder.generic.setText(generic);
        holder.itemView.setSelected(isSelected());
    }

    @Override
    public void unbindView(ViewHolder holder) {
        holder.name.setText(null);
        holder.generic.setText(null);
    }

    @Override
    public boolean equals(int id) {
        return false;
    }

    @Override
    public ProductsModel withIdentifier(long identifier) {
        return null;
    }

    @Override
    public long getIdentifier() {
        return 0;
    }


    static class ViewHolder extends RecyclerView.ViewHolder{

        ATextView name, generic;

        public ViewHolder(View itemView) {
            super(itemView);

            name = (ATextView) itemView.findViewById(R.id.name);
            generic = (ATextView) itemView.findViewById(R.id.generic);
        }
    }
}
A. K. M. Tariqul Islam
  • 2,824
  • 6
  • 31
  • 48
  • Is the withSelectable attribute for your fastadapter set to true? – Shafayat Mamun Jun 05 '17 at 08:40
  • Yes `fastAdapter.withSelectable(true);` – A. K. M. Tariqul Islam Jun 05 '17 at 08:45
  • You implemented FastAdapter with Realm incorrectly. Model in FastAdapter is an item in recycler adapter, it 's not a model as you think. Wrong is here`ProductsModel extends RealmObject`. You need to get example and follow here https://github.com/mikepenz/FastAdapter/tree/develop/app. – RoShan Shan Jun 05 '17 at 09:23
  • I followed [this example by FastAdapter](https://github.com/mikepenz/FastAdapter/blob/develop/app/src/main/java/com/mikepenz/fastadapter/app/items/RealmSampleUserItem.java) which also doing the same. – A. K. M. Tariqul Islam Jun 06 '17 at 08:59

1 Answers1

0

Your item is not enabled which results in the click event not being forwarded. Just change your code to return isEnabled=true

@Override
public boolean isEnabled() {
    return true;
}
mikepenz
  • 12,708
  • 14
  • 77
  • 117
  • Worked. My `PrimaryKey` is a `String` not a `Long`. Your `getIdentifier()` doesn't support `String` so I converted my code to long by doing `new BigInteger(sb.toString()).longValue()` which is not a good idea. Can I use my primary key as an identifier anyhow? – A. K. M. Tariqul Islam Jun 07 '17 at 03:00
  • 1
    well that's a different question. but something like this would work: private `static long hashString64Bit(CharSequence str) { long result = 0xcbf29ce484222325 L; final int len = str.length(); for (int i = 0; i < len; i++) { result ^= str.charAt(i); result *= 0x100000001b3 L; } return result; }` – mikepenz Jun 07 '17 at 09:13