0

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")) {

            }


        }

    }
Calis
  • 47
  • 1
  • 11
  • http://stackoverflow.com/questions/13412341/how-to-add-remove-item-from-listview-in-android-when-click-button-in-item-listvi – em_ Sep 16 '15 at 20:57

1 Answers1

0

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();
}
Cory Roy
  • 5,379
  • 2
  • 28
  • 47
  • The thing is, productList is unused. It's from the tutorial and he references it nowhere. It actually uses 'exercises[]' for the list which got me confused. Any ideas? – Calis Sep 16 '15 at 21:05
  • Well I'd hope so but I've been stuck on this for hours so I won't get my hopes up! Thanks! – Calis Sep 16 '15 at 21:11
  • you may need cast or use the other toArray(..) method – Cory Roy Sep 16 '15 at 21:24
  • Thanks. Could you make 1 add, and 1 remove string to/from arraylist ? Cause this seems like Chinese.! – Calis Sep 16 '15 at 21:28
  • What does this even do? It looks nothng like I thought it look like – Calis Sep 16 '15 at 21:48
  • I was looking for somethng after this, like, add substring to stringarray, remove substring from stringarray. if (diffilter.equals("Beginner") && (musfilter.equals("All Exercises"))) { } – Calis Sep 16 '15 at 21:51
  • This will create smaller arrays than the original that contain only strings matching what is in the "filter" variable. I thought you were filtering spinners and that this would help you. – Cory Roy Sep 16 '15 at 22:01
  • Well yea filter sounds right, but more like filtering the listview by spinners were ill just name the strings it has to show or not show per spinner option. Any idea on that? – Calis Sep 16 '15 at 22:29
  • Seeing I already have the detection for spinnerfiltering by having if 'diffilter equals beginner... etc.. as the last line of the current code. All I need a way to tell the code what strings to show – Calis Sep 16 '15 at 22:30