“MultiautoComplete -android” Kod odpowiedzi

MultiautoComplete -android

import androidx.annotation.NonNull;
import ridmik.one.entities.MucOptions;
import ridmik.one.ui.components.autocomplete.filter.HandleTokenFilter;

public class MucUserTokenFilter extends HandleTokenFilter<MucOptions.User> {

  public MucUserTokenFilter() {
    this('@');
  }
  public MucUserTokenFilter(char handleChar) {
    super(handleChar);
  }

  @Override
  protected boolean matchesConstraint(@NonNull MucOptions.User user, @NonNull CharSequence constraint) {
    return user.getComparableName().toLowerCase().contains(constraint.toString().toLowerCase());
  }

  @Override
  public @NonNull CharSequence toTokenString(@NonNull MucOptions.User user) {
    return handleChar +user.getComparableName(); // Add handler at the begining! this is the trick!
  }
}
Horrible Hamerkop

MultiautoComplete -android

private MultiAutoComplete multiAutoComplete = null; // A field in your Activity / Fragment class, initialize it to null.

void initAutoSuggestions() {
  
ArrayList<MucOptions.User> someList = getUsers(); // <--- suggestions dataset depending on your code.
        AutoCompleteTypeAdapter<MucOptions.User> nameTypeAdapter =
            AutoCompleteTypeAdapter.Build.from(new MucUserViewBinder(), new MucUserTokenFilter());


        nameTypeAdapter.setItems(someList);

        this.multiAutoComplete = new MultiAutoComplete.Builder()
            .tokenizer(new PrefixTokenizer('@'))
            .addTypeAdapter(nameTypeAdapter)
            // .delayer(constraint -> { return 10; }) // eta ki bujhi ni
            .build();
        
        this.multiAutoComplete.onViewAttached(binding.textinput); // binding.textinput is your multiAutoCompleteView 
}
Horrible Hamerkop

MultiautoComplete -android

public class MucUserViewBinder implements AutoCompleteViewBinder<MucOptions.User> {
  public static final String TAG = MucUserViewBinder.class.getSimpleName();
  @Override
  public long getItemId(@NonNull @NotNull MucOptions.User user) {
    try{
      if(user!=null) {
        return user.userId;  // ei line e crash kore :/
      }
    }catch (Exception x) {
      Timber.tag(TAG).e("Exception "+x.getMessage());
    }


    return 0;
  }

  @Override
  public int getItemLayoutId() {
    return R.layout.simple_autocomplete_row;
  }

  @NonNull
  @NotNull
  @Override
  public AutoCompleteViewHolder getViewHolder(@NonNull @NotNull View view) {
    return new MucUserViewHolder(view);
  }

  @Override
  public void bindData(@NonNull @NotNull AutoCompleteViewHolder viewHolder, @NonNull @NotNull MucOptions.User user,
                       @Nullable @org.jetbrains.annotations.Nullable CharSequence constraint) {
    MucUserViewHolder itemViewHolder = (MucUserViewHolder) viewHolder;
    itemViewHolder.textView.setText(user.getComparableName());
    String smallAvatar = user.image + "_sm.png";
    Timber.tag(TAG).e("user.image = "+smallAvatar);
    Picasso.get().load(smallAvatar)
        .placeholder(ContextCompat.getDrawable(itemViewHolder.rootView.getContext(),
            R.drawable.ic_avatar_placeholder)).into(itemViewHolder.avatar);
  }

  static class MucUserViewHolder extends AutoCompleteViewHolder {
    public final TextView textView;
    public final CircleImageView avatar;
    public final View rootView;

    protected MucUserViewHolder(@NonNull @NotNull View view) {
      super(view);
      this.rootView = view;
      this.textView = view.findViewById(R.id.textView);
      this.avatar = view.findViewById(R.id.avatar);
    }
  }
}
Horrible Hamerkop

MultiautoComplete -android

@Override
    public void onDestroy() {
        super.onDestroy();
          if(multiAutoComplete!=null) {
            this.multiAutoComplete.onViewDetached(); // needed for @mention that thing
        }
}
Horrible Hamerkop

Odpowiedzi podobne do “MultiautoComplete -android”

Przeglądaj popularne odpowiedzi na kod według języka

Przeglądaj inne języki kodu