I have 2 Spinners and 2 respective .json
files. The first Spinner
holds the category, the second the subcategory. I need to load the respective data when the specific category is chosen.
Category JSON
file:
{
"Categories": {
"Category": [{
"cat_id": 1,
"cat_title": "..."
}, {
"cat_id": 2,
"cat_title": "..."
}, {
"cat_id": 3,
"cat_title": "..."
}]
}
}
Subcategory JSON
file:
{
"Subcategories": {
"Subcategory": [{
"cat_id": 1,
"subcat_id": 1,
"subcat_title": ".."
}]
}
}
They are linked on the cat id. The loading is controlled by identical methods.
Here is the method for the category:
private void prepareCats() {
inputStream = getResources().openRawResource(R.raw.categories);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
try {
ctr = inputStream.read();
while (ctr != -1) {
byteArrayOutputStream.write(ctr);
ctr = inputStream.read();
}
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
// Parse the data into jsonobject to get original data in form of json.
JSONObject jObject = new JSONObject(
byteArrayOutputStream.toString());
JSONObject jObjectResult = jObject.getJSONObject("Categories");
JSONArray jArray = jObjectResult.getJSONArray("Category");
String cat_title = "";
for(int i=0;i<jArray.length();i++){
cat.setCatname(jArray.getJSONObject(i).getString("cat_title"));
cat.setCatId(jArray.getJSONObject(i).getString("cat_id"));
Log.v("cat",cat.getCatname());
categories.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
System.out.println("cat position" + position);
catPosition = position;
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
categoriesList.add(cat.getCatname());
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout
.simple_spinner_item, categoriesList);
categories.setAdapter(
new NothingSelectedSpinnerAdapter(
dataAdapter,
R.layout.contact_spinner_cat_row_nothing_selected,
this));
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
}
} catch (Exception e) {
e.printStackTrace();
}
}
And for loading subcategory:
private void prepareSubCats() {
inputStream = getResources().openRawResource(R.raw.subcategories);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
try {
ctr = inputStream.read();
while (ctr != -1) {
byteArrayOutputStream.write(ctr);
ctr = inputStream.read();
}
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
// Parse the data into jsonobject to get original data in form of json.
JSONObject jObject = new JSONObject(
byteArrayOutputStream.toString());
JSONObject jObjectResult = jObject.getJSONObject("Subcategories");
JSONArray jArray = jObjectResult.getJSONArray("Subcategory");
String subcat_title = "";
for(int i=0;i<jArray.length();i++){
subcat.setSubCatname(jArray.getJSONObject(i).getString("subcat_title"));
subcat.setCatId(jArray.getJSONObject(i).getString("cat_id"));
subcategoriesList.add(subcat.getSubCatname());
subCategories.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
subcatPosition = position;
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout
.simple_spinner_item, subcategoriesList);
subCategories.setAdapter(
new NothingSelectedSpinnerAdapter(
dataAdapter,
R.layout.contact_spinner_subcat_row_nothing_selected,
this));
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
}
} catch (Exception e) {
e.printStackTrace();
}
}
What I need is to compare the category's cat id and the subcategory's cat id, so that the subcategories to be filled up to the according category.