2017-02-15 23 views
1

から重複した値の削除:私のJSONデータは、私は次のコードを使用してJSONデータからスピナーをロードしようとしていますスピナー

私はJSONデータで
[ 
{ 
    "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" 
} 

]

ある

@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; 
} 

をjsonからユニークな値だけをスピナーにロードしたいときに重複した標準値を持っています(標準値は7,7,8,7,8,7,6あり、私はそれらのうち6,7,8を表示したい重複値)

私はすべてのオプションや他の多くのバリエーションについて考えてみましたが、有用な結果が見つからないので、私は明らかに何かが欠けています。

重複する値を削除する正しい方法は何ですか?

+0

簡単な方法では、cList weatherをチェックする必要があります。私は(!cList.contains(Standard))が動作するかどうかは、 –

答えて

1

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); 
     } 
    } 
にあなたのイテレータを更新
+0

あなたの提案をありがとう..それは動作します – neeta

+0

これは他の人にも役立つことができますとしてマークしてください。 –

+0

には、大文字と小文字が区別されます。大文字と小文字の区別について懸念がある場合は、それを見てください – sanjay

0

From Array to HashSet

今、あなたはユニークな要素を持っています。再びArrayに変換し、アダプタで使用します。それをしなければならない。お役に立てれば。書き込みこのコードをあなたの方法

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

return cList; 

または

for (int i = 0; i < jsonArray.length(); i++) { 
if(!Clist.contains(jsonArray.getJSONObject(i).getString("standard"))){ 
    cList.add(); 
} 
} 
0
if(cList.contains(jsonArray.getJSONObject(i).getString("standard"))){ 

}else{ 

    //Add value here.. 

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

} 
+0

には大文字と小文字が区別されます。あなたが大文字と小文字の区別について心配している場合はそれを見てください – sanjay

0
for (int i = 0; i < jsonArray.length(); i++) { 
    if(!cList.contains(jsonArray.getJSONObject(i).getString("standard"))){ 
     cList.add(jsonArray.getJSONObject(i).getString("standard")); 
     } 
    } 
0

戻りCLIST上記

0

大文字と小文字の区別について心配していない場合は、メソッドを使用してください。

関連する問題