我有一個ListView與名稱的人和一個ActionBar與搜索小部件。 我想要做的是將包含搜索查詢的ListView中的第一項聚焦爲子字符串。Android ListView搜索和焦點
例如,如果我有一個名爲「John Doe」的人,並且我搜索「hn D」行,只要它是第一個包含「hn D」作爲子字符串的行,就應該關注它。
請注意,我不希望從列表中刪除不包含子字符串的項目。
這就是我如何製作清單。
activity_main.xml中
<ListView android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
list_item.xml
<TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dip"
android:textSize="16sp"
android:textStyle="bold" />
MainActivity.java
String[] names = {"John Doe","Mark Marky","Donald Duck","Derp Derpson"};
lv = (ListView) findViewById(R.id.list_view);
adapter = new ArrayAdapter<String>(this,R.layout.list_item, R.id.name, names);
lv.setAdapter(adapter);
我也有一個帶有搜索小部件的操作欄,我實現了onQueryTextChange()
和onQueryTextSubmit()
方法。
問題是如何在這些方法中搜索列表,以及如何關注適當的列表項?
嗯,我不太確定,但我認爲你可以使用'String.co獲取包含你的搜索的第一個元素的索引ntains()'在'String []'的每個元素上。如果你得到這個索引,你可以使用'mListView.requestFocus();'&'mListView.setSelection(position);' –