我有2個Spinners和2個.json
文件。第一個Spinner
擁有類別,第二個子類別。我需要在選擇特定類別時加載相應的數據。如何在Spinner中加載相應的JSON?
類別JSON
文件:
{
"Categories": {
"Category": [{
"cat_id": 1,
"cat_title": "..."
}, {
"cat_id": 2,
"cat_title": "..."
}, {
"cat_id": 3,
"cat_title": "..."
}]
}
}
子目錄JSON
文件:
{
"Subcategories": {
"Subcategory": [{
"cat_id": 1,
"subcat_id": 1,
"subcat_title": ".."
}]
}
}
他們在貓ID鏈接。加載由相同的方法控制。
這裏是該類別的方法:
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();
}
}
以及加載子類別:
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();
}
}
我需要的是比較類別的類別ID和子類別的類別ID,從而使子類別將被填充到相應的類別。
什麼是你的問題面對? –
我想根據所選擇的類別更新子類別列表(如果cat1,只有具有cat1屬性的子目錄) – user6456773
'prepareSubCats'方法中已經有一個邏輯(註釋部分)。那有什麼問題? –