1

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);
2
  • 1
    What is your error? Commented Oct 3, 2017 at 20:49
  • I was not so sure, but I'm sure my error was somewhere in the looping. Since I try not to have an array that has {student, student, teacher, student, teacher}. But when I run it, it run perfectly fine, but spiner is empty Commented Oct 3, 2017 at 22:10

2 Answers 2

1

Kind of the way to remove duplicates:

int lastIdx = 0;
String[] result = new String[temp.lenght];
mainLoop: for(int i =0; i < temp.length; i++) {
   for(int j = 0; j < lastIdx; j++)
      if(result[j].equals(temp[i])) continue mainLoop;
   result[lastIdx++] = temp[i];
}

then your unique list will be the array result positions from 0 to lastIdx (and the rest will be filled with null;

Sign up to request clarification or add additional context in comments.

Comments

0

The easiest way to remove duplicates from a list is to convert it to a Set and back. Sets inherently have no duplicates, so you have to do virtually nothing.

Set<String> tempSet = new HashSet()<> (Arrays.asList(temp));

String[] no_duplicates = tempSet.toArray(new String[tempSet.size()]);

Or as a sweet, succinct one-liner:

String[] no_duplicates = new HashSet<String>(Arrays.asList(temp)).toArray(new String[temp.length]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.