Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/217.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 将searchbox添加到listview_Android_Listview - Fatal编程技术网

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(...)