Android 在listview中搜索不';行不通
我无法在我的Android应用程序中完成这项研究。 我创建了一个editText,并在其中添加了一个TextWatcher。在我的自定义数组适配器中,我重写了getFilter函数来过滤结果并更新listview 我创建了一个edittext并对其进行了设置:Android 在listview中搜索不';行不通,android,listview,Android,Listview,我无法在我的Android应用程序中完成这项研究。 我创建了一个editText,并在其中添加了一个TextWatcher。在我的自定义数组适配器中,我重写了getFilter函数来过滤结果并更新listview 我创建了一个edittext并对其进行了设置: et.addTextChangedListener(new TextWatcher() { @Override public void onTextChanged(CharSequence s, int
et.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
adapter.getFilter().filter(s.toString());
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable arg0) {
//ContactActivity.this.adapter.getFilter().filter(arg0);
}
我的ContactAdapter是:
public ContactAdapter(Context context, int textViewResourceId, ArrayList<String> objects) {
super(context, textViewResourceId, objects);
this.objects = objects;
}
/*
* we are overriding the getView method here - this is what defines how each
* list item will look.
*/
public View getView(int position, View convertView, ViewGroup parent){
// assign the view we are converting to a local variable
View v = convertView;
// first check to see if the view is null. if so, we have to inflate it.
// to inflate it basically means to render, or show, the view.
if (v == null) {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.list_item, null);
}
/*
* Recall that the variable position is sent in as an argument to this method.
* The variable simply refers to the position of the current object in the list. (The ArrayAdapter
* iterates through the list we sent it)
*
* Therefore, i refers to the current Item object.
*/
String i = objects.get(position);
TextView tt = (TextView) v.findViewById(R.id.name);
if (i != null) {
// This is how you obtain a reference to the TextViews.
// These TextViews are created in the XML files we defined.
if (tt != null){
tt.setText(i);
}
}
// the view must be returned to our activity
return v;
}
@Override
public Filter getFilter() {
return new Filter() {
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
ArrayList<String> list = (ArrayList<String>) results.values;
int size = list.size();
//list.clear();
for (int i = 0; i<list.size();i++){
add(list.get(i));
}
notifyDataSetChanged();
}
@Override
protected FilterResults performFiltering(CharSequence constraint) {
ArrayList<String> filteredResults = getFilteredResults(constraint);
FilterResults results = new FilterResults();
results.values = filteredResults;
return results;
}
private ArrayList<String> getFilteredResults(CharSequence constraint) {
ArrayList<String> names = ContactAdapter.this.objects;
ArrayList<String> filteredNames = new ArrayList<String>();
for(int i=0;i< names.size();i++){
if(names.get(i).toLowerCase().startsWith(constraint.toString().toLowerCase())){
filteredNames.add(names.get(i));
}
}
return filteredNames;
}
};
}
}
public ContactAdapter(上下文上下文、int textViewResourceId、ArrayList对象){
超级(上下文、textViewResourceId、对象);
this.objects=对象;
}
/*
*我们在这里重写getView方法-这就是定义每个
*列表项将显示。
*/
公共视图getView(int位置、视图转换视图、视图组父视图){
//将要转换的视图指定为局部变量
视图v=转换视图;
//首先检查视图是否为空。如果是,我们必须将其充气。
//充气基本上意味着渲染或显示视图。
如果(v==null){
LayoutInflater充气器=(LayoutInflater)getContext().getSystemService(Context.LAYOUT\u充气器\u SERVICE);
v=充气机充气(R.layout.list_项,空);
}
/*
*回想一下,变量位置作为参数发送到此方法。
*变量只是引用当前对象在列表中的位置。(ArrayAdapter
*遍历我们发送的列表)
*
*因此,我引用了当前的Item对象。
*/
字符串i=objects.get(位置);
TextView tt=(TextView)v.findViewById(R.id.name);
如果(i!=null){
//这是获取对TextView的引用的方式。
//这些文本视图是在我们定义的XML文件中创建的。
如果(tt!=null){
tt.setText(i);
}
}
//视图必须返回到我们的活动
返回v;
}
@凌驾
公共过滤器getFilter(){
返回新筛选器(){
@凌驾
受保护的void publishResults(CharSequence约束、FilterResults结果){
ArrayList=(ArrayList)results.values;
int size=list.size();
//list.clear();
对于(int i=0;i您可以将此函数添加到自定义适配器:
@重写公共筛选器getFilter(){
源代码[main.xml]是
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical">
<EditText android:id="@+id/EditText01"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:hint="Search">
</EditText>
<ListView android:id="@+id/ListView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
源代码[ListViewSearchExample.java]是
package com.ListViewSearchExample;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
public class ListViewSearchExample extends Activity
{
private ListView lv;
private EditText et;
private String listview_array[] = { "ONE", "TWO", "THREE", "FOUR", "FIVE",
"SIX", "SEVEN", "EIGHT", "NINE", "TEN" };
private ArrayList<String> array_sort= new ArrayList<String>();
int textlength=0;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lv = (ListView) findViewById(R.id.ListView01);
et = (EditText) findViewById(R.id.EditText01);
lv.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, listview_array));
et.addTextChangedListener(new TextWatcher()
{
public void afterTextChanged(Editable s)
{
// Abstract Method of TextWatcher Interface.
}
public void beforeTextChanged(CharSequence s,
int start, int count, int after)
{
// Abstract Method of TextWatcher Interface.
}
public void onTextChanged(CharSequence s,
int start, int before, int count)
{
textlength = et.getText().length();
array_sort.clear();
for (int i = 0; i < listview_array.length; i++)
{
if (textlength <= listview_array[i].length())
{
if(et.getText().toString().equalsIgnoreCase(
(String)
listview_array[i].subSequence(0,
textlength)))
{
array_sort.add(listview_array[i]);
}
}
}
lv.setAdapter(new ArrayAdapter<String>
(ListViewSearchExample.this,
android.R.layout.simple_list_item_1, array_sort));
}
});
}
}
package com.ListViewSearchExample;
导入java.util.ArrayList;
导入android.app.Activity;
导入android.os.Bundle;
导入android.text.Editable;
导入android.text.TextWatcher;
导入android.widget.ArrayAdapter;
导入android.widget.EditText;
导入android.widget.ListView;
公共类ListViewSearchExample扩展活动
{
私有ListView lv;
私人编辑;
私有字符串listview_数组[]={“一”、“二”、“三”、“四”、“五”,
“六”、“七”、“八”、“九”、“十”};
私有ArrayList数组_sort=新ArrayList();
int textlength=0;
创建时的公共void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lv=(ListView)findViewById(R.id.ListView01);
et=(EditText)findViewById(R.id.EditText01);
lv.setAdapter(新阵列适配器),
android.R.layout.simple_list_item_1,listview_array));
et.addTextChangedListener(新的TextWatcher()
{
公共无效后文本已更改(可编辑)
{
//TextWatcher接口的抽象方法。
}
更改前的公共无效文本(字符序列,
整数开始、整数计数、整数结束)
{
//TextWatcher接口的抽象方法。
}
公共void onTextChanged(字符序列,
整数开始、整数之前、整数计数)
{
textlength=et.getText().length();
数组_sort.clear();
for(int i=0;i<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical">
<EditText android:id="@+id/EditText01"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:hint="Search">
</EditText>
<ListView android:id="@+id/ListView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
package com.ListViewSearchExample;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
public class ListViewSearchExample extends Activity
{
private ListView lv;
private EditText et;
private String listview_array[] = { "ONE", "TWO", "THREE", "FOUR", "FIVE",
"SIX", "SEVEN", "EIGHT", "NINE", "TEN" };
private ArrayList<String> array_sort= new ArrayList<String>();
int textlength=0;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lv = (ListView) findViewById(R.id.ListView01);
et = (EditText) findViewById(R.id.EditText01);
lv.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, listview_array));
et.addTextChangedListener(new TextWatcher()
{
public void afterTextChanged(Editable s)
{
// Abstract Method of TextWatcher Interface.
}
public void beforeTextChanged(CharSequence s,
int start, int count, int after)
{
// Abstract Method of TextWatcher Interface.
}
public void onTextChanged(CharSequence s,
int start, int before, int count)
{
textlength = et.getText().length();
array_sort.clear();
for (int i = 0; i < listview_array.length; i++)
{
if (textlength <= listview_array[i].length())
{
if(et.getText().toString().equalsIgnoreCase(
(String)
listview_array[i].subSequence(0,
textlength)))
{
array_sort.add(listview_array[i]);
}
}
}
lv.setAdapter(new ArrayAdapter<String>
(ListViewSearchExample.this,
android.R.layout.simple_list_item_1, array_sort));
}
});
}
}