Android 如何使用textwatcher从列表视图中搜索数据?
我创建了一个listview,其中数据来自存储在string.xml中的字符串数组 我有一个edittext,我想从我的listview中搜索特定的数据。我的listview数据包含如下内容。。。我该怎么做 这是我的密码Android 如何使用textwatcher从列表视图中搜索数据?,android,listview,Android,Listview,我创建了一个listview,其中数据来自存储在string.xml中的字符串数组 我有一个edittext,我想从我的listview中搜索特定的数据。我的listview数据包含如下内容。。。我该怎么做 这是我的密码 adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arrayList); Resources res = getResources()
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, arrayList);
Resources res = getResources();
String[] a = res.getStringArray(R.array.states);
arrayList.add("" + a);
adapter.notifyDataSetChanged();
editText.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// TODO Auto-generated method stub
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
我希望下面的代码对你有用
public class MainActivity extends Activity {
private ListView lv;
private EditText et;
String listview_array[]={"123","TWO","Dell Inspiron", "HTC One X", "HTC Wildfire S", "HTC Sense", "1234", "iPhone 4S", "Samsung Galaxy Note 800", "Samsung Galaxy S3", "MacBook Air", "Mac Mini", "MacBook Pro"};
private ArrayList<String> array_sort = new ArrayList<String>();
int textlength = 0;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_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));
int x= lv.getHeaderViewsCount ();
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>
(MainActivity.this,
android.R.layout.simple_list_item_1, array_sort));
}
});
}
}
我希望下面的代码对你有用
public class MainActivity extends Activity {
private ListView lv;
private EditText et;
String listview_array[]={"123","TWO","Dell Inspiron", "HTC One X", "HTC Wildfire S", "HTC Sense", "1234", "iPhone 4S", "Samsung Galaxy Note 800", "Samsung Galaxy S3", "MacBook Air", "Mac Mini", "MacBook Pro"};
private ArrayList<String> array_sort = new ArrayList<String>();
int textlength = 0;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_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));
int x= lv.getHeaderViewsCount ();
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>
(MainActivity.this,
android.R.layout.simple_list_item_1, array_sort));
}
});
}
}
您可以这样搜索:
String text_to_search = editText.getText().toString();
for(String str : your_list) {
if(str.equals(text_to_search) {
//Do your operation
}
}
如果您需要索引,则可以使用传统的for循环。您可以这样搜索:
String text_to_search = editText.getText().toString();
for(String str : your_list) {
if(str.equals(text_to_search) {
//Do your operation
}
}
如果需要索引,则可以使用传统的for循环。在适配器中使用此代码
public void filter(String charText) {
charText = charText.toLowerCase();
picList.clear();
if (charText.length() == 0) {
picList.addAll(listpicOrigin);
} else {
for (Picture pic : listpicOrigin) {
if (pic.getPicName().toLowerCase().contains(charText)) {
picList.add(pic);
}
}
}
notifyDataSetChanged();
}
更多详细信息,请参考在适配器中使用此代码
public void filter(String charText) {
charText = charText.toLowerCase();
picList.clear();
if (charText.length() == 0) {
picList.addAll(listpicOrigin);
} else {
for (Picture pic : listpicOrigin) {
if (pic.getPicName().toLowerCase().contains(charText)) {
picList.add(pic);
}
}
}
notifyDataSetChanged();
}
更多详情,您可以参考Thanx很多。它是有效的…但有一个问题我的listview包含011德里德里…如果我写德里,它就不会显示wt来做这件事?要搜索德里,你必须键入011 D。它匹配011,然后空格搜索以D开头的任何东西。但是如果我想找到新德里的代码,那该怎么办呢?写你自己的搜索标准修改for循环内部的if条件。您可以使用正则表达式和模式匹配来匹配字符串的一部分。您希望您的搜索条件匹配代码011还是城市名称德里?Thanx很多。它是有效的…但有一个问题我的listview包含011德里德里…如果我写德里,它就不会显示wt来做这件事?要搜索德里,你必须键入011 D。它匹配011,然后空格搜索以D开头的任何东西。但是如果我想找到新德里的代码,那该怎么办呢?写你自己的搜索标准修改for循环内部的if条件。可以使用正则表达式和模式匹配来匹配字符串的一部分。是否希望搜索条件与代码011或城市名称匹配?string.xml中的数组是否有限制?string.xml中的数组是否有限制?