下面列出了android.support.v4.app.FragmentActivity#getSharedPreferences ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void saveCachedDashboardList(String rootNodeStr) throws Exception {
FragmentActivity fa = getActivity();
if (fa == null) {
throw new Exception("Unknown Error: Null getActivity()!");
}
SharedPreferences sharedPreferences = fa.getSharedPreferences("cached_xml", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("dashboard_list_str", rootNodeStr);
editor.apply();
}
private String getCachedDashboardList() throws Exception {
FragmentActivity fa = getActivity();
if (fa == null) {
throw new Exception("Unknown Error: Null getActivity()!");
}
SharedPreferences sharedPreferences = fa.getSharedPreferences("cached_xml", Context.MODE_PRIVATE);
return sharedPreferences.getString("dashboard_list_str", null);
}
private void saveCachedAnnouncementsList(String rootNodeStr) throws Exception {
FragmentActivity fa = getActivity();
if (fa == null) {
throw new Exception("Unknown Error: Null getActivity()!");
}
SharedPreferences sharedPreferences = fa.getSharedPreferences("cached_xml", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("announcement_list_str", rootNodeStr);
editor.apply();
}
private String getCachedAnnouncementsList() throws Exception {
FragmentActivity fa = getActivity();
if (fa == null) {
throw new Exception("Unknown Error: Null getActivity()!");
}
SharedPreferences sharedPreferences = fa.getSharedPreferences("cached_xml", Context.MODE_PRIVATE);
return sharedPreferences.getString("announcement_list_str", null);
}
private void saveCachedCourseList(String rootNodeStr) throws Exception {
FragmentActivity fa = getActivity();
if (fa == null) {
throw new Exception("Unknown Error: Null getActivity()!");
}
SharedPreferences sharedPreferences = fa.getSharedPreferences("cached_xml", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("course_list_str", rootNodeStr);
editor.apply();
}
private String getCachedCourseList() throws Exception {
FragmentActivity fa = getActivity();
if (fa == null) {
throw new Exception("Unknown Error: Null getActivity()!");
}
SharedPreferences sharedPreferences = fa.getSharedPreferences("cached_xml", Context.MODE_PRIVATE);
return sharedPreferences.getString("course_list_str", null);
}
private void updateAdapter(String rootNodeStr, boolean showAnimation) {
Node rootNode = Utils.stringToNode(rootNodeStr);
if (rootNode != null) {
FragmentActivity fa = getActivity();
if (fa == null) {
return;
}
SharedPreferences sharedPreferences = fa.getSharedPreferences("pinnedCourseList", Context.MODE_PRIVATE);
Set<String> hset = sharedPreferences.getStringSet("key", null);
if (hset == null)
hset = new HashSet<>();
ArrayList<CourseInfo> courses_list = new ArrayList<>();
NodeList nCoursesList = rootNode.getFirstChild().getChildNodes();
for (int temp = 0; temp < nCoursesList.getLength(); temp++) {
CourseInfo ci = new CourseInfo((Element) nCoursesList.item(temp));
if (hset.contains(ci.getRawCourseName()))
ci.setPinned(1);
courses_list.add(ci);
}
adapter.updateList(courses_list);
// 显示课程列表的fancy的动画
if (showAnimation)
mRecyclerView.scheduleLayoutAnimation();
if (showLongPressHintFlag)
showLongPressHint();
}
}
private void saveCachedAnnouncementsList(String rootNodeStr) throws Exception {
FragmentActivity fa = getActivity();
if (fa == null) {
throw new Exception("Unknown Error: Null getActivity()!");
}
SharedPreferences sharedPreferences = fa.getSharedPreferences("cached_xml", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("announcement_list_str", rootNodeStr);
editor.apply();
}
private String getCachedAnnouncementsList() throws Exception {
FragmentActivity fa = getActivity();
if (fa == null) {
throw new Exception("Unknown Error: Null getActivity()!");
}
SharedPreferences sharedPreferences = fa.getSharedPreferences("cached_xml", Context.MODE_PRIVATE);
return sharedPreferences.getString("announcement_list_str", null);
}
private void saveCachedCourseList(String rootNodeStr) throws Exception {
FragmentActivity fa = getActivity();
if (fa == null) {
throw new Exception("Unknown Error: Null getActivity()!");
}
SharedPreferences sharedPreferences = fa.getSharedPreferences("cached_xml", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("course_list_str", rootNodeStr);
editor.apply();
}
private String getCachedCourseList() throws Exception {
FragmentActivity fa = getActivity();
if (fa == null) {
throw new Exception("Unknown Error: Null getActivity()!");
}
SharedPreferences sharedPreferences = fa.getSharedPreferences("cached_xml", Context.MODE_PRIVATE);
return sharedPreferences.getString("course_list_str", null);
}
private void updateAdapter(String rootNodeStr, boolean showAnimation) {
Node rootNode = Utils.stringToNode(rootNodeStr);
if (rootNode != null) {
FragmentActivity fa = getActivity();
if (fa == null) {
return;
}
SharedPreferences sharedPreferences = fa.getSharedPreferences("pinnedCourseList", Context.MODE_PRIVATE);
Set<String> hset = sharedPreferences.getStringSet("key", null);
if (hset == null)
hset = new HashSet<>();
ArrayList<CourseInfo> courses_list = new ArrayList<>();
NodeList nCoursesList = rootNode.getFirstChild().getChildNodes();
for (int temp = 0; temp < nCoursesList.getLength(); temp++) {
CourseInfo ci = new CourseInfo((Element) nCoursesList.item(temp));
if (hset.contains(ci.getRawCourseName()))
ci.setPinned(1);
courses_list.add(ci);
}
adapter.updateList(courses_list);
// 显示课程列表的fancy的动画
if (showAnimation)
mRecyclerView.scheduleLayoutAnimation();
if (showLongPressHintFlag)
showLongPressHint();
}
}
public LoadWebData(String search_url, FragmentActivity activity) {
super();
this.search_url = search_url;
this.activity = activity;
progressViewLoading = (ProgressWheel) (activity).findViewById(R.id.progressViewLoading);
title_txt = (FontText) (activity).findViewById(R.id.title_txt);
body_txt = (FontText) (activity).findViewById(R.id.body_txt);
btn_view_links = (Button) (activity).findViewById(R.id.btn_view_links);
mainActivity=(MainActivity)activity;
sharedPrefs=activity.getResources().getString(R.string.sharedPrefs);
sharedPreferences=activity.getSharedPreferences(sharedPrefs, Context.MODE_PRIVATE);
editor=sharedPreferences.edit();
}
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
LinearLayout linearLayout = (LinearLayout) inflater.inflate(R.layout.fragment_course_list, container, false);
// 查找xml文件中的对象并保存进Java变量
mRecyclerView = linearLayout.findViewById(R.id.recycler_courses);
mCourseListSwipeContainer = linearLayout.findViewById(R.id.course_list_swipe_container);
// 设置动画
LayoutAnimationController animation = AnimationUtils.loadLayoutAnimation(getContext(), R.anim.layout_animation_fall_down);
mCourseListSwipeContainer.setLayoutAnimation(animation);
// 设置刷新的监听类为此类(监听函数onRefresh)
mCourseListSwipeContainer.setOnRefreshListener(this);
mCourseListSwipeContainer.setColorSchemeColors(getResources().getColor(R.color.colorPrimary), getResources().getColor(R.color.colorAccent));
FragmentActivity fa = getActivity();
// 为了消除编译器Warning,需要判断一下是不是null,其实这基本上不可能出现null
if (fa == null) {
return linearLayout;
}
// 将读取已置顶课程列表的SharedPreferences传递给CourseListRecyclerViewAdapter
SharedPreferences sharedPreferences = fa.getSharedPreferences("pinnedCourseList", Context.MODE_PRIVATE);
adapter = new CourseListRecyclerViewAdapter(new ArrayList<CourseInfo>(), sharedPreferences, this);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
mRecyclerView.setAdapter(adapter);
try {
String cachedCourseList = getCachedCourseList();
if (cachedCourseList == null)
throw new Exception();
updateAdapter(cachedCourseList, false);
} catch (Exception e) {
// 显示Loading的小动画,并在后台读取课程列表
showLoading(true);
mLoadingTask = new CoursesLoadingTask();
mLoadingTask.execute((Void) null);
}
return linearLayout;
}
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
LinearLayout linearLayout = (LinearLayout) inflater.inflate(R.layout.fragment_course_list, container, false);
// 查找xml文件中的对象并保存进Java变量
mRecyclerView = linearLayout.findViewById(R.id.recycler_courses);
mCourseListSwipeContainer = linearLayout.findViewById(R.id.course_list_swipe_container);
// 设置动画
LayoutAnimationController animation = AnimationUtils.loadLayoutAnimation(getContext(), R.anim.layout_animation_fall_down);
mCourseListSwipeContainer.setLayoutAnimation(animation);
// 设置刷新的监听类为此类(监听函数onRefresh)
mCourseListSwipeContainer.setOnRefreshListener(this);
mCourseListSwipeContainer.setColorSchemeColors(getResources().getColor(R.color.colorPrimary), getResources().getColor(R.color.colorAccent));
FragmentActivity fa = getActivity();
// 为了消除编译器Warning,需要判断一下是不是null,其实这基本上不可能出现null
if (fa == null) {
return linearLayout;
}
// 将读取已置顶课程列表的SharedPreferences传递给CourseListRecyclerViewAdapter
SharedPreferences sharedPreferences = fa.getSharedPreferences("pinnedCourseList", Context.MODE_PRIVATE);
adapter = new MyGradeListRecyclerViewAdapter(new ArrayList<CourseInfo>(), sharedPreferences, this);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
mRecyclerView.setAdapter(adapter);
try {
String cachedCourseList = getCachedCourseList();
if (cachedCourseList == null)
throw new Exception();
updateAdapter(cachedCourseList, false);
} catch (Exception e) {
// 显示Loading的小动画,并在后台读取课程列表
showLoading(true);
mLoadingTask = new CoursesLoadingTask();
mLoadingTask.execute((Void) null);
}
return linearLayout;
}