私の断片にスピナーがあります。このスピナーのデータは、私のデータベースのJSONによって取り込まれています。私が持っている問題は、常にスピナーで同じデータを繰り返すということです。私はタブを切り替えると、これは私のスピナーに起こる戻るたびAndroidスピナーが同じ値を追加し続ける
バスケットボール 野球 サッカー
:例
。
バスケットボール 野球 サッカー バスケットボール 野球 サッカー
ここに私のスピナー
public class Games extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.games, container, false);
spinner1 = (Spinner) rootView.findViewById(R.id.sp1);
getSpinner();
return rootView;
}
private void getSpinner(){
try
{
URL url = new URL (Config.URL_SPIN);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
is = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
StringBuilder sb = new StringBuilder();
while((line = bufferedReader.readLine()) != null)
{
sb.append(line + "\n");
}
is.close();
result = sb.toString();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try
{
JSONArray JA = new JSONArray(result);
JSONObject json;
e_name = new String[JA.length()];
e_gender = new String[JA.length()];
for(int i = 0; i<JA.length(); i++)
{
json = JA.getJSONObject(i);
e_gender[i] = json.getString("e_gender");
e_name[i] = json.getString("e_name");
}
list1.add("All");
list1.add("NonSports");
for(int i = 0; i<e_name.length; i++)
{
list1.add(e_name[i] + " "+e_gender[i]);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
あなたはAndroでRetrofitまたは少なくともOkHttpを使用することを検討したいと思うかもしれませんIDプロジェクトこれは、 'HttpURLConnection'を使用し、jsonを手作業で解析するよりもはるかに簡単で最適化されています。 – Vucko