1

I am trying to load spinner from json data using following code:

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ArrayList<String> items = getCountries("data.json");

    Spinner spinner = (Spinner)findViewById(R.id.spinnerStandard);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.second_layout,R.id.txtStandard,items);
    spinner.setAdapter(adapter);
}

private ArrayList<String> getCountries(String fileName){
    JSONArray jsonArray = null;
    ArrayList<String> cList = new ArrayList<String>();
    try {
        InputStream is = getResources().getAssets().open(fileName);
        int size = is.available();
        byte[] data = new byte[size];
        is.read(data);
        is.close();
        String json = new String(data, "UTF-8");
        jsonArray=new JSONArray(json);
        if (jsonArray != null) {
            for (int i = 0; i < jsonArray.length(); i++) {
                cList.add(jsonArray.getJSONObject(i).getString("standard"));
            }
        }
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
    catch (JSONException je)
    {
        je.printStackTrace();
    }
    return cList;
}

My json data is,

[
{
    "name":"Manoj",
    "surname":"Jadhav",
    "age":"18",
    "div":"A",
    "standard":"7"
},
{
    "name":"Nilesh",
    "surname":"Patankar",
    "age":"17",
    "div":"B",
    "standard":"7"
},
{
    "name":"Gourav",
    "surname":"Patil",
    "age":"18",
    "div":"A",
    "standard":"8"
},
{
    "name":"Pushkar",
    "surname":"Ganpule",
    "age":"17",
    "div":"A",
    "standard":"7"
},
{
    "name":"Prashant",
    "surname":"Raut",
    "age":"18",
    "div":"A",
    "standard":"8"
},
{
    "name":"Nachiket",
    "surname":"Salvi",
    "age":"17",
    "div":"A",
    "standard":"7"
},
{
    "name":"Hiren",
    "surname":"Lakhmani",
    "age":"18",
    "div":"B",
    "standard":"6"
}

]

In json data I am having duplicate standard values as I want to load only unique values from json into spinner (like there are standard values are 7, 7, 8, 7, 8, 7, 6 and I want to show only 6, 7, 8 out of those duplicate values.)

I've tried googling for all the options and as many other variations as I can think of and can't find any useful results, so I'm obviously missing something.

What is correct way to remove duplicate values ?

Barry Michael Doyle
  • 9,333
  • 30
  • 83
  • 143
neeta
  • 43
  • 12
  • Easy way is you have to check in cList weather Standard is already there in it if yes then no need to add otherwise add. I think if(!cList.contains(Standard)) this works – Zaki Pathan Feb 15 '17 at 10:27

6 Answers6

1

write this code above return cList in your method

HashSet<String> hashSet = new HashSet<String>();
    hashSet.addAll(cList);
    cList.clear();
    cList.addAll(hashSet);

return cList;

or

for (int i = 0; i < jsonArray.length(); i++) {
if(!Clist.contains(jsonArray.getJSONObject(i).getString("standard"))){
    cList.add();
}
}
Rajesh Satvara
  • 3,842
  • 2
  • 30
  • 50
0

From Array to HashSet

Now you have Unique elements. Transform it to Array again and use it in your adapter. Should do it. Hope this helps.

Community
  • 1
  • 1
T.Dimitrov
  • 157
  • 2
  • 7
0
if(cList.contains(jsonArray.getJSONObject(i).getString("standard"))){

}else{

   //Add value here..

   cList.add(jsonArray.getJSONObject(i).getString("standard"));

}
Rajkumar Kumawat
  • 290
  • 2
  • 11
  • contains method is case sensitive. Please have a look at it if you have a concern about case sensitivity – sanjay Feb 15 '17 at 10:52
0
for (int i = 0; i < jsonArray.length(); i++) {
    if(!cList.contains(jsonArray.getJSONObject(i).getString("standard"))){
        cList.add(jsonArray.getJSONObject(i).getString("standard"));
       }
   }
Zaki Pathan
  • 1,762
  • 1
  • 13
  • 26
0

update your iterator to

if (jsonArray != null) {
        for (int i = 0; i < jsonArray.length(); i++) {
            String standard = jsonArray.getJSONObject(i).getString("standard");
            if(!cList.contains(standard))
               cList.add(standard);
        }
    }
Ankush Bist
  • 1,862
  • 1
  • 15
  • 32
0

Use contains method if you don't have concern about case sensitivity

sanjay
  • 228
  • 2
  • 12