我无法从中搜索listview
,我尝试了各种方法,但它对我不起作用(没有错误)。我有其他搜索方式,但我想让这种方式成为可能。
这是代码
public class MainActivity extends Activity implements TextWatcher {
CustomAdapter mAdapter;
ListView mListview;
EditText search;
List<String> tempCity, cityNames;
String list[];
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();
bindComponents();
addListeners();
}
private void addListeners() {
// TODO Auto-generated method stub
search.addTextChangedListener(this);
}
private void init() {
// TODO Auto-generated method stub
mPos = new ArrayList<Integer>();
tempCity = new ArrayList<String>();
cityNames = new ArrayList<String>();
list = getResources().getStringArray(R.array.cityList);
cityNames.addAll(Arrays.asList(list));
}
private void bindComponents() {
// TODO Auto-generated method stub
mListview = (ListView) findViewById(R.id.cityList);
mAdapter = new CustomAdapter();
search = (EditText) findViewById(R.id.eTsearch);
mListview.setAdapter(mAdapter);
}
public class CustomAdapter extends ArrayAdapter<String> {
public CustomAdapter() {
super(MainActivity.this, android.R.layout.simple_list_item_1,
cityNames);
// TODO Auto-generated constructor stub
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return cityNames.size();
}
@Override
public String getItem(int pos) {
// TODO Auto-generated method stub
return cityNames.get(pos);
}
@Override
public long getItemId(int pos) {
// TODO Auto-generated method stub
return pos;
}
@Override
public View getView(int pos, View v, ViewGroup vGroup) {
// TODO Auto-generated method stub
View row = v;
if (row == null) {
row = getLayoutInflater().inflate(R.layout.main_row, null);
}
TextView mCityTv = (TextView) row.findViewById(R.id.itemTv);
mCityTv.setText(cityNames.get(pos));
return row;
}
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
mAdapter.getFilter().filter(s.toString());
}
}
任何帮助将不胜感激..
谢谢