ListView控件过滤后,显示每个项目两次 [英] ListView displays each item twice after filtering
问题描述
我有一个列表,每行包括主要项目和子项目的。我使用的是过滤器,以刷新列表为用户键入搜索的EditText。
上输入列表中的项目就根据输入的字母得到持续显示,但是每个项目被显示两次。
下面是我的code:
I have a list, each row consisting of a main item and subitem. I am using a filter to refresh the list as the user types into a search edittext. On typing the items in the list do get displyed according to the typed alphabets, however each item is displayed twice. Below is my code:
public class NewReqFragment extends ListFragment
{
ListView newReqList;
LayoutInflater inflater;
String[] from = new String[] {"mainrow", "subrow"};
EditText searchBar = null;
SimpleAdapter sAdapter =null;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
newReqList = this.getListView();
searchBar = (EditText)this.getActivity().findViewById(R.id.searchbar);
List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();
for(int i = 0; i < ListItemStrings.NEWREQTITLES.length; i++)
{
HashMap<String, String> map = new HashMap<String, String>();
map.put("mainrow",ListItemStrings.NEWREQTITLES[i]);
map.put("subrow",ListItemStrings.NEWREQCHILDLIST[i]);
fillMaps.add(map);
}
sAdapter = new SimpleAdapter (this.getActivity(), fillMaps
, R.layout.simple_list_item_checkable_1,
from, new int[] {R.id.text1, R.id.text2});
newReqList.setAdapter(sAdapter);
searchBar.addTextChangedListener(filterTextWatcher);
}
private TextWatcher filterTextWatcher = new TextWatcher()
{
public void afterTextChanged(Editable s)
{
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after)
{
}
public void onTextChanged(CharSequence s, int start, int before,
int count)
{
sAdapter.getFilter().filter(s);
sAdapter.notifyDataSetChanged();
}
};
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View v = inflater.inflate(R.layout.newreqscrlayout, container,false);
return v;
}
}
任何人都可以请帮我找出什么是错的?
Can anyone please help me to find out what is wrong?
推荐答案
我解决它通过一个定制SimpleAdapter。结果
我的名单由一个项目和分项目。该SimpleAdapter方法performFiltering()也过滤子项文本,从而增加每个项目twice.Making循环只执行一次得到的结果。
I solved it by making a custom SimpleAdapter.
My list consists of a Item and Subitem. The SimpleAdapter method performFiltering() was filtering the subitem text also and hence adding each item twice.Making the loop execute only once got the result.
这篇关于ListView控件过滤后,显示每个项目两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!