Android アプリで調査を行うことができません。editText を作成し、それに TextWatcher を追加しました。カスタム配列アダプターでは、getFilter 関数をオーバーライドして結果をフィルター処理し、リストビューを更新しました。
私は編集テキストを作成し、それに設定しました:
et.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
adapter.getFilter().filter(s.toString());
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable arg0) {
//ContactActivity.this.adapter.getFilter().filter(arg0);
}
私のContactAdapterは次のとおりです。
public ContactAdapter(Context context, int textViewResourceId, ArrayList<String> objects) {
super(context, textViewResourceId, objects);
this.objects = objects;
}
/*
* we are overriding the getView method here - this is what defines how each
* list item will look.
*/
public View getView(int position, View convertView, ViewGroup parent){
// assign the view we are converting to a local variable
View v = convertView;
// first check to see if the view is null. if so, we have to inflate it.
// to inflate it basically means to render, or show, the view.
if (v == null) {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.list_item, null);
}
/*
* Recall that the variable position is sent in as an argument to this method.
* The variable simply refers to the position of the current object in the list. (The ArrayAdapter
* iterates through the list we sent it)
*
* Therefore, i refers to the current Item object.
*/
String i = objects.get(position);
TextView tt = (TextView) v.findViewById(R.id.name);
if (i != null) {
// This is how you obtain a reference to the TextViews.
// These TextViews are created in the XML files we defined.
if (tt != null){
tt.setText(i);
}
}
// the view must be returned to our activity
return v;
}
@Override
public Filter getFilter() {
return new Filter() {
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
ArrayList<String> list = (ArrayList<String>) results.values;
int size = list.size();
//list.clear();
for (int i = 0; i<list.size();i++){
add(list.get(i));
}
notifyDataSetChanged();
}
@Override
protected FilterResults performFiltering(CharSequence constraint) {
ArrayList<String> filteredResults = getFilteredResults(constraint);
FilterResults results = new FilterResults();
results.values = filteredResults;
return results;
}
private ArrayList<String> getFilteredResults(CharSequence constraint) {
ArrayList<String> names = ContactAdapter.this.objects;
ArrayList<String> filteredNames = new ArrayList<String>();
for(int i=0;i< names.size();i++){
if(names.get(i).toLowerCase().startsWith(constraint.toString().toLowerCase())){
filteredNames.add(names.get(i));
}
}
return filteredNames;
}
};
}
}
提案?ありがとう