Android 当我为listview使用简单适配器时,如何在listview中搜索?
当我使用一个简单的适配器时,我正在listview中创建一个搜索栏。我正在使用下面的代码。执行此代码时,它显示空指针异常。有人能帮我吗?提前谢谢Android 当我为listview使用简单适配器时,如何在listview中搜索?,android,Android,当我使用一个简单的适配器时,我正在listview中创建一个搜索栏。我正在使用下面的代码。执行此代码时,它显示空指针异常。有人能帮我吗?提前谢谢 String headlines1[] = {"Dell Inspiron", "HTC One X", "HTC Wildfire S", "HTC Sense", "HTC Sensation XE","iPhone 4S", "Samsung Galaxy Note 800","Samsung Galaxy S3", "MacBook Air",
String headlines1[] = {"Dell Inspiron", "HTC One X", "HTC Wildfire S", "HTC Sense", "HTC Sensation XE","iPhone 4S", "Samsung Galaxy Note 800","Samsung Galaxy S3", "MacBook Air", "Mac Mini", "MacBook Pro"};
for (int j = 0; j < headlines1.length; j++)
{
HashMap<String, String> map = new HashMap<String, String>();
map.put("row1", ":"+headlines1[j]);
mylistData.add(map);
}
SimpleAdapter arrayAdapter = new SimpleAdapter(this, mylistData, R.layout.simple_list_item_2, row, new int[] { R.id.tv});
lst.setAdapter(arrayAdapter);
lst.setTextFilterEnabled(true);
inputsearch.addTextChangedListener(new TextWatcher()
{
@Override
public void onTextChanged( CharSequence cs, int arg1, int arg2, int arg3)
{
// TODO Auto-generated method stub
String searchString=inputsearch.getText().toString();
int textLength=searchString.length();
//clear the initial data set
searchResults.clear();
for(int i=0;i<mylistData.size();i++)
{
String playerName=mylistData.get(i).get("row1").toString();
if(textLength<=playerName.length()){
//compare the String in EditText with Names in the ArrayList
if(searchString.equalsIgnoreCase(playerName.substring(0,textLength)))
searchResults.add(mylistData.get(i));
}
}
arrayAdapter.notifyDataSetChanged();
}
字符串标题1[]={“Dell Inspiron”、“HTC One X”、“HTC Wildfire S”、“HTC Sense”、“HTC Sensation XE”、“iPhone 4S”、“三星Galaxy Note 800”、“三星Galaxy S3”、“MacBook Air”、“Mac Mini”、“MacBook Pro”};
对于(int j=0;j 对于(inti=0;i请查看下面的代码
public class MainActivity 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.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));
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));
}
});
}
}
公共类MainActivity扩展活动{
私有ListView lv;
私人编辑;
私有字符串listview_数组[]={“一”、“二”、“三”、“四”、“五”、“六”、“七”、“八”、“九”、“十”};
私有ArrayList数组_sort=新ArrayList();
int textlength=0;
创建时的公共void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_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