我有一个RecyclerView
填充ArrayList
的应用程序,我从网站上抓取了fragment
。正如我目前所构建的那样,包含RecyclerView
的相关AsyncTask
包含fragment's
,因此数据会在onCreate
AsyncTask's onPostExecute
方法中被删除。然后在AsyncTask
中填充RecyclerView。现在,事实是,这通常导致我的应用程序随机崩溃,因为来自null
刮刀的数据返回RecyclerView
。此外,在我看来,这种方法似乎不必要地复杂并且可以简化。因此,我的问题是通常被认为是用填充数据填充TabLayout
的最有效和最简单的方法? (注意:我还有fragment
更改了正在抓取的数据的网址,因此我必须为每个标签NavigationDrawer
(并且每次用户)r-escrape数据重新打开相关的public class RefresherFragment extends Fragment{
SwipeRefreshLayout swipeRefreshLayout;
public String markingPeriod;
public static RefresherFragment newInstance(String markingPeriod){
RefresherFragment instance = new RefresherFragment();
Bundle bundle = new Bundle();
bundle.putString("mp", markingPeriod);
instance.setArguments(bundle);
return instance;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
markingPeriod = getArguments().getString("mp");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.mp_refresher_fragment, container, false);
new Scraper(markingPeriod).execute();
Log.e("RefresherFragment", "MP=" + markingPeriod);
return view;
}
//fragment setup-------------------------------------------------------------------------
public ArrayList<String> extractClasses (Document document){
ArrayList<String> classList = new ArrayList<>();
Elements elements = document.select(".categorytab");
for (Element element : elements){
classList.add(element.text().toString());
}
return classList;
}
public ArrayList<String> extractGrades (Document document){
ArrayList<String> keepList = new ArrayList<>();
Elements elements = document.select(".cellRight");
for (Element element : elements){
boolean keep = true;
for (Element e : element.parents()){
if (e.hasClass("cellRight")){
keep = false;
break;
}
}
if (keep){
keepList.add(element.text().toString());
}
}
return keepList;
}
public class Wrapper{
Document document;
Elements elements;
}
private class Scraper extends AsyncTask<String, Void, Wrapper> {
String mMarkingPeriod;
public Scraper(String mp) {
mMarkingPeriod = mp;
}
@Override
protected Wrapper doInBackground(String... strings) {
Wrapper wrapper = new Wrapper();
SessionManager sessionManager = new SessionManager(getContext());
String username = sessionManager.getUsername();
String password = sessionManager.getPassword();
DataManager dataManager = new DataManager(username,password);
Map<String, String> preLoginCookies = dataManager.getPreLoginCookies(mMarkingPeriod);
Map<String, String> postLoginCookies = dataManager.getPostLoginCookies(preLoginCookies);
Document doc = dataManager.getDocumentByMP(postLoginCookies,mMarkingPeriod);
if (doc == null){
Log.e("doInBackground Check", "null");}
Elements elements = doc.select(".cellRight");
wrapper.document = doc;
wrapper.elements = elements;
return wrapper;
}
@Override
protected void onPostExecute(Wrapper wrapper) {
Document document = wrapper.document;
ArrayList <String> grades = extractGrades(document);
ArrayList <String> classes = extractClasses(document);
for (String classElement : classes){
Log.e("Class", classElement);
}
for (String gradeElement : grades){
Log.e("Grade", gradeElement);
}
Log.e("Checkpoint", "Iterating elements");
RecyclerView recyclerView = (RecyclerView)getActivity().findViewById(R.id.grade_recycler);
recyclerView.setAdapter(new GradeRecyclerAdapter(classes, grades));
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
Log.e("Checkpoint", "Setting up Recycler");
swipeRefreshLayout = (SwipeRefreshLayout) getActivity().findViewById(R.id.refresher);
swipeRefreshLayout.setRefreshing(false);
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
new Scraper(getArguments().getString("mp")).execute();
}
});
}
}
}
部分,或者以某种方式重构我的应用程序,以便不再需要这样做。感谢任何帮助!
我的RefresherFragment类:
Service::with(['vehicle','parts'])->find($serviceid);