下面的代码列出了字符串[]练习中的项目。您也可以通过搜索过滤它。
问题是,我有两个需要过滤列表中项目的微调器。所以我的问题是,如何在以后的列表中添加/删除字符串?
public class CalExercises extends Activity {
private ListView myListView;
ArrayAdapter<String> myAdapter;
EditText inputSearch;
ArrayList<HashMap<String, String>> productList;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calexercise);
String exercises[] = { "Ola", "dolla" };
myListView = (ListView) findViewById(R.id.exp_list);
inputSearch = (EditText) findViewById(R.id.itemSearch);
myAdapter = new ArrayAdapter<String>(this, R.layout.parent_layout, R.id.parent_txt, exercises);
myListView.setAdapter(myAdapter);
inputSearch.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
CalExercises.this.myAdapter.getFilter().filter(cs);
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
}
@Override
public void afterTextChanged(Editable arg0) {
}
});
final Spinner spindif = (Spinner) findViewById((R.id.filterDif));
final Spinner spinmus = (Spinner) findViewById((R.id.filterMus));
final String diffilter = String.valueOf(spindif.getSelectedItem());
final String musfilter = String.valueOf(spinmus.getSelectedItem());
if (diffilter.equals("All Exercises")) {
}
}
}
答案 0 :(得分:0)
如果您需要创建一个仅包含与字符串匹配的项目的数组,请创建一个新数组并将其分配给练习:
exercises = filterArray(exercises, filter);
myAdapter.notifyDataSetChanged();
String[] filterArray(String[] arr, String filter) {
ArrayList<String> newArray = new ArrayList<String>();
for (int i=0;i<arr.length()<i++) {
if (arr[i].contains(filter) {
newArray.add(arr[i]);
}
}
return newArray.toArray();
}
如果你需要添加到数组......
exercises = addToArray(exercises, newString);
myAdapter.notifyDataSetChanged();
String[] addToArray(String[] arr, String newString) {
ArrayList<String> newArray = new ArrayList<String>(arr);
arr.add(newString);
return newArray.toArray();
}
如果您需要从阵列中删除......
exercises = removeFromArray(exercises, newString);
myAdapter.notifyDataSetChanged();
String[] removeFromArray(String[] arr, String removeMe) {
ArrayList<String> newArray = new ArrayList<String>(arr);
for (String item: newArray) {
if (item.equals(removeMe) {
newArray.remove(item);
}
}
return newArray.toArray();
}