How to load items of a category from database in a depandent spinner

587 views Asked by At

I created a List of Categories.Then I saved the names as a strings in an adapter. what I wanted to do is if I select a category from spinner1 I want the spinner2 to show items of category1 for example. the categories and items are two tables from database.

table category
-------------------------
     id      |   name   |                           
-------------------------

table item
-----------------------------
  id   |  name | category_id |
-----------------------------

I use ActiveAndroid as an ORM. https://github.com/pardom/ActiveAndroid

        final Spinner dropdown = (Spinner)rootView.findViewById(R.id.spinner1);
        final Spinner dropdown2 = (Spinner)rootView.findViewById(R.id.spinner2);

        //Display single choice category
        final Categories categories = null;
        final List<Categories> AllCategories ;
        AllCategories =  (List<Categories>) getAll(categories);

        final ArrayList<String> list = new ArrayList<String>();
        for (int i=0;i<AllCategories.size();i++) {
            String val=AllCategories.get(i).getName().toString();
            list.add(new String(val));
        }
        ArrayAdapter arrayAdapter;
        arrayAdapter = new ArrayAdapter(getActivity(),android.R.layout.simple_spinner_item,list);
        arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        dropdown.setAdapter(arrayAdapter);
        dropdown.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {

                String category = adapterView.getItemAtPosition(i).toString();
                Toast.makeText(getActivity(),"Selected Category : " + category, Toast.LENGTH_LONG).show();


            }

final Spinner dropdown = (Spinner)rootView.findViewById(R.id.spinner1);
        final Spinner dropdown2 = (Spinner)rootView.findViewById(R.id.spinner2);

        //Display single choice category


        final Categories categories = null;
        final List<Categories> AllCategories ;
        AllCategories =  (List<Categories>) getAll(categories);
        final ArrayList<String> list = new ArrayList<String>();
        for (int i=0;i<AllCategories.size();i++) {
            String val=AllCategories.get(i).getName().toString();
            list.add(new String(val));
        }
        ArrayAdapter arrayAdapter;
        arrayAdapter = new ArrayAdapter(getActivity(),android.R.layout.simple_spinner_item,list);
        arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        dropdown.setAdapter(arrayAdapter);

        dropdown.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {

                String category = adapterView.getItemAtPosition(i).toString();
                Toast.makeText(getActivity(),"Selected Category : " + category, Toast.LENGTH_LONG).show();


            }


            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });

        public static List<Categories> getAll(Categories category){
            return new Select()
                    .from(Categories.class)
                    .orderBy("name ASC")
                    .execute();
        }

        /*return Items ordred by name

         */

        public static List<Item> getAll(Item item){
            return new Select()
                    .from(Item.class)
                    .where("item=?",item.getId())
                    .execute();
        }

        public static List<Item> getAllItemsInCategory(Categories category){
            return new Select()
                    .from(Item.class)
                    .where("Categories = ?", category.getId())
                    .orderBy("Name ASC")
                    .execute();
        }
1

There are 1 answers

0
TheGreenGoblen On BEST ANSWER

I Solved the issue; May be you have a good imporovement.

    final Categories categories = null;
    final List<Categories> AllCategories ;
    AllCategories =  (List<Categories>) getAll(categories);

    final ArrayList<String> list = new ArrayList<String>();
    for (int i=0;i<AllCategories.size();i++) {
        String val=AllCategories.get(i).getName().toString();
        list.add(new String(val));
    }
    final ArrayAdapter arrayAdapter;
    arrayAdapter = new ArrayAdapter(getActivity(),android.R.layout.simple_spinner_item,list);
    arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    dropdown.setAdapter(arrayAdapter);

    dropdown.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {

            String category = adapterView.getItemAtPosition(i).toString();
            String name = new String(category);
            Categories cat = new Categories();
            cat.setName(name);
            Categories cat1 = getCategoryByName(new String(cat.getName()));



            Toast.makeText(getActivity(),"Selected Category : " + category, Toast.LENGTH_LONG).show();


            List<Item> listOfItems = getAllItemsInCategoryById(cat1);


            final ArrayList<String> listItemString = new ArrayList<String>();

            for(int j=0;j<listOfItems.size();j++){

                String val = listOfItems.get(j).getName().toString();

                System.out.println(val);

                listItemString.add(new String(val));

            }

            final ArrayAdapter arrayAdapter1;
            arrayAdapter1 = new ArrayAdapter(getActivity(),android.R.layout.simple_spinner_item,listItemString);
            arrayAdapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            dropdown2.setAdapter(arrayAdapter1);


        }



        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {

        }
    });