我正在開發應用程序,其中有列表和上面的編輯文本,現在我會在editText中鍵入的東西我應該得到匹配的輸入word.bt的結果我不能得到結果,Plz告訴我在哪裏犯錯誤?以下是我的活動和適配器。列表過濾器自定義適配器不給結果
**活動* *
public class ListFilterActivity extends ListActivity{
ListView list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_filter);
list = getListView();
list.isTextFilterEnabled();
// final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
// android.R.layout.simple_list_item_1, android.R.id.text1,
// getModel());
final CustomAdapter adapter = new CustomAdapter(ListFilterActivity.this, getModel());
setListAdapter(adapter);
EditText filterEditText = (EditText) findViewById(R.id.filterText);
filterEditText.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before,
int count) {
adapter.getFilter().filter(s.toString());
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void afterTextChanged(Editable s) {
}
});
}
private ArrayList<String> getModel() {
ArrayList<String> list = new ArrayList<String>();
list.add("Linux");
list.add("Windows7");
list.add("Suse");
list.add("Eclipse");
list.add("Ubuntu");
list.add("Solaris");
list.add("Android");
list.add("Ayes");
list.add("iPhone");
return list;
}
}
適配器 ***
public class CustomAdapter extends ArrayAdapter<String> {
private final Context context;
private final ArrayList<String> names;
private LayoutInflater mInflater;
private TextView txt;
public CustomAdapter(Context context, ArrayList<String> names) {
super(context, R.layout.row, names);
this.context = context;
this.names = names;
mInflater = LayoutInflater.from(context);
}
public Filter getFilter() {
if(newFilter == null) {
newFilter = new Filter() {
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
// TODO Auto-generated method stub
Log.d("TAG", "publishResults");
notifyDataSetChanged();
}
@Override
protected FilterResults performFiltering(CharSequence prefix) {
FilterResults results = new FilterResults();
ArrayList<String> i = new ArrayList<String>();
if (prefix!= null && prefix.toString().length() > 0) {
for (int index = 0; index < names.size(); index++) {
String si = names.get(index);
if(si.compareTo(prefix.toString()) == 0){
i.add(si);
}
}
results.values = i;
results.count = i.size();
}
else{
synchronized (names){
results.values = names;
results.count = names.size();
}
}
return results;
}
};
}
// Log.d("TAG", "end getFilter");
return newFilter;
}
@Override
public String getItem(int position) {
return super.getItem(position);
}
@Override
public int getCount() {
return super.getCount();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
if(rowView == null) {
rowView = mInflater.inflate(R.layout.row, null);
txt = (TextView)rowView.findViewById(R.id.text1);
}
else {
rowView.getTag();
}
txt.setText(names.get(position));
return rowView;
}
}
PLZ幫我....
謝謝,它的工作 – Richa 2011-12-30 11:16:42
@Lalit Poptani +1 Nice answer !!! – Venky 2011-12-30 12:48:43
偉大的@我和我們現在它的工作,謝謝 – Weloo 2012-04-24 14:50:16