This is from the Main Activity class. I have an arraylist of arrays and need to create a search bar for the listview.
这来自Main Activity类。我有一个数组的arraylist,需要为listview创建一个搜索栏。
adapter = new ArrayAdapter<String[]>(this, R.layout.list_view, android.R.id.text1, spellList)
{
public View getView(int position, View convertView, ViewGroup parent)
{
View view = super.getView(position, convertView, parent);
String[] entry = spellList.get(position);
TextView text1 = (TextView) view.findViewById(android.R.id.text1);
TextView text2 = (TextView) view.findViewById(android.R.id.text2);
text1.setText(entry[0]);
text2.setText(entry[1]);
return view;
}
};
wizList.setAdapter(adapter);
searchText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
This is the data in an Arraylist of string arrays. Prefered if searched by the spell name.
这是字符串数组的Arraylist中的数据。如果通过拼写名称搜索,则首选。
final ArrayList<String[]> spellList;
public WizardDatabase()
{
spellList = new ArrayList<>();
spellList.add(new String[] { "Resistance", "Subject gains +1 on saving throws."});
spellList.add(new String[] { "Acid Splash", "Orb deals 1d3 acid damage."});
spellList.add(new String[] { "Detech Poison", "Detects poison in one creature or small object."});
public ArrayList<String[]> getList()
{
return spellList;
}
thanks.
1 个解决方案
#1
0
If it's simple String search only then use the following code
如果只是简单的字符串搜索,则使用以下代码
searchText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
if (s.length() > 3) { // it's for performance I've put 3 you can change it
adapter.getFilter().filter(s.toString());
}
}
});
#1
0
If it's simple String search only then use the following code
如果只是简单的字符串搜索,则使用以下代码
searchText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
if (s.length() > 3) { // it's for performance I've put 3 you can change it
adapter.getFilter().filter(s.toString());
}
}
});