私はGridViewのとのEditTextを持っているシンプルなアプリの願い事をしようとしていると私はのEditTextで入力すると、それはここで入力アンドロイド:フィルタはcostumのGridViewのアダプタで動作していない
をコードする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","[email protected]"));
items.add(new Personne("Tazir","oussama","0654613257","[email protected]"));
items.add(new Personne("kabouya","imed","0553164597","[email protected]"));
items.add(new Personne("Tazir","oussama","0654613257","[email protected]"));
items.add(new Personne("kabouya","imed","0553164597","[email protected]"));
items.add(new Personne("Tazir","oussama","0654613257","[email protected]"));
items.add(new Personne("kabouya","imed","0553164597","[email protected]"));
items.add(new Personne("Tazir","oussama","0654613257","[email protected]"));
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();
}
}
);
}
が、これは学校のプロジェクトであり、それはそうplsは私を助けて期日を持っている:)
をオーバーライドする必要があなたの調査結果は、今までは何ですか?フィルタリングが機能していて、publishResultsが呼び出されていますか? – diedu