Android 将searchbox添加到listview
我正在尝试添加搜索框Android 将searchbox添加到listview,android,listview,Android,Listview,我正在尝试添加搜索框 我知道下面写的搜索框代码。但我不知道如何添加和放置在哪里。MainActivity类无权访问listview,因为它是在CountryXmlParser中创建的,CountryXmlParser类不扩展Activity?我正在发布MainActivity和CountryXmlParser的代码 搜索框代码 searchBox.addTextChangedListener(new TextWatcher() { public void onTextChan
我知道下面写的搜索框代码。但我不知道如何添加和放置在哪里。MainActivity类无权访问listview,因为它是在CountryXmlParser中创建的,CountryXmlParser类不扩展Activity?我正在发布MainActivity和CountryXmlParser的代码 搜索框代码
searchBox.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count) {
//get the text in the EditText
String searchString=searchBox.getText().toString();
int textLength=searchString.length();
//clear the initial data <span class="h9umc81m835" id="h9umc81m835_8">set</span>
list.clear();
for(int i=0;i<originalValues.size();i++)
{
String countryName=originalValues.get(i).get("country").toString();
if(textLength<=countryName.length()){
//compare the String in EditText with Names in the ArrayList
if(searchString.equalsIgnoreCase(countryName.substring(0,textLength)))
searchResults.add(originalValues.get(i));
}
}
adapter.notifyDataSetChanged();
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void afterTextChanged(Editable s) {
}
});
searchBox.addTextChangedListener(新的TextWatcher(){
public void onTextChanged(字符序列、int start、int before、int count){
//获取编辑文本中的文本
String searchString=searchBox.getText().toString();
int textLength=searchString.length();
//清除初始数据集
list.clear();
对于(int i=0;i保留对适配器的引用,并告诉它筛选结果:
adapter.getFilter().filter(sometext);
如果从getFilter()
返回的筛选器不适合您,您可以通过重写
编辑:
试着这样做:
在活动中,在onCreate之前,将ListView、Adapter和searchbox定义为类变量。在onCreate中,找到searchbox
Public class MainActivity extends Activity {
ListView listView;
SimpleAdapter adapter;
EditText searchBox;
@Override
public void onCreate(Bundle savedInstanceState) {
setContentView(..);
searchBox = (EditText)findViewById(...);
...
在您的列表视图LoaderTask
onPostExecute
中,保存这些引用并添加侦听器:
listView = ( ListView ) findViewById(R.id.lv_countries);
adapter = adapter;
/** Setting the adapter containing the country list to listview */
listView.setAdapter(adapter);
//set textwatcher
searchBox.addTextChangeListener(...)
在textwatcher afterTextChanged
filter results(如前所述)中,u plzz能否建议将此代码放在哪个类中以及如何工作?MainActivity类无权访问listview,因为它是在CountryXmlParser中创建的,CountryXmlParser类不会在您的搜索框代码中扩展Actvit您似乎有一些参考ce到适配器,因为您调用了adapter.notifyDataSetChanged();
。您有afterTextChanged
方法,我认为该方法返回搜索框中当前文本的可编辑表示形式。filter()
方法应接受可编辑。请尝试使用.thnks作为您的答案。但我的问题是将此搜索框代码放在何处。MainActivity类无权访问listview,因为它是在CountryXmlParser中创建的,CountryXmlParser类不扩展Activity。您的答案与我的问题有何关系?谢谢。.如您所说更新了我的代码。.但出现了错误是“无法解析列表”。列表在CountryXmlParser.java中声明,因此无法在MainActivity.java中访问
Public class MainActivity extends Activity {
ListView listView;
SimpleAdapter adapter;
EditText searchBox;
@Override
public void onCreate(Bundle savedInstanceState) {
setContentView(..);
searchBox = (EditText)findViewById(...);
...
listView = ( ListView ) findViewById(R.id.lv_countries);
adapter = adapter;
/** Setting the adapter containing the country list to listview */
listView.setAdapter(adapter);
//set textwatcher
searchBox.addTextChangeListener(...)