我正在使用带有国家名称的微调器,下面我根据国家微调器显示标志。我想根据微调器选择显示标志而不是所有..我可以在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中获得了所有标志,但我希望选择spinner..kindly help
答案 0 :(得分:0)
尝试扩展GridView
类以使用图片,将其设置为ArrayAdapter
并根据国家/地区名称实施过滤器,而不是使用ListView
。如果你想一次只显示一个标志,这应该没问题。