Currently, I'm still a college student and struggling a little with this lab project. Actually, I finished this lab, but I wanted to challenge myself a bit and did extra stuff in my project. I have JSON file like this:
{
"human":
[
{"name":"Richard",
"age":"16",
"job":"student",
"height":"160cm"
},
{"name":"Cindy",
"age":"17",
"job":"student",
"height":"150cm"
},
{"name":"Yuan",
"age":"20",
"job":"teacher",
"height":"180cm"
},
{"name":"Kathy",
"age":"18",
"job":"student",
"height":"175cm"
},
{"name":"Lee",
"age":"23",
"job":"teacher",
"height":"165cm"
},
In my XML code, I have lay out like this:
Spinner job
Spinner name
Textview txtview
Textview dispInfo
This is how I tried to get my spinner only have job as teacher and student, however, it does not work...
JSONObject myJSON_object = new JSONObject(myText);
//the main JSON object is/includes an array
//extract that array
final JSONArray myJSON_array = myJSON_object.getJSONArray("human");
//temporarily array for Nettle Spinner
String[] temp = new String[myJSON_array.length()];
for (int i=0; i < myJSON_array.length(); i++){
try{
//get an individual element of the JSON array
JSONObject aJSON_element = myJSON_array.getJSONObject(i);
//get the individual properties of the JSON element
String jsJob = aJSON_element.getString("job");
temp[i] = jsJob;
}
catch (JSONException e)
{
Toast.makeText(getBaseContext(), "Dude, you have to know what the JSON looks like to parse it", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
//actually how the array gonna be in
String[] forJob = null;
for (int i=0; i < temp.length; i++){
boolean duplicate = false;
int b = 0;
while (b < i){
if (temp[i] == temp[b]) {
duplicate = true;
b++;
}
}
if (duplicate == false) {
forNettle[i] = temp[i];
}
}
//associate the full name with the listView
Spinner spJob = (Spinner) findViewById(R.id.spNettle);
Spinner spName = (Spinner) findViewById(R.id.spCate);
ArrayAdapter<String> arrJob = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, forJob);
arrJob.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // The drop down view
spJob.setAdapter(arrJob);