如何在我的Android应用中添加搜索功能? 我有一个listview,我想在其中添加搜索功能,我有代码,但我不知道如何正确地与我的应用程序集成 这是代码 -
inputSearch.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
MainActivity.this.adapter.getFilter().filter(cs);
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
这是我的java文件
package com.Example.app;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends Activity {
private ListView list1;
private String array[] = { "Iphone", "Tutorials", "Gallery", "Android",
"item 1", "item 2", "item3", "item 4", "item 1", "item 2", "item3", "item 4","item 1", "item 2", "item3", "item 4"};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list1 = (ListView) findViewById(R.id.ListView01);
// By using setAdpater method in listview we an add string array in
// list.
list1.setAdapter(new ArrayAdapter(this,
android.R.layout.simple_list_item_1, array));
list1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if (position == 1)
{
Intent myIntent = new Intent(getApplicationContext(),Baba.class);
startActivity(myIntent);
}
}
});
}
}
main.xml中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:stackFromBottom="true">
<EditText
android:id="@+id/inputSearch"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:hint="Search"
android:inputType="textVisiblePassword">
</EditText>
<ListView
android:id="@+id/ListView01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbarSize="5dp"
>
</ListView>
</LinearLayout>
答案 0 :(得分:1)
尝试以下
EditText inputSearch;
ArrayAdapter<String> adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
inputSearch = (EditText) findViewById(R.id.inputSearch); // initialize edittext
list1 = (ListView) findViewById(R.id.ListView01);
adapter =new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, array));
list1.setAdapter(adapter);
inputSearch.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
MainActivity.this.adapter.getFilter().filter(cs);
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
list1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if (position == 1)
{
Intent myIntent = new Intent(getApplicationContext(),Baba.class);
startActivity(myIntent);
}
}
});
}
在onCreate
初始化editText inputSearch。
inputSearch = (EditText) findViewById(R.id.inputSearch); // initialize edittext
初始化适配器
adapter =new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, array));
设置适配器操作列表视图
list1.setAdapter(adapter);
这是搜索发生的地方
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
MainActivity.this.adapter.getFilter().filter(cs); // filter based on input
}
答案 1 :(得分:0)
由于您使用的是Android的数组适配器,因此您也可以使用其getFilter()方法。所以你只需要改变代码中的小东西。
而不是像这样设置数组适配器
list1.setAdapter(new ArrayAdapter(this,android.R.layout.simple_list_item_1, array));
在方法
之外创建一个私有变量private ArrayAdapter adapter;
然后初始化并设置它
adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1, array);
list1.setAdapter(adapter);
现在,文本更改方法可以毫无问题地调用MainActivity.this.adapter.getFilter().filter(cs);