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();
}
I Solved the issue; May be you have a good imporovement.