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

Help with usage #74

Open
iamchurchill opened this issue Mar 24, 2018 · 0 comments
Open

Help with usage #74

iamchurchill opened this issue Mar 24, 2018 · 0 comments

Comments

@iamchurchill
Copy link

Hi thanks for this wonderful library. please can you help with how i can use it for my recyclerview.

this is my model:
public class Country {
private int id;
private String name, iso2name, iso3name, iso2continent, phone_code, currency, active, added, updated;

public Country() {
}

public Country(int id, String name, String iso2name, String iso3name, String iso2continent, String phone_code, String currency, String active, String added, String updated) {
    this.id = id;
    this.name = name;
    this.iso2name = iso2name;
    this.iso3name = iso3name;
    this.iso2continent = iso2continent;
    this.phone_code = phone_code;
    this.currency = currency;
    this.active = active;
    this.added = added;
    this.updated = updated;
}

public Country(int id, String name, String iso2name, String iso3name, String iso2continent, String phone_code, String currency) {
    this.id = id;
    this.name = name;
    this.iso2name = iso2name;
    this.iso3name = iso3name;
    this.iso2continent = iso2continent;
    this.phone_code = phone_code;
    this.currency = currency;
}

public Country(int id, String name, String iso2name, String iso3name, String iso2continent, String phone_code, String currency, String active) {
    this.id = id;
    this.name = name;
    this.iso2name = iso2name;
    this.iso3name = iso3name;
    this.iso2continent = iso2continent;
    this.phone_code = phone_code;
    this.currency = currency;
    this.active = active;
}

public Country(String name, String iso2name, String iso3name, String iso2continent, String phone_code, String currency, String active, String added, String updated) {
    this.name = name;
    this.iso2name = iso2name;
    this.iso3name = iso3name;
    this.iso2continent = iso2continent;
    this.phone_code = phone_code;
    this.currency = currency;
    this.active = active;
    this.added = added;
    this.updated = updated;
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getName() {
    return name;
}

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

public String getIso2name() {
    return iso2name;
}

public void setIso2name(String iso2name) {
    this.iso2name = iso2name;
}

public String getIso3name() {
    return iso3name;
}

public void setIso3name(String iso3name) {
    this.iso3name = iso3name;
}

public String getIso2continent() {
    return iso2continent;
}

public void setIso2continent(String iso2continent) {
    this.iso2continent = iso2continent;
}

public String getPhone_code() {
    return phone_code;
}

public void setPhone_code(String phone_code) {
    this.phone_code = phone_code;
}

public String getCurrency() {
    return currency;
}

public void setCurrency(String currency) {
    this.currency = currency;
}

public String getActive() {
    return active;
}

public void setActive(String active) {
    this.active = active;
}

public String getAdded() {
    return added;
}

public void setAdded(String added) {
    this.added = added;
}

public String getUpdated() {
    return updated;
}

public void setUpdated(String updated) {
    this.updated = updated;
}

@Override
public String toString() {
    return "Country{" +
            "id=" + id +
            ", name='" + name + '\'' +
            ", iso2name='" + iso2name + '\'' +
            ", iso3name='" + iso3name + '\'' +
            ", iso2continent='" + iso2continent + '\'' +
            ", phone_code='" + phone_code + '\'' +
            ", currency='" + currency + '\'' +
            ", active='" + active + '\'' +
            ", added='" + added + '\'' +
            ", updated='" + updated + '\'' +
            '}';
}

}

and this is my adapter
public class CountryAdapter extends RecyclerView.Adapter<CountryAdapter.ViewHolder> implements Filterable {

public List<Country> countries, countryFiltered;
public Context context;
public AdapterListener adapterListener;
public CountryFilter countryFilter;
private ColorGenerator colorGenerator = ColorGenerator.MATERIAL;

public CountryAdapter(List<Country> countries, Context context, AdapterListener adapterListener) {
    this.context = context;
    this.countries = countries;
    this.countryFiltered = countries;
    this.adapterListener = adapterListener;
}

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
    View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.country_item, viewGroup, false);
    return new ViewHolder(v);
}

@Override
public void onBindViewHolder(ViewHolder viewHolder, int position) {
    TextDrawable drawable = TextDrawable.builder()
            .beginConfig()
            .textColor(Color.BLACK)
            .useFont(Typeface.DEFAULT)
            .fontSize(80)
            .bold()
            .toUpperCase()
            .endConfig()
            .buildRound(countries.get(position).getName().substring(0, 1), colorGenerator.getColor(countries.get(position).getName()));
    viewHolder.getImageView().setImageDrawable(drawable);
    viewHolder.getTvName().setText(countries.get(position).getName());
    viewHolder.getTvCode().setText(countries.get(position).getPhone_code());
}

@Override
public int getItemCount() {
    return countries.size();
}

@Override
public Filter getFilter() {
    if (countryFilter == null) {
        countryFilter = new CountryFilter(countryFiltered, this);
    }
    return countryFilter;
}

public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    AppCompatImageView imageView;
    AppCompatTextView tvName, tvCode;

    ViewHolder(View itemView) {
        super(itemView);
        tvName = itemView.findViewById(R.id.tvName);
        tvCode = itemView.findViewById(R.id.tvCode);
        imageView = itemView.findViewById(R.id.imageView);
        itemView.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        adapterListener.onSelected(countries.get(getAdapterPosition()));
    }

    private AppCompatImageView getImageView() {
        return imageView;
    }

    private AppCompatTextView getTvName() {
        return tvName;
    }

    private AppCompatTextView getTvCode() {
        return tvCode;
    }
}

}

Thanks again

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

1 participant