Ham*_*amy 159 android listview filter android-widget
在Android上,我如何ListView
根据用户输入进行过滤,其中显示的项目是根据TextView
值动态更新的?
我正在寻找这样的东西:
-------------------------
| Text View |
-------------------------
| List item |
| List item |
| List item |
| List item |
| |
| |
| |
| |
-------------------------
Run Code Online (Sandbox Code Playgroud)
Ham*_*amy 286
首先,您需要创建一个包含EditText和ListView的XML布局.
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<!-- Pretty hint text, and maxLines -->
<EditText android:id="@+building_list/search_box"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="type to filter"
android:inputType="text"
android:maxLines="1"/>
<!-- Set height to 0, and let the weight param expand it -->
<!-- Note the use of the default ID! This lets us use a
ListActivity still! -->
<ListView android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
这将使一切正常,在ListView上方有一个很好的EditText.接下来,像平常一样创建ListActivity,但setContentView()
在onCreate()
方法中添加一个调用,以便我们使用最近声明的布局.请记住,我们ListView
特意识别了android:id="@android:id/list"
.这允许我们ListActivity
知道ListView
在声明的布局中使用哪个.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.filterable_listview);
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
getStringArrayList());
}
Run Code Online (Sandbox Code Playgroud)
现在运行应用程序应该显示您的上一个ListView
,上面有一个很好的框.为了使该框执行某些操作,我们需要从中获取输入,并使该输入过滤列表.虽然很多人都尝试手动执行此操作,但大多数 ListView
Adapter
类都附带了一个Filter
可用于自动执行过滤的对象.我们只需将输入管道输入EditText
到Filter
.事实证明这很容易.要运行快速测试,请将此行添加到您的onCreate()
通话中
adapter.getFilter().filter(s);
Run Code Online (Sandbox Code Playgroud)
请注意,您需要将您保存ListAdapter
到变量才能使其正常工作 - 我已将我ArrayAdapter<String>
之前保存到名为'adapter'的变量中.
下一步是从中获取输入EditText
.这实际上需要一些思考.你可以添加OnKeyListener()
到您的EditText
.但是,此侦听器仅接收一些关键事件.例如,如果用户输入"wyw",预测文本可能会推荐"眼睛".在用户选择"wyw"或"eye"之前,您OnKeyListener
将不会收到关键事件.有些人可能更喜欢这种解决方案,但我发现它令人沮丧.我想要每个关键事件,所以我可以选择过滤或不过滤.解决方案是TextWatcher
.只需创建并添加TextWatcher
到EditText
,并通过ListAdapter
Filter
每一次的过滤器请求文本更改.记得删除TextWatcher
in OnDestroy()
!这是最终的解决方案:
private EditText filterText = null;
ArrayAdapter<String> adapter = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.filterable_listview);
filterText = (EditText) findViewById(R.id.search_box);
filterText.addTextChangedListener(filterTextWatcher);
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
getStringArrayList());
}
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) {
adapter.getFilter().filter(s);
}
};
@Override
protected void onDestroy() {
super.onDestroy();
filterText.removeTextChangedListener(filterTextWatcher);
}
Run Code Online (Sandbox Code Playgroud)
j7n*_*n7k 10
运行程序将导致力关闭.
我划过界限:
机器人:ID = "@ + building_list/search_box"
同
机器人:ID = "@ + ID/search_box"
这可能是问题吗?什么是'@ + building_list'?