以下是截图
下面是部分代码:
et_search.addTextChangedListener(new TextWatcher(){
@Override
public void beforeTextChanged(CharSequence paramCharSequence,
int paramInt1, int paramInt2, int paramInt3) {
// TODO Auto-generated method stub
Log.d(TAG, "beforeTextChanged");
}
@Override
public void onTextChanged(CharSequence value,
int paramInt1, int paramInt2, int paramInt3) {
// TODO Auto-generated method stub
Log.d(TAG, "onTextChanged");
Log.w(TAG, "input.text=" + value.toString());
Message msg = new Message();
msg.what = MSG_KEY;
Bundle data = new Bundle();
data.putString("value", value.toString());
msg.setData(data);
handler.sendMessage(msg);
}
@Override
public void afterTextChanged(Editable paramEditable) {
// TODO Auto-generated method stub
Log.d(TAG, "afterTextChanged");
}
});
SimpleAdapter simpleAdapter = new SimpleAdapter(MainActivity.this, listItems, R.layout.simple_item, new String[]{"image","weather","desc"}, new int[]{R.id.image,R.id.weather,R.id.desc});
listView.setAdapter(simpleAdapter);
handler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MSG_KEY:
refreshListView(msg.getData().get("value").toString());
}
}
//刷新ListView
private void refreshListView(String value) {
if (value == null || value.trim().length() == 0){
//把listItems集合中的名字和布局中的相关信息一一对应
SimpleAdapter simpleAdapter = new SimpleAdapter(MainActivity.this, listItems, R.layout.simple_item, new String[]{"image","weather","desc"}, new int[]{R.id.image,R.id.weather,R.id.desc});
listView.setAdapter(simpleAdapter);
}
final List<HashMap<String,Object>> tmplistItems = new ArrayList<HashMap<String,Object>>();
for(int i=0;i<names.length;i++){
HashMap<String,Object> tmplistItem = new HashMap<String,Object>();
if(names[i].indexOf(value)>=0){
tmplistItem.put("image", imageIDs[i]);
tmplistItem.put("weather", names[i]);
tmplistItem.put("desc", desc[i]);
tmplistItems.add(tmplistItem);
}
}
SimpleAdapter simpleAdapter = new SimpleAdapter(MainActivity.this, tmplistItems, R.layout.simple_item, new String[]{"image","weather","desc"}, new int[]{R.id.image,R.id.weather,R.id.desc});
listView.setAdapter(simpleAdapter);
listView.invalidateViews();
}
};
下面是完整代码: