为什么我的AlertDialog一直在显示?

问题描述 投票:1回答:1

我试图用recyclerview加片段制作一个显示热点新闻的应用程序,使用的是newsapi。

当我启动程序时,AlertDialog永远不会结束。你能帮助我吗?

package com.addindev.projectalpha.Layouts;

import android.app.AlertDialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.addindev.projectalpha.Adapter.ListNewsAdapter;
import com.addindev.projectalpha.Common.Common;
import com.addindev.projectalpha.Interface.NewsService;
import com.addindev.projectalpha.Model.Article;
import com.addindev.projectalpha.Model.News;
import com.addindev.projectalpha.R;

import java.util.List;

import dmax.dialog.SpotsDialog;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ArticleFragment extends Fragment {
    AlertDialog dialog;
    NewsService mService;
    SwipeRefreshLayout swipeRefreshLayout;

    String source="",sortBy="";

    ListNewsAdapter adapter;
    RecyclerView lstNews;
    RecyclerView.LayoutManager layoutManager;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View RootView = inflater.inflate(R.layout.fragment_article, container, false);

        mService = Common.getNewsService();
        dialog = new SpotsDialog(getActivity());

        swipeRefreshLayout = (SwipeRefreshLayout)RootView.findViewById(R.id.swipeRefresh);
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                loadNews(source,true);
            }
        });

        lstNews =  (RecyclerView)RootView.findViewById(R.id.recycler_view);
        lstNews.setHasFixedSize(true);
        layoutManager = new LinearLayoutManager(getActivity());
        lstNews.setLayoutManager(layoutManager);
        loadNews(source,false);

        return RootView;
    }

    private void loadNews(String source, boolean isRefreshed) {
        if(!isRefreshed)
        {
            dialog.show();
            mService.getNewestArticles(Common.getAPIUrl(source,sortBy,Common.API_KEY))
                    .enqueue(new Callback<News>() {
                        @Override
                        public void onResponse(Call<News> call, Response<News> response) {
                            dialog.dismiss();

                            List<Article> item = response.body().getArticles();

                            adapter = new ListNewsAdapter(item, getActivity().getBaseContext());
                            adapter.notifyDataSetChanged();
                            lstNews.setAdapter(adapter);

                        }

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

                        }
                    });
        }
        else
        {
            dialog.show();
            mService.getNewestArticles(Common.getAPIUrl(source,sortBy,Common.API_KEY))
                    .enqueue(new Callback<News>() {
                        @Override
                        public void onResponse(Call<News> call, Response<News> response) {
                            dialog.dismiss();

                            List<Article> item = response.body().getArticles();

                            adapter = new ListNewsAdapter(item, getActivity().getBaseContext());
                            adapter.notifyDataSetChanged();
                            lstNews.setAdapter(adapter);

                        }

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

                        }
                    });
            swipeRefreshLayout.setRefreshing(false);
        }
    }

}
java android fragment loading infinite
1个回答
0
投票

猜测了一下,但你并没有驳回对话框中的 onFailure.

你几乎可以肯定的是,在成功和失败的对话中,你都会想要驳回。 故障案例。

© www.soinside.com 2019 - 2024. All rights reserved.