在应用程序中,我有一个扩展 BaseAdapter 的自定义适配器。适配器包括两个项目的标题,其中一个是搜索栏。我要做的是使用我的适配器在栏上输入文本以搜索列表视图上的内容。这里的挑战是三星 Galaxy Note II,当我输入一个字符时,搜索栏会显示它是什么。但是,对于我要输入的第二个字符,该栏没有显示任何内容,并且似乎前一个字符也被清除了。对于任何其他设备,这都很好。我对此一无所知。任何大师都可以知道解决方案吗?
下面是 ResearveHeaderAdapter 的一些片段:
@Override
public View getView(final int position, final View convertView, final ViewGroup parent)
{
View rowView = null;
HeaderItem header = mHeaderDef.get(position);
if (header != null)
{
switch(header)
{
case REFRESHBAR:
{
rowView = updateRefreshRow(convertView, parent);
break;
}
case SEARCHBAR:
{
rowView = updateSearchRow(convertView, parent);
break;
}
default:
{
rowView = super.getView(position, convertView, parent);
break;
}
}
}
else
{
rowView = super.getView(position, convertView, parent);
}
return rowView;
}
private View updateSearchRow(final View convertView, final ViewGroup parent)
{
mCacheSearchView = mAllContentInflator.inflate( R.layout.search_bar_item, parent, false );
mEditText = (ClearableEditText) mCacheSearchView.findViewById(R.id.topSearchBar);
mEditText.setText(getSearchVarString());
mEditText.setOnKeyListener(getSearchListener());
mEditText.addTextChangedListener(getSearchTextWatcher());
String search = getSearchVarString();
String stxt = mEditText.getText().toString();
if (getSearchVarString().trim().length() > 0)
{
mEditText.requestFocus();
}
mEditText.setEnabled(true);
return mCacheSearchView;
}
它的子类 AllContentAdapter:
public class AllContentAdapter extends ReserveHeaderAdapter
{
public AllContentAdapter(Activity callerContext,
MediaObjectManager manager, boolean sensitive,
MediaPolicy displayPolicy) {
super(callerContext, manager, sensitive, displayPolicy);
// TODO Auto-generated constructor stub
super.appendHeaderItem(HeaderItem.REFRESHBAR);
super.appendHeaderItem(HeaderItem.SEARCHBAR);
}
private static OnKeyListener mOnKeySearchListener = null;
private static TextWatcher mSearchTextWatcher = null;
private static OnClickListener mOnClickRefreshListener = null;
private static String mSearchString = "";
@Override
protected String getSearchVarString()
{
return mSearchString;
}
@Override
protected void setSearchVarString(String searchString)
{
mSearchString = searchString;
}
@Override
protected OnKeyListener getSearchListener()
{
return mOnKeySearchListener;
}
@Override
protected TextWatcher getSearchTextWatcher()
{
return mSearchTextWatcher;
}
@Override
protected OnClickListener getClickRefreshListener()
{
return mOnClickRefreshListener;
}
public void setOnClickRefreshListener(OnClickListener listener)
{
mOnClickRefreshListener = listener;
}
public void setSearchOnKeyListener(OnKeyListener listener)
{
mOnKeySearchListener = listener;
}
public void addTextChangedListenerForSearch(TextWatcher listener)
{
mSearchTextWatcher = listener;
}
因此,对于所需的 Activity,我将adapter.addTextChangedListenerForSearch(new TextWatcher())
其称为如下:
((AllContentAdapter) mAdapter).addTextChangedListenerForSearch(new TextWatcher()
{
public void afterTextChanged(Editable s)
{
String stxt = s.toString();
((AllContentAdapter) mAdapter).setSearchVarString(stxt);
// some handle code omitted...
AllContentScreen.this.loadSearchFilter();// do some filter to search desired content
}
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{
}
public void onTextChanged(CharSequence s, int start, int before, int count)
{
}
});
真正的结果是,在 Note II 上,当输入第二个字符时,(Editable)s 会捕捉到注意但为空的字符串。为什么?