Android Custom ArrayAdapter does not update after filter

So, I have a custom one ArrayAdapter, so I can use the Title / Subtitle view available in ListView. I have EditTextone that takes a string and filters the adapter.

A filter works in the sense that it filters the correct objects (I can tell by clicking on it and it starts the intent with the correct "extra").

However, despite the fact that filtering works, the elements in the adapter are not updated to display the correct information ... The name and subtitles are incorrect.

Suppose we have items from 0 to 9 on ListView, I filter by 3 elements with a search, and lets say that the filtered elements are displayed 5, 6, 9 ... 3, but the first 3 elements of the initial preliminary search ListView(0-2 ) If I click on element 2 (third element), the contents of 9 will be contained in the new intent. This is true for the search criteria, however the name does reflect the correct information.

I am not sure what I need to report ListViewfor the update. I don't think himnotifyDataSetChanged();

Any help is appreciated. Thank!

    public class myListAdapter extends ArrayAdapter<Pizza>{
    private ArrayList<Pizza> items;
    private PizzaViewHolder myListHolder;

    private class PizzaViewHolder{
        TextView title;
        TextView subtitle;
    }

    public myListAdapter(Context context, int textViewResourceId, ArrayList<Pizza> items) {
        super(context, textViewResourceId, items);
        this.items = items;
        // TODO Auto-generated constructor stub
    }

    @Override
    public View getView(int pos, View convertView, ViewGroup parent){
        View v = convertView;
        if(v == null){
            LayoutInflater vi = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.myList_item, null);
            myListHolder = new PizzaViewHolder();
            myListHolder.title = (TextView)v.findViewById(R.id.title);
            myListHolder.subtitle = (TextView)v.findViewById(R.id.subtitle);
            v.setTag(myListHolder);
        }else myListHolder = (PizzaViewHolder)v.getTag();

        Pizza myList = items.get(pos);

        if (myList != null){
            myListHolder.title.setText(myList.getTitle());
            myListHolder.subtitle.setText(myList.getSubTitle());
        }       
        return v;           
    }
}

This is a search

    private TextWatcher filterTextWatcher = new TextWatcher(){

    public void afterTextChanged(Editable s) {
        // TODO Auto-generated method stub
        if(!s.equals("")){  
            ((Filterable) this.listView1.getAdapter()).getFilter().filter(s);
            }
    }

    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {
        // TODO Auto-generated method stub

    }

    public void onTextChanged(CharSequence s, int start, int before,
            int count) {
        // TODO Auto-generated method stub

    }

};
+5
source share
1 answer

, , , ArrayAdapter. ArrayAdapter , :

import java.util.ArrayList;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Filter;
import android.widget.TextView;

public class PizzaAdapter extends ArrayAdapter<Pizza>{

private ArrayList<Pizza> original;
private ArrayList<Pizza> fitems;
private Filter filter;


public PizzaAdapter(Context context, int textViewResourceId, ArrayList<Pizza> items) {
    super(context, textViewResourceId, items);

    this.original = new ArrayList<Pizza>(items);
    this.fitems = new ArrayList<Pizza>(items);
    this.filter = new PizzaFilter();
}

@Override
public View getView(int position, View convertView, ViewGroup parent){
    View v = convertView;

    if(v == null){
        LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = vi.inflate(R.layout.pizza_list_item, null);
    }

    Pizza pizza = fitems.get(position);

    if(pizza != null){
        String subtitleString = new String("[" + pizza.getPizzaType() + "]   " + pizza.getPizzaCategory() + ": " + pizza.getPizzaCode());

        TextView title = (TextView)v.findViewById(R.id.title);
        TextView subtitle = (TextView)v.findViewById(R.id.subtitle);

        if(title != null){
            title.setText(pizza.getPizzaName());
        }
        if(subtitle != null){
            subtitle.setText(subtitleString);
        }
    }
    return v;
}

@Override
public Filter getFilter(){

    if(filter == null){
        filter = new PizzaFilter();
    }
    return filter;
}

private class PizzaFilter extends Filter{
    @Override
    protected FilterResults performFiltering(CharSequence constraint){
        FilterResults results = new FilterResults();
        String prefix = constraint.toString().toLowerCase();

        if (prefix == null || prefix.length() == 0){
            ArrayList<Pizza> list = new ArrayList<Pizza>(original);
            results.values = list;
            results.count = list.size();
        }else{
            final ArrayList<Pizza> list = new ArrayList<Pizza>(original);
            final ArrayList<Pizza> nlist = new ArrayList<Pizza>();
            int count = list.size();

            for (int i = 0; i<count; i++){
                final Pizza pizza = list.get(i);
                final String value = Pizza.getPizzaName().toLowerCase();

                if(value.contains(prefix)){
                    nlist.add(pizza);
                }
                results.values = nlist;
                results.count = nlist.size();
            }
        }
        return results;
    }

    @SuppressWarnings("unchecked")
    @Override
    protected void publishResults(CharSequence constraint, FilterResults results) {
        fitems = (ArrayList<Pizza>)results.values;
        notifyDataSetChanged();
        clear();
        int count = fitems.size();
        for(int i = 0; i<count; i++){
            add(fitems.get(i));
            notifyDataSetInvalidated();
        }
    }
}
}

, . , .

+13

All Articles