我想實現一個搜索欄到我的ListView。 我使用SimpleAdapter,代碼如下:將過濾器應用於ListView的單個項目
adapter = new SimpleAdapter(this, menuItems,
R.layout.list_item,
new String[] { KEY_NAME, KEY_DESC, KEY_ADDRESS, KEY_LAT, KEY_LNG, KEY_IDLOC, KEY_DATE, KEY_IDPR}, new int[] {
R.id.name, R.id.desciption, R.id.cost, R.id.latitude, R.id.longitude, R.id.id, R.id.date, R.id.pr});
我會申請一個搜索欄只爲KEY_ADDRESS項目。我該怎麼做?
編輯:添加我的活動
ListAdapter adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab3main);
ParserTask pt=new ParserTask();
pt.execute();
ArrayList<HashMap<String, String>> menuItems;
try {
menuItems = pt.get();
adapter = new SimpleAdapter(this, menuItems,
R.layout.list_item,
new String[] { KEY_NAME, KEY_DESC, KEY_ADDRESS, KEY_LAT, KEY_LNG, KEY_IDLOC, KEY_DATE, KEY_IDPR}, new int[] {
R.id.name, R.id.desciption, R.id.cost, R.id.latitude, R.id.longitude, R.id.id, R.id.date, R.id.pr});
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
我嘗試:
final EditText myFilter = (EditText) findViewById(R.id.myFilter);
myFilter.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
String text = myFilter.getText().toString().toLowerCase(Locale.getDefault());
((SimpleAdapter) adapter).getFilter().filter(text);
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
@Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
// TODO Auto-generated method stub
}
但它返回一個NullPointerException。下面的logcat:
07-18 18:40:44.536: E/AndroidRuntime(274): FATAL EXCEPTION: main
07-18 18:40:44.536:E/AndroidRuntime(274):顯示java.lang.NullPointerException 07-18 18:40:44.536:E/AndroidRuntime(274):在機器人.widget.SimpleAdapter.getCount(SimpleAdapter.java:93) 07-18 18:40:44.536:E/AndroidRuntime(274):at android.widget.AdapterView.checkFocus(AdapterView.java:689) 07-18 18 :40:44.536:E/AndroidRuntime(274):at android.widget.AdapterView $ AdapterDataSetObserver.onInvalidated(AdapterView.java:813) 07-18 18:40:44.536:E/AndroidRuntime(274):at android.database .DataSetObservable.notifyInvalidated(DataSetObservable.java:43) 07-18 18:40:44.536:E/AndroidRuntime(274):at android.widget.BaseAdap ter.notifyDataSetInvalidated(BaseAdapter.java:54) 07-18 18:40:44.536:E/AndroidRuntime(274):at android.widget.SimpleAdapter $ SimpleFilter.publishResults(SimpleAdapter.java:383) 07-18 18: 40:44.536:E/AndroidRuntime(274):在android.widget.Filter $ ResultsHandler.handleMessage(Filter.java:282) 07-18 18:40:44.536:E/AndroidRuntime(274):在android.os。 Handler.dispatchMessage(Handler.java:99) 07-18 18:40:44.536:E/AndroidRuntime(274):at android.os.Looper.loop(Looper.java:123) 07-18 18:40: 44.536:E/AndroidRuntime(274):在android.app.ActivityThread.main(ActivityThread.java:4627) 07-18 18:40:44.536:E/AndroidRuntime(274):在java.lang.reflect.Method。 invokeNative(Native Method) 07-18 18:40:44.536:E/AndroidRuntime(274):at java.lang.reflect.Method.invoke(Method.java:521) 07-18 18:40:44.536:E/AndroidRuntime(274):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:868) 07-18 18:40:44.536:E/AndroidRuntime(274):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 07-18 18:40:44.536:E/AndroidRuntime(274):at dalvik.system.NativeStart.main(本地方法)
發表您的活動i'l幫助 –
@Rajajisubramanian我加入吧! – KappS