如何将搜索添加到列表视图android,按您的类型对项目进行排序
Posted
技术标签:
【中文标题】如何将搜索添加到列表视图android,按您的类型对项目进行排序【英文标题】:how to add search to a list view android, sort items as u type 【发布时间】:2012-01-29 03:34:09 【问题描述】:我有一个带有搜索栏的列表视图,我想在编辑文本中键入文本时对项目进行排序。我一直在尝试做一整天,但无法完成,你们能帮我做吗?我会很感激你提前谢谢
【问题讨论】:
参考这个***.com/questions/1737009/… 【参考方案1】:请参考此代码
package com.android.swfPlayer;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
public class SwfAndroidActivity extends Activity
/** Called when the activity is first created. */
private EditText edtField;
ListView l1;
ArrayList<String> array = new ArrayList<String>();
ArrayList<String> type_name_copy = new ArrayList<String>();
String[] array1 = "abd", "bcd", "cdg", "sada", "fs", "fsf", "fsf" ;
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
array.add("abd");
array.add("bcd");
array.add("cdg");
array.add("sada");
array.add("abd");
array.add("fs");
array.add("fsf");
array.add("afsf");
l1 = (ListView) findViewById(R.id.listView1);
edtField = (EditText) findViewById(R.id.editText1);
listUpdate(array);
edtField.addTextChangedListener(new TextWatcher()
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count)
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after)
@Override
public void afterTextChanged(Editable theWatchedText)
ArrayList<String> type_name_filter = new ArrayList<String>();
String text = theWatchedText.toString();
for (int i = 0; i < array.size(); i++)
if ((array.get(i).toLowerCase()).contains(text
.toLowerCase()))
type_name_filter.add(array.get(i));
type_name_copy = type_name_filter;
listUpdate(type_name_copy);
);
public void listUpdate(ArrayList<String> data)
l1.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, data));
【讨论】:
这里你用一个静态数组来做这件事。如果没有那个我怎么办?文本改变了? 你只需要传递动态数组而不是静态数组【参考方案2】:你必须在 afterTextChanged 时使用 lv.setAdapter(this.adapter) 设置适配器。
@Override
public void afterTextChanged(Editable s)
//adapeter should be member variable
lv.setAdapter(this.adapter)
【讨论】:
感谢您的帮助..但这不起作用..我想在我们输入时进行过滤 你确定这条线吗? adapter.getFilter().filter(s.toString().trim()) ; 是的,这就是我写的……我应该改变吗?当我开始输入时,列表消失了 您想在输入时还是输入后过滤列表? @Override public void afterTextChanged(Editable s) //adapeter 应该是成员变量 lv.setAdapter(this.adapter) 【参考方案3】:data = new ArrayList<Item>(10);
data.add(new Item(100, "Sample Food1"));
data.add(new Item(110, "Sample Food2"));
data.add(new Item(120, "Sample Food3"));
data.add(new Item(130, "Sample Food4"));
data.add(new Item(140, "Sample Food5"));
data.add(new Item(150, "Sample Food6"));
data.add(new Item(160, "Sample Food7"));
data.add(new Item(170, "Sample Food8"));
data.add(new Item(180, "Sample Food9"));
data.add(new Item(190, "Sample Food10"));
data.add(new Item(200, "Sample Food11"));
data.add(new Item(210, "Sample Food12"));
Collections.sort(data, new Comparator<Item>()
public int compare(Item i1, Item i2)
return i1.getCaption().compareTo(i2.getCaption());
);
adapter = new ItemListAdapter(this, data);
【讨论】:
【参考方案4】:Use combination of methods startsWith() and contains().
public void onTextChanged(CharSequence str, int start, int before,
int count)
array_sort.clear();
for (int i = 0; i < myList.size(); i++)
String tex_inlist = myList.get(i).getTitle()
.toLowerCase();
if (tex_inlist.startsWith(str))
array_sort.add(0,myList.get(i));//add to the begining of list
else if(tex_inlist.contains(str))
array_sort.add(myList.get(i));//add to the end of list
RateArrayAdapter new_adapter = new RateArrayAdapter(
(Activity) context, array_sort);
lvDetail.setAdapter(new_adapter);
【讨论】:
以上是关于如何将搜索添加到列表视图android,按您的类型对项目进行排序的主要内容,如果未能解决你的问题,请参考以下文章
Android Studio:如何将列表视图中的项目添加到列表视图 onClick
如何使用复选框将Android联系人添加到列表视图中[重复]