androidandroid-layoutlistviewandroid-listviewandroid-filter

Linear layout with dynamic textviews in listview error while filtering


i want to add dynamic text views to a horizontal linear layout of a listview, i have implemented it successfully.

I have listview search functionality, when ever i am filtering all the dynamic textviews that i have created are appending to the existing linearlayout's of listview

below is my listview row xml:

<LinearLayout
               <!-- All the other widgets -->
                <LinearLayout
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/business_header"
                    android:id="@+id/predicate_layout"
                    android:orientation="horizontal"/>

</LinearLayout

this is my adapter code associated with it:

public class MarketPlaceAdapter extends BaseAdapter implements Filterable {

    private Context context;
    private List<Result> businesses;
    private List<Result> orig;

    //obtain the context and list of business data
    public MarketPlaceAdapter(Context context)
    {
        this.context = context;
        businesses = new ArrayList<>();
    }

    public void updateMarketPlaceList(List<Result> newList) {
        businesses.addAll(newList);
        this.notifyDataSetChanged();
    }

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

    @Override
    public Result getItem(int position) {
        return businesses.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        BusinessesViewHolder businessesViewHolder;

        //Implementing view holder pattern for smooth listing of businesses in list
        //initialize views once and store them in class
        if (convertView == null) {
            LayoutInflater mInflater = (LayoutInflater)
                    context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            convertView = mInflater.inflate(R.layout.list_row_marketplace, null);
            businessesViewHolder = new BusinessesViewHolder();
            businessesViewHolder.businessImage = (ImageView) convertView.findViewById(R.id.business_image);
            businessesViewHolder.businessTitle = (TextView) convertView.findViewById(R.id.business_title);
            businessesViewHolder.businessCategory = (TextView) convertView.findViewById(R.id.business_category);
            businessesViewHolder.linearLayout = (LinearLayout) convertView.findViewById(R.id.predicate_layout);

            convertView.setTag(businessesViewHolder);
        }

        //reuse views from view holder if already initialised
        else {
            businessesViewHolder = (BusinessesViewHolder) convertView.getTag();
        }

            Result result = businesses.get(position);

        //obtain data object from Array list passed and set the corresponding values to widgets
        //to create list row
        if(result != null)
        {
            String businessName = result.getProperties().getName();

            if(result.getProperties().getThumbnail()!=null){
                Picasso.with(context).load(result.getProperties().getThumbnail()).
                        placeholder(R.drawable.intro_business).into(businessesViewHolder.businessImage);
            }
            else{
                businessesViewHolder.businessImage.setImageDrawable(MediaUtils.getTextIcon(businessName));
            }

            businessesViewHolder.businessTitle.setText(businessName);
            businessesViewHolder.businessCategory.setText(result.getProperties().getDomain());

            List<String> partnerTypes = result.getProperties().getPartner_types();
            if(!partnerTypes.isEmpty()) partnerTypes.add(0,"Need:");

            boolean isFirstPosition = false;
            for(String partnerType: partnerTypes){

                TextView partnerTypeTextView = new TextView(context);
                partnerTypeTextView.setText(partnerType);
                float pixels = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 6, context.getResources().getDisplayMetrics());
                partnerTypeTextView.setTextSize(pixels);
                if(isFirstPosition){


                    partnerTypeTextView.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.rounded_text_view));
                }
                isFirstPosition = true;
                partnerTypeTextView.setSingleLine(true);
                businessesViewHolder.linearLayout.addView(partnerTypeTextView);
            }
        }

        return convertView;

    }

    //filters the adapter with the business names based on the search query given in search widget
    //at Market place fragment action bar
    @Override
    public Filter getFilter() {
        return new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                final FilterResults filterResults = new FilterResults();
                final List<Result> intermediateResults = new ArrayList<Result>();
                if(orig == null)
                    orig = businesses;
                if(constraint != null)
                {
                    if(orig!=null && orig.size() > 0){
                        for (final Result g : orig) {
                            if (g.getProperties().getName().toLowerCase(Locale.ENGLISH).contains(constraint.toString()))
                                intermediateResults.add(g);
                        }
                    }
                    filterResults.values = intermediateResults;
                }
                return filterResults;
            }

            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {
                businesses = (ArrayList<Result>) results.values;
                notifyDataSetChanged();
            }
        };
    }

    class BusinessesViewHolder {
        ImageView businessImage;
        TextView businessTitle;
        TextView businessCategory;
        LinearLayout linearLayout;
    }

}

My problem is i am getting the other list rows dynamic textviews in the filtered rows.

Please help me to solve this problem.


Solution

  • Because you are using the ViewHolder pattern, all of the dynamically added Views are being recycled when you call:

    businessesViewHolder = (BusinessesViewHolder) convertView.getTag();
    

    I would follow that line with:

    businessesViewHolder.linearLayout.removeAllViews();
    

    That should clear the dynamically added TextViews from the LinearLayout in each ListView item.