Java listview上的筛选工作不正常

Java listview上的筛选工作不正常,java,android,listview,android-edittext,textwatcher,Java,Android,Listview,Android Edittext,Textwatcher,我在筛选列表时遇到问题。我正在使用onTextChange()方法。它工作正常,但问题是,当我在编辑文本中键入单个字符时,软键盘消失,我必须再次点击编辑文本以键入完整的字符串。我还编写了代码,以便在文本更改后软键盘不会消失,但要键入,我必须点击编辑文本。以下是我的代码: search.addTextChangedListener(new TextWatcher() { @ Override public void onTextChanged(CharSequen

我在筛选列表时遇到问题。我正在使用onTextChange()方法。它工作正常,但问题是,当我在编辑文本中键入单个字符时,软键盘消失,我必须再次点击编辑文本以键入完整的字符串。我还编写了代码,以便在文本更改后软键盘不会消失,但要键入,我必须点击编辑文本。以下是我的代码:

 search.addTextChangedListener(new TextWatcher() {
        @ Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // TODO Auto-generated method stub
            String startwith = s.toString();
            ArrayList < Myshares > filterList = new ArrayList < Myshares > ();
            for (int i = 0; i < fileList.size(); i++) {
                if (fileList.get(i).getName().toLowerCase().startsWith(startwith) || fileList.get(i).getName().toUpperCase().startsWith(startwith)) {
                    filterList.add(fileList.get(i));
                }
            }
            adapter = new MListAdapter(PlayListActivity.this, filterList);

            playListView.setAdapter(adapter);
            adapter.notifyDataSetChanged();

        }
        @
        Override
        public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {    
        }
        @
        Override
        public void afterTextChanged(Editable s) {
            search.setSelection(search.getText().length());
            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.showSoftInput(search, InputMethodManager.SHOW_IMPLICIT);
        }
    });
search.addTextChangedListener(新的TextWatcher(){
@凌驾
public void onTextChanged(字符序列、int start、int before、int count){
//TODO自动生成的方法存根
字符串startwith=s.toString();
ArrayListfilterList=新的ArrayList();
对于(int i=0;i
以下是我编辑文本的xml代码:

<EditText
         android:ems="10"
        android:id="@+id/search_bar"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_margin="5dp"
        android:layout_weight="2"
        android:background="@drawable/searchbox"
        android:editable="true"
        android:focusable="true"
        android:inputType="textFilter"
        android:hint="Search"
        android:keepScreenOn="true"
        android:paddingLeft="10dp"
        android:textSize="15dp" />

我还在列表视图上设置了android:focusable=“false”属性,这样它就不会和软键盘重叠。但是编辑文本的问题仍然是一样的

当我插入一个单字符列表时,它会得到一个过滤器,光标从编辑文本框中消失

//ArrayAdapter Definition. Make it activity class field.
ArrayAdapter<String> adapter;

//initialize the adapter by loading complete data and set Adapter to ListView.
//**Note: I have used ArrayList<String> below.**
ArrayList <String> filterList = new ArrayList <Myshares> ();

for (int i = 0; i < fileList.size(); i++) {
            if (fileList.get(i).getName().toLowerCase() || fileList.get(i).getName().toUpperCase()) {
                filterList.add(fileList.get(i));
            }
        }

adapter = new ArrayAdapter<String>(this, R.layout.searchresult_item, R.id.search_item_name,filterList);

//Here, searchresult_item is the layout file name. and search_item_name is the Textview inside it.

search.addTextChangedListener(new TextWatcher() {

@Override
public void onTextChanged(CharSequence cs, int start, int before, int count) {

        YourActivity.this.Youradapter.getFilter().filter(cs);

}

@Override
public void beforeTextChanged(CharSequence s, int start, int count,int after) {


}

@Override
public void afterTextChanged(Editable s) {


}
});

//阵列适配器定义。将其设置为活动类字段。
阵列适配器;
//通过加载完整数据初始化适配器,并将适配器设置为ListView。
//**注意:我使用了下面的ArrayList**
ArrayList filterList=新的ArrayList();
对于(int i=0;i
我浏览了ArrayAdapter的实际源代码,看起来它实际上是按照这种方式编写的

ArrayAdapter有两个列表:mObjects和mOriginalValues。mObjects是适配器将使用的主要数据集。以add()函数为例:

}

mOriginalValues最初为null,因此所有操作(添加、插入、删除、清除)默认都以mobject为目标。在您决定对列表启用筛选并实际执行筛选之前,这一切都是正常的。首次筛选将使用mObjects具有的任何对象初始化MoriginalValue:

私有类ArrayFilter扩展了过滤器{
@凌驾
受保护的筛选器结果执行筛选(字符序列前缀){
FilterResults results=新的FilterResults();
if(mOriginalValues==null){
已同步(mLock){
mOriginalValues=新数组列表(mObjects);
//mOriginalValues不再为空
}
}
if(prefix==null | | prefix.length()==0){
已同步(mLock){
ArrayList=新的ArrayList(MoriginalValue);
结果.值=列表;
results.count=list.size();
}
}否则{
//过滤工作在这里进行,新的过滤集存储在newValues中
results.values=新值;
results.count=newValues.size();
}
返回结果;
}
@凌驾
受保护的void publishResults(CharSequence约束、FilterResults结果){
//未检查
mObjects=(列表)results.values;
如果(results.count>0){
notifyDataSetChanged();
}否则{
notifyDataSetionValidated();
}
}
}
mOriginalValues现在拥有原始值/项的副本,因此适配器可以通过mObjects执行其工作并显示过滤列表,而不会丢失预过滤的数据

如果我的想法不正确,请原谅(请告诉并解释),但我觉得这很奇怪,因为现在mOriginalValues不再为null,所有后续对任何适配器操作的调用都只会修改mOriginalValues。然而,由于适配器被设置为将mObjects作为其主要数据集,所以在屏幕上显示什么也没有发生。直到执行另一轮过滤为止。删除筛选器会触发以下操作:

if(prefix==null | | prefix.length()==0){
已同步(mLock){
ArrayList=新的ArrayList(MoriginalValue);
结果.值=列表;
results.count=list.size();
}
}
mOriginalValues存储在另一个列表中,并复制到mObjects,最后显示所做的更改。我们从第一个过滤器开始就一直在修改mOriginalValues(尽管我们无法在屏幕上看到它发生)。尽管如此,从这个角度来看,情况将是这样的
public void add(T object) {
    if (mOriginalValues != null) {
    synchronized (mLock) {
        mOriginalValues.add(object);
        if (mNotifyOnChange) notifyDataSetChanged();
    }
 } else {
    mObjects.add(object);
    if (mNotifyOnChange) notifyDataSetChanged();
 }
 private class ArrayFilter extends Filter {
     @Override
     protected FilterResults performFiltering(CharSequence prefix) {
    FilterResults results = new FilterResults();
    if (mOriginalValues == null) {
        synchronized (mLock) {
            mOriginalValues = new ArrayList<T>(mObjects);
            //mOriginalValues is no longer null
        }
    }
    if (prefix == null || prefix.length() == 0) {
        synchronized (mLock) {
            ArrayList<T> list = new ArrayList<T>(mOriginalValues);
            results.values = list;
            results.count = list.size();
        }
     } else {
        //filtering work happens here and a new filtered set is stored in newValues
        results.values = newValues;
        results.count = newValues.size();
     }
     return results;
  }
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
    //noinspection unchecked
    mObjects = (List<T>) results.values;
    if (results.count > 0) {
        notifyDataSetChanged();
     } else {
        notifyDataSetInvalidated();
     }
  }
 }
    if (prefix == null || prefix.length() == 0) {
        synchronized (mLock) {
            ArrayList<T> list = new ArrayList<T>(mOriginalValues);
            results.values = list;
            results.count = list.size();
        }
    }