我试图制作一个应用程序,该应用程序将使用newsapi来显示带有recyclerview plus片段的热门新闻。
当我启动程序时,警报对话永远不会结束。你能帮我吗?
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);
}
}
}
有点猜想,但您没有忽略onFailure
中的对话框。
几乎可以肯定,在成功的两个案例中,您都希望关闭该对话框。