android:filter在costum gridview适配器中不起作用

时间:2016-12-19 21:47:34

标签: android gridview filter

我正在尝试创建一个简单的应用程序愿望,我有一个gridview和edittext,当我输入edittext时,它过滤gridview输入代码

class CostumAdapter  extends ArrayAdapter<Personne> implements Filterable{
    Costumfilter filter;
    ArrayList <Personne> filterlist;
    ArrayList <Personne> items;
    public CostumAdapter(Context context, ArrayList <Personne> items) {
        super(context,R.layout.costum_row,items);
        this.filterlist=items;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = LayoutInflater.from(getContext());
        View costumview = inflater.inflate(R.layout.costum_row, parent, false);
        Personne imed=getItem(position);
        TextView mytext = (TextView) costumview.findViewById(R.id.mytext1);
        mytext.setText(imed.firstname);
        mytext.setBackgroundColor(getRandomColor());
        return  costumview;
    }

    @Override
    public Filter getFilter() {
        if(filter == null){
            filter=new Costumfilter();
        }
        return filter;
    }

    public int getRandomColor(){
        Random rnd = new Random();
        return Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
    }

    class Costumfilter extends Filter{

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            FilterResults results = new FilterResults();
            if(constraint != null && constraint.length()!=0){
                constraint = constraint.toString().toLowerCase();
                ArrayList<Personne> filters =new ArrayList<Personne>();
                for (int i=0; i <filterlist.size();i++){
                    //probleme peut etre ici
                    if (filterlist.get(i).firstname.toLowerCase().contains(constraint.toString().toLowerCase())){
                        Personne p =new Personne(filterlist.get(i).firstname,filterlist.get(i).lastname,filterlist.get(i).number,filterlist.get(i).mail);
                        filters.add(p);
                    }
                    if (filterlist.get(i).firstname.toLowerCase().startsWith(constraint.toString().toLowerCase())){
                        Personne p =new Personne(filterlist.get(i).firstname,filterlist.get(i).lastname,filterlist.get(i).number,filterlist.get(i).mail);
                        filters.add(p);
                    }
                }
                results.count=filters.size();
                results.values=filters;
            }else{
                results.count=filterlist.size();
                results.values=filterlist;
            }
            return results;
        }

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

这是我的主要课程

public class MainActivity extends AppCompatActivity {

ArrayList<Personne> items = new ArrayList<Personne>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ActionBar actionBar= getSupportActionBar();
    actionBar.hide();

    EditText textfilter =(EditText) findViewById(R.id.textfilter);

    items.add(new Personne("kabouya","imed","0553164597","haha95@live.fr"));
    items.add(new Personne("Tazir","oussama","0654613257","oussamaMail@live.fr"));
    items.add(new Personne("kabouya","imed","0553164597","haha95@live.fr"));
    items.add(new Personne("Tazir","oussama","0654613257","oussamaMail@live.fr"));
    items.add(new Personne("kabouya","imed","0553164597","haha95@live.fr"));
    items.add(new Personne("Tazir","oussama","0654613257","oussamaMail@live.fr"));
    items.add(new Personne("kabouya","imed","0553164597","haha95@live.fr"));
    items.add(new Personne("Tazir","oussama","0654613257","oussamaMail@live.fr"));

    final CostumAdapter adapter = new CostumAdapter(MainActivity.this, items);

    GridView mygrid=(GridView)findViewById(R.id.mygrid);
    mygrid.setAdapter(adapter);
    mygrid.setTextFilterEnabled(true);
    textfilter.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence ss, int i, int i1, int i2) {
            adapter.getFilter().filter(ss.toString());
        }

        @Override
        public void afterTextChanged(Editable editable) {

        }
    });

    mygrid.setOnItemClickListener(
            new AdapterView.OnItemClickListener(){

                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                    String  someone=String.valueOf(adapterView.getItemAtPosition(i).toString());
                    AlertDialog.Builder mBuilder = new AlertDialog.Builder(MainActivity.this);
                    View mView = getLayoutInflater().inflate(R.layout.custom_dialog, null);
                    TextView textnum =(TextView) mView.findViewById(R.id.textnum);
                    TextView textmail =(TextView) mView.findViewById(R.id.textmail);
                    TextView maintext =(TextView) mView.findViewById(R.id.maintext);
                    maintext.setText(items.get(i).firstname.toString()+" "+items.get(i).lastname.toString());
                    textnum.setText(items.get(i).number.toString());
                    textmail.setText(items.get(i).mail.toString());
                    mBuilder.setView(mView);
                    AlertDialog dialog = mBuilder.create();
                    dialog.show();
                }
            }
    );
}

这是一个学校项目,它已到期,所以请帮助我:)。

1 个答案:

答案 0 :(得分:0)

我认为问题在于您没有覆盖u32string,因此适配器从未修改的列表中获取数据。

尝试这些更改

getItem

可能您还需要覆盖public CostumAdapter(Context context, ArrayList <Personne> items) { super(context,R.layout.costum_row,items); this.filterlist = items; this.items = items; //initially you want to show all items } ... @Override public Personne getItem(int position) { return items.get(position); }

getCount()