正如標題所解釋的,我在過濾/搜索列表視圖時遇到了問題。當我嘗試搜索讓我們說「A」時,listView顯示A-31兩次,A-32兩次等等。有人有一個想法,爲什麼發生這種情況?Android,ListView搜索/過濾 - >重複條目
package hpliu.grupp2;
import android.content.Intent;
import android.os.Bundle;
import android.os.Debug;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.TableRow;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.app.ListActivity;
import android.app.Activity;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
public class SearchRoomActivity extends ListActivity {
private EditText filterText = null;
SimpleAdapter menu = null;
ListView lv = null;
static final String[] AHalls = new String[] { "A-31", "A-32", "A-33",
"A-34", "A-35", "A-36", "A-37" };
String[] from = new String[] { "roomName", "buildingName" };
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search_room);
createCampusList("Campus Valla, Linköping",
createHashMap(AHalls, "A Huset"));
// filterText = (EditText) findViewById(R.id.search_box);
// filterText.addTextChangedListener(filterTextWatcher);
}
private TextWatcher filterTextWatcher = new TextWatcher() {
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
menu.getFilter().filter(s);
}
};
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
try {
@SuppressWarnings("unchecked")
HashMap<String, String> toHash = (HashMap<String, String>) this
.getListAdapter().getItem(position); // {text1 = "A-31",
// text2 = "Herp"};
Toast.makeText(
this,
"Searching for... " + " "
+ toHash.get("roomName").toString(),
Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
}
}
public List<HashMap<String, String>> createHashMap(String[] Halls,
String Building) {
List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();
for (int i = 0; i < Halls.length; i++) {
HashMap<String, String> map = new HashMap<String, String>();
map.put(from[0], Halls[i]);
map.put(from[1], Building);
fillMaps.add(map);
}
return fillMaps;
}
public List<HashMap<String, String>> createHashMap(String[] Halls,
String[] Buildings) {
List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();
for (int i = 0; i < Halls.length; i++) {
HashMap<String, String> map = new HashMap<String, String>();
map.put(from[0], Halls[i]);
map.put(from[1], Buildings[i]);
fillMaps.add(map);
}
return fillMaps;
}
public void createCampusList(String campusHeader,
List<HashMap<String, String>> fillMaps) {
int[] to = new int[] { R.id.roomName, R.id.buildingName };
menu = new SimpleAdapter(this, fillMaps, R.layout.list_item, from, to);
setListAdapter(menu);
lv = getListView();
lv.setTextFilterEnabled(true);
}
@Override
protected void onDestroy() {
super.onDestroy();
filterText.removeTextChangedListener(filterTextWatcher);
}
}