2017-02-26 12 views
0

私は国名がスピナーを持っています。以下は国spinner.hereに応じてフラグを表示しています。スピナーの選択に応じてフラグを表示したいのですが、それをgridViewで表示できますか?どのようにgridviewでスピナーの選択を表示するには?

public class FanciersFragment extends Fragment { 

    private View rootView; 
    private Spinner spFindCountry; 
    private RecyclerView rvFanciers; 
    private ArrayList<CountryDetail> countryDetailArray; 
    private LinearLayout llContainer; 
    private String countryCode; 

    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 

     rootView = inflater.inflate(R.layout.fragment_fanciers, container, false); 

     iitializeView(); 

     return rootView; 
    } 

    private void iitializeView() { 

     spFindCountry = (Spinner) rootView.findViewById(R.id.sp_country_find); 
     rvFanciers = (RecyclerView) rootView.findViewById(R.id.rv_fanc_items); 
     llContainer = (LinearLayout) rootView.findViewById(R.id.ll_fanciers_container); 

     GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity() , 3); 
     rvFanciers.setHasFixedSize(true); 
     rvFanciers.setLayoutManager(gridLayoutManager); 

     getCounties(); 

    } 


    @Override 
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { 
     super.onViewCreated(view, savedInstanceState); 

     getActivity().setTitle("FanciersResponse"); 
    } 

    private void getCounties() { 

     if(Utility.isNetworkConnected(getActivity())) { 

      final Call<Countries> resp = Utility.getApiService().getContries(); 

      resp.enqueue(new Callback<Countries>() { 
       @Override 
       public void onResponse(Call<Countries> call, Response<Countries> response) { 

        if (response.code() == 200) { 

         Countries countries = (Countries) response.body(); 

         if (countries != null) 
          if (countries.getStatus() != null) 
           if (countries.getStatus().equals("OK")) { 

            countryDetailArray = (ArrayList<CountryDetail>) countries.getCountryDetails(); 
            if(countryDetailArray != null) 
             populateCountrySpinner(countryDetailArray); 
             pupulateFanciers(); 

           } else { 

            Utility.ShowSnackBar(countries.getStatusMessage() , llContainer); 
           } 
        } else { 

         Utility.ShowSnackBar("Network Error" , llContainer); 
        } 
       } 

       @Override 
       public void onFailure(Call<Countries> call, Throwable t) { 

        Utility.ShowSnackBar("No Response" , llContainer); 

       } 
      }); 

     } 
     else { 
      Utility.ShowSnackBar(getResources().getString(R.string.no_internet), llContainer); 

     } 
    } 

    private void pupulateFanciers() { 

     Fanciers rcAdapter = new Fanciers(getActivity(), countryDetailArray , new Fanciers.OnItemClickListener() { 
      @Override 
      public void onItemClick(CountryDetail item) { 

       if(item != null){ 

        Bundle b = new Bundle(); 
        b.putSerializable(Constants.COUNTRY_DETAILS , item); 
        Intent intent = new Intent(getActivity() , CountryBaseFanciers.class); 
        intent.putExtra(Constants.BUNDLE , b); 
        startActivity(intent); 

       } 
      } 
     }); 
     rvFanciers.setAdapter(rcAdapter); 
    } 

    private void populateCountrySpinner(final ArrayList<CountryDetail> countryDetailList) { 

     ArrayList<String> countryList = new ArrayList<>(); 

     if(countryDetailList != null){ 


      for(CountryDetail countryDetail: countryDetailList){ 

       countryList.add(countryDetail.getCountryName()); 
      } 

     } 
     /*adapterCountry = new ArrayAdapter<String>(getActivity(), R.layout.spinner_layout, R.id.tv_country_name, countryList); 
     spCountry.setAdapter(adapterCountry);*/ 

     HintSpinner<String> hintSpinner = new HintSpinner<>(
       spFindCountry, 
       // Default layout - You don't need to pass in any layout id, just your hint text and 
       // your list data 
       new HintAdapter<>(getActivity(), getResources().getString(R.string.find_country), countryList), 
       new HintSpinner.Callback<String>() { 
        @Override 
        public void onItemSelected(int position, String itemAtPosition) { 
         // Here you handle the on item selected event (this skips the hint selected event) 

         countryCode = countryDetailList.get(position).getCountryCode(); 



        } 
       }); 
     hintSpinner.init(); 
    } 


} 

私はGridViewの中のすべてのフラグを取得していますが、私は、使用するArrayAdapterクラスを拡張してみてくださいGridViewを使用しての

答えて

0

代わりspinner..kindlyヘルプの選択にしたい:HERE

はMY CODE IS画像を ListViewに設定し、国名に基づいてフィルタを実装します。これは一度に1つのフラグだけを表示させたい場合には問題ありません。

+0

選択なしで既に動作しています...他のオプションも可能です – user7316606

+0

選択した国旗をスピナーの右側に表示したかったですか? –

+0

はい。スピナーに応じて – user7316606

関連する問題