我有一个包含大约 30 个项目的 listView,并通过 editText 向其中添加了搜索功能。当我在文本字段中输入“a”时,所有以“a”开头的内容都会显示在列表中,但是当我输入另一个字母时,即使列表包含带有“a”的项目+我输入的另一个字母,列表也会消失。
让我感到困惑的另一件事是列表中有一个名为 IWU Trading 的项目,这是我唯一可以搜索的项目,这意味着如果我键入“I”+“W”,该项目就会显示在 listView 中。但是,如果我输入“a”+“r”,就会出现一个名为“art”的项目。
我的问题。我该怎么做才能使此搜索功能起作用?为什么它的行为如此?
我的 XML
<EditText
android:id="@+id/searchEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/selectCustomerTextView"
android:layout_marginTop="20dp"
android:hint="@string/typeToSearch"
android:ems="10"
android:imeOptions="actionDone"
android:inputType="textNoSuggestions"/>
<ListView
android:id="@+id/list_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/nextButton"
android:layout_alignParentLeft="true"
android:layout_below="@+id/searchEditText"
android:choiceMode="singleChoice"
android:dividerHeight="5dp" >
</ListView>
我的代码:
private ArrayAdapter<Customer> _oldAdapter = null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
setContentView(R.layout.activity_customer_pick);
EditText searchText = (EditText) findViewById(R.id.searchEditText);
ListView listView = (ListView) findViewById(R.id.list_view);
listView.setClickable(true);
searchText.addTextChangedListener(filterTextWatcher);
_oldAdapter = _phoneDAL.BindValues(this);
listView.setAdapter(_oldAdapter);
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
String s = _oldAdapter.getItem(arg2).toString();
_listViewPostion = arg2;
Toast.makeText(CustomerPick.this, "Du valde: " + s, arg2).show();
}
});
}
搜索方法:
private TextWatcher filterTextWatcher = new TextWatcher() {
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
ArrayAdapter<Customer> _adapter = new ArrayAdapter<Customer>(CustomerPick.this, android.R.layout.simple_list_item_single_choice, new ArrayList<Customer>());
ListView listView = (ListView) findViewById(R.id.list_view);
int textLength = s.length();
if(textLength == 0){
listView.setAdapter(_oldAdapter);
return;
}
for (int i = 0; i < listView.getAdapter().getCount(); i++)
{
String word = _oldAdapter.getItem(i).getCustName().toLowerCase();
if (textLength <= word.length())
{
if(s.toString().equalsIgnoreCase(word.substring(0, textLength)))
{
_adapter.add(_oldAdapter.getItem(i));
}
}
}
listView.setAdapter(_adapter);
}
};
}
我的 CustomerClass(私有 ArrayAdapter _oldAdapter = null;)
public class Customer {
private final int _custNumber;
private final String _custName;
public Customer(int custNumber, String custName){
_custNumber = custNumber;
_custName = custName;
}
public int getCustNumber() {
return _custNumber;
}
public String getCustName() {
return _custName;
}
@Override
public String toString(){
return _custName;
}
}