简体   繁体   English

如何在Android中的微调器onItemSelected上刷新片段

[英]How to refresh fragment on spinner onItemSelected in android

I have a fragment with a spinner and a listview the list view is updated when the user chooses one of the options from the spinner, and I came to an idea that I have to refresh the fragment when an option is chosen. 我有一个带有微调器和listview的片段,当用户从微调器中选择一个选项时,列表视图会更新,并且我想到一个想法,当选择一个选项时,我必须刷新该片段。 but the issue is the fragment goes in an infinity loop when I refresh the fragment from "onItemSelected" so I made a "refreshFragment method" to solve the issue but now after the user chooses an option the first option from the spinner don't refresh the fragment again, all the other options working well except the first one because I didn't add the "refreshFragment method" in to this option because if I add it it will go to the infinity loop again. 但是问题是当我从“ onItemSelected”刷新片段时片段进入无限循环,所以我做了一个“ refreshFragment方法”来解决该问题,但是现在用户选择一个选项后,微调器中的第一个选项不会刷新再次执行该片段操作,除第一个选项外,其他所有选项均正常运行,因为我没有在该选项中添加“ refreshFragment方法”,因为如果添加该选项,它将再次进入无限循环。 sorry for my bad English I always get down votes because of that. 对不起,我的英语不好,因此我总是不赞成。

    public class MatchesFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener, AdapterView.OnItemSelectedListener {

        private RecyclerView recyclerView;
        private MatchAdapter matchAdapter;
        private ArrayList<Matches> matchCollection;
        SwipeRefreshLayout mSwipeRefreshLayout;

        String apiLink;
        String apiType = "1";


        public MatchesFragment() {
            // Required empty public constructor
        }

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
        }


        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            // Inflate the layout for this fragment
            return inflater.inflate(R.layout.fragment_matches, container, false);
        }

        @Override
        public void onActivityCreated(@Nullable Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);

            mSwipeRefreshLayout = (SwipeRefreshLayout) getView().findViewById(R.id.swiperefresh);
            mSwipeRefreshLayout.setOnRefreshListener(this);


            if (getArguments() != null) {
                Bundle arguments = getArguments();
                apiLink = arguments.getString("ApiLink");
                apiType = arguments.getString("apiType");
                Toast.makeText(getContext().getApplicationContext(), apiType, Toast.LENGTH_LONG).show();
            }else {
                apiLink = "http://first-api-link";
                Toast.makeText(getContext().getApplicationContext(), "nothing", Toast.LENGTH_LONG).show();
            }

            Spinner spinner = (Spinner) getView().findViewById(R.id.spinner);
            spinner.setOnItemSelectedListener(this);

    // Create an ArrayAdapter using the string array and a default spinner layout
            ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity(),
                    R.array.planets_array, android.R.layout.simple_spinner_item);
    // Specify the layout to use when the list of choices appears
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    // Apply the adapter to the spinner
            spinner.setAdapter(adapter);

            ConnectivityManager cm = (ConnectivityManager) getContext().getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);

            NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
            String answer = "Internet";
            if (null != activeNetwork) {
                if(activeNetwork.getType() == ConnectivityManager.TYPE_WIFI)
                    answer="You are connected to a WiFi Network";
                if(activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE)
                    answer="You are connected to a Mobile Network";
                init();
                new FetchDataTask().execute();
            }
            else {
                answer = "No internet Connectivity";
                Toast.makeText(getContext().getApplicationContext(), answer, Toast.LENGTH_LONG).show();
            }
        }

        private void init() {
            recyclerView = (RecyclerView) getActivity().findViewById(R.id.matchesRecyclerview);
            recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
            recyclerView.setHasFixedSize(true);
            matchCollection = new ArrayList<>();
            matchAdapter = new MatchAdapter(matchCollection, getActivity());
            recyclerView.setAdapter(matchAdapter);
        }

        @Override
        public void onRefresh() {
            init();
            new FetchDataTask().execute();
        }

        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

            apiLink = "http://first-api-link";
            switch (position){
//can't add refreshFragment in here it makes infinity lopp
                case 0: apiLink = "first-api-link";
                break;
                case 1:
                    refreshFragment("second-api-link", 2);
                break;
                case 2:
                    refreshFragment("third-api-link", 3);
                    break;
                case 3:
                    refreshFragment("fourth-api-link", 4);
                    break;
                case 4:
                    refreshFragment("fifth-api-link", 5);
                    break;
                case 5:
                    refreshFragment("sixth-api-link", 6);
                    break;
            }
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }

        public class FetchDataTask extends AsyncTask<Void, Void, Void> {
            private String mZomatoString;

            @Override
            protected Void doInBackground(Void... params) {
                HttpURLConnection urlConnection = null;
                BufferedReader reader = null;
                Uri builtUri = Uri.parse(apiLink);
                URL url;
                try {
                    url = new URL(builtUri.toString());
                    urlConnection = (HttpURLConnection) url.openConnection();
                    urlConnection.setRequestMethod("GET");
                    urlConnection.setRequestProperty("user-key", "acfd3e623c5f01289bd87aaaff1926c1");
                    urlConnection.connect();

                    InputStream inputStream = urlConnection.getInputStream();
                    StringBuffer buffer = new StringBuffer();
                    if (inputStream == null) {
                        //Nothing to do
                        return null;
                    }

                    reader = new BufferedReader(new InputStreamReader(inputStream));

                    String line;
                    while ((line = reader.readLine()) != null) {
                        buffer.append(line + "\n");
                    }

                    if (buffer.length() == 0) {
                        return null;
                    }

                    mZomatoString = buffer.toString();
                    JSONObject jsonObject = new JSONObject(mZomatoString);

                    Log.v("Response", jsonObject.toString());

                    JSONArray matchArray = jsonObject.getJSONArray("data");


                    //list = new ArrayList<>();
                    for (int i = 0; i < matchArray.length(); i++) {

                        Log.v("BRAD_", i + "");
                        String home_team;
                        String away_team;
                        String home_flag;
                        String away_flag;

                        JSONObject jTeam = (JSONObject) matchArray.get(i);

                        if (apiType == "1"){
                        JSONObject jHomeTeam = (JSONObject) jTeam.getJSONObject("home_team");
                        JSONObject jAwayTeam = (JSONObject) jTeam.getJSONObject("away_team");

                        //get data's from json sent from link
                        home_team = jHomeTeam.getString("name");
                        away_team = jAwayTeam.getString("name");
                        home_flag = jHomeTeam.getString("flag");
                        away_flag = jAwayTeam.getString("flag");
                        }else{
                            //get data's from json sent from link
                            home_team = jTeam.getString("home_team");
                            away_team = jTeam.getString("away_team");
                            home_flag = "https://www.countries-ofthe-world.com/flags-normal/flag-of-Russia.png";
                            away_flag = "https://www.countries-ofthe-world.com/flags-normal/flag-of-Russia.png";
                        }



                        Matches matches = new Matches();
                        matches.setHome_team(home_team);
                        matches.setAway_team(away_team);
                        matches.setHome_flag(home_flag);
                        matches.setAway_flag(away_flag);

                        matchCollection.add(matches);
                        // Stopping swipe refresh
                        mSwipeRefreshLayout.setRefreshing(false);
                    }
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (JSONException e) {
                    e.printStackTrace();
                } finally {
                    if (urlConnection != null) {
                        urlConnection.disconnect();
                    }
                    if (reader != null) {
                        try {
                            reader.close();
                        } catch (final IOException e) {
                            Log.e("MainActivity", "Error closing stream", e);
                        }
                    }
                }
                return null;
            }

            @Override
            protected void onPostExecute(Void aVoid) {
                matchAdapter.notifyDataSetChanged();
            }
        }

        public void refreshFragment(String apiLinks, int apiType){
            FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            MatchesFragment fragment = new MatchesFragment();
            Bundle args = new Bundle();
            args.putString("ApiLink", apiLinks);
            args.putString("apiType", String.valueOf(apiType));
            fragment.setArguments(args);
            fragmentTransaction.replace(R.id.linearfr, fragment);
            fragmentTransaction.commit();
        }

    }

THere are a few things to consider when working with a spinner. 这些是与微调器一起使用时要考虑的几件事。 It always has a selected value and the default value selected is the first one. 它始终具有选定的值,并且选定的默认值是第一个。 Therefore, when the fragment is created the first value in the spinner is selected. 因此,创建片段时,将选择微调器中的第一个值。

When you manually open the dropdown and select the first item for the first time, it is currently selected by default and therefore your selection is not triggered and thus refresh is not called. 当您手动打开下拉菜单并第一次选择第一个项目时,当前默认情况下处于选中状态,因此不会触发您的选择,因此不会调用刷新。 The trick is to add dummy item as the first one and ignore position 0 in onClick (which you're already doing). 技巧是将虚拟项添加为第一个项,并忽略onClick中的位置0(您已经在执行此操作)。

The infinite loop is happening because spinner selects the first item by default when the view is created and therefore onClick with position 0 is triggered which calls refresh again, changes fragment, new spinner is created, first item is selected, and the vicious cycle continues. 之所以发生无限循环,是因为在创建视图时微调器默认情况下选择了第一项,因此触发了位置为0的onClick,该调用再次调用刷新,更改片段,创建了新的微调器,选择了第一项,并且恶性循环继续。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM