Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] Android Java Publish result cast issue at MyCustomAdapter.java

Posted on November 11, 2022 By

Solution 1 :

I have checked you code .. problem is you are assigning wrong value to filterResults also at wrong place (inside loop). Your code should look like below .

@Override
protected FilterResults performFiltering(CharSequence constraint) {
    FilterResults filterResults = new FilterResults();
    if (constraint == null || constraint.length() == 0) {
        filterResults.count = countryModelList.size();
        filterResults.values = countryModelList;
    } else {

        List<CountryModel> resultModel = new ArrayList<>();
        String searchString = constraint.toString().toLowerCase();

        for (CountryModel itemModel:countryModelList) {
            if (itemModel.getCountry().toLowerCase().contains(searchString)) {
                resultModel.add(itemModel);
            }
        }
        filterResults.count = resultModel.size();
        filterResults.values = resultModel;
    }
    return filterResults;
}

Problem :

on JDK 1.8
tried casting many ways but none working for me.

public class CountryAdapter extends ArrayAdapter<CountryModel> {

private Context context;
private List<CountryModel> countryModelList;
private List<CountryModel> countryModelListFiltered;

public CountryAdapter(Context context, List<CountryModel> countryModelList) {
    super(context, R.layout.custom_list_item, countryModelList);
    this.context = context;
    this.countryModelList = countryModelList;
    this.countryModelListFiltered = countryModelList;
}

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

    //Instantiates a layout XML file into its corresponding View objects.
    @SuppressLint("ViewHolder")
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.custom_list_item, null, true);
    TextView tvCountryName = view.findViewById(R.id.tvCountryName);
    ImageView countryFlag = view.findViewById(R.id.imageFlag);

    tvCountryName.setText(countryModelListFiltered.get(position).getCountry());
    //using glide library to set image to image View.
    Glide.with(context).load(countryModelListFiltered.get(position).getFlag()).into(countryFlag);
    return view;

}

@Override
public int getCount() {
    return countryModelListFiltered.size();
}

@Nullable
@Override
public CountryModel getItem(int position) {
    return countryModelListFiltered.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

//Filter Logic


@NonNull
@Override
public Filter getFilter() {
    Filter filter = new Filter() {

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            FilterResults filterResults = new FilterResults();
            if (constraint == null || constraint.length() == 0) {
                filterResults.count = countryModelList.size();
                filterResults.values = countryModelList;
            } else {

                List<CountryModel> resultModel = new ArrayList<>();
                String searchString = constraint.toString().toLowerCase();

                for (CountryModel itemModel:countryModelList) {
                    if (itemModel.getCountry().toLowerCase().contains(searchString)) {
                        resultModel.add(itemModel);
                    }
                    filterResults.count = resultModel.size();
                    filterResults.values = itemModel;
                }
            }
            return filterResults;
        }
        @SuppressWarnings("unchecked")
        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            Log.d("Cast:", "publishResults: " + results.values.toString());



            countryModelListFiltered = ((List<CountryModel>) results.values);
            AffectedCountries.countryModelList = (List<CountryModel>) results.values;
            notifyDataSetChanged();
        }
    };
    return filter;
}
}

Comments

Comment posted by ADM

Add the whole code of adapter ..

Comment posted by github.com/techieasif/Covid19-Java/blob/master/Covid19Tracker/…

@ADM Stack overflow showing too much code warning , i have github repository please take a look at:

READ  [FIXED] android - keyListener does not recognise input
Powered by Inline Related Posts

Android Tags:android, casting, java

Post navigation

Previous Post: [FIXED] android – How to achieve ionic permissions on first load
Next Post: [FIXED] android – How do i put online database into local database

Related Posts

[FIXED] java – How to compare a arraylist with nested arraylist in kotlin? Android
[FIXED] android – onReceive method doesn’t get called on the first launch of application Android
[FIXED] java – How do I remove the default menu button that appears on the left of my toolbar? Android
[FIXED] How to set buildConfigField for module from outside for android? Android
[FIXED] android studio – Why get request http dont working in Kotlin? Android
[FIXED] java – I’m unable to save camera captured image no matter what I do. How do I do it? Android

Archives

  • April 2023
  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • Can Vicks humidifier be used without filter?
  • What color is Spanish green?
  • How old is Jamie in The War That Saved My Life?
  • When should I use scalp massager for hair growth?
  • Can I put polyurethane over liming wax?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme