如何从依赖的微调器中的数据库加载类别的项目

时间:2014-11-16 13:19:38

标签: java android sqlite activeandroid

我创建了一个类别列表。然后我将名称保存为适配器中的字符串。 我想要做的是,如果我从spinner1中选择一个类别,我希望spinner2显示category1的项目。 类别和项目是数据库中的两个表。

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

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

我使用ActiveAndroid作为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 个答案:

答案 0 :(得分:0)

我解决了这个问题;可能你有一个很好的改进。

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

        }
    });