添加/从我的列表视图中删除的项目? [英] Adding/Removing items from my listview?
本文介绍了添加/从我的列表视图中删除的项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
下code使得从字符串[]演习项目清单。您也可以通过搜索过滤。
问题是,我有两个纺纱厂,这将需要过滤列表中的项目。所以我的问题是,我该如何添加/删除字符串从列表以后?
公共类CalExercises延伸活动{
私人的ListView myListView;
ArrayAdapter<串GT; myAdapter;
的EditText inputSearch;
ArrayList的<&HashMap的LT;字符串,字符串>> productList的;
@覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_calexercise);
字符串演习[] = {奥拉,DOLLA};
myListView =(ListView控件)findViewById(R.id.exp_list);
inputSearch =(EditText上)findViewById(R.id.itemSearch);
myAdapter =新ArrayAdapter<串GT;(这一点,R.layout.parent_layout,R.id.parent_txt,练习);
myListView.setAdapter(myAdapter);
inputSearch.addTextChangedListener(新TextWatcher(){
@覆盖
公共无效onTextChanged(CharSequence的CS,INT ARG1,ARG2 INT,INT ARG3){
。CalExercises.this.myAdapter.getFilter()过滤器(CS);
}
@覆盖
公共无效beforeTextChanged(CharSequence的为arg0,ARG1 INT,INT ARG2,
INT ARG3){
}
@覆盖
公共无效afterTextChanged(编辑为arg0){
}
}); 最后的微调spindif =(微调)findViewById((R.id.filterDif));
最后的微调spinmus =(微调)findViewById((R.id.filterMus));
最后弦乐diffilter =将String.valueOf(spindif.getSelectedItem());
最后弦乐musfilter =将String.valueOf(spinmus.getSelectedItem()); 如果(diffilter.equals(所有练习)){ }
} }
解决方案
如果你需要创建一个只包含的项目匹配的字符串数组,创建一个新的数组并把它分配给练习:
=行使filterArray(练习,过滤器);
myAdapter.notifyDataSetChanged();的String [] filterArray(字符串[]编曲,弦乐过滤器){
ArrayList的<串GT; newArray =新的ArrayList<串GT;();
的for(int i = 0; I< arr.length()<我++){
如果(ARR [I]。载有(过滤器){
(ARR [I])newArray.add;
}
}
返回newArray.toArray();
}
如果您需要添加到阵列...
=行使addToArray(练习,newString);
myAdapter.notifyDataSetChanged();的String [] addToArray(字符串[]编曲,弦乐newString){
ArrayList的<串GT; newArray =新的ArrayList<串GT;(ARR);
arr.add(newString);
返回newArray.toArray();
}
如果您需要从数组中删除...
练习= removeFromArray(练习,newString);
myAdapter.notifyDataSetChanged();的String [] removeFromArray(字符串[]编曲,弦乐removeMe){
ArrayList的<串GT; newArray =新的ArrayList<串GT;(ARR);
对于(字符串项:newArray){
如果(item.equals(removeMe){
newArray.remove(项目);
}
}
返回newArray.toArray();
}
The code below makes a list with the items from the string[] exercises. You can also filter it by searching.
The problem is, I have two spinners that will need to filter the items in the list. So my question is, how do I add/remove strings from the list later on?
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")) {
}
}
}
解决方案
If you need to create an array that only contains items matching a string, create a new array and assign it to exercises:
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();
}
If you need to add to the array...
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();
}
If you need to remove from the array...
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();
}
这篇关于添加/从我的列表视图中删除的项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文