如标题所示,我在过滤/搜索列表视图时遇到问题。当我尝试搜索让我们说“A”时,listView显示A-31两次,A-32显示两次,依此类推。有谁知道为什么会这样?
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);
}
}
答案 0 :(得分:2)
如果过滤每个项目中包含两个字符串的适配器,则过滤器将通过适配器两次。这就是你有倍数的原因。如果你有三个字符串,你可能会得到三个重复的项目。
我在网上找到的一个解决方案是创建一个自定义SimpleAdapter并在填充ListView之前检查重复项。
答案 1 :(得分:0)
我的自定义列表视图面临同样的问题。
listview的超类不知道您在过滤后已更改了大小。这意味着getCount()将返回数组的原始大小,这可以理解地大于过滤后的数组。因此,您必须覆盖getCount()函数以返回已过滤数组的大小,而不是原始数组。之后,重复的项目消失了。
@Override
public int getCount() {
return filter_array.size();
}
@Override
public Object getItem(int position) {
return filter_array.get(position);
}