我需要存儲recyclerview檢索到的數據並將其顯示爲供離線使用。如何使用sharedpreference存儲和檢索recyclerview數據
下面是我的代碼:
RecyclerViewFragment
public class HomeFragment extends Fragment implements OnClickListener {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
this.mContext = getActivity().getApplicationContext();
mView = inflater.inflate(R.layout.fragment_home, container, false);
initializeView();
getPosts();
return mView;
}
private void initializeView() {
postsList = (RecyclerView) mView.findViewById(R.id.postsList);
pBar = (LinearLayout) mView.findViewById(R.id.pBar2);
mFabButton = (FloatingActionButton) mView.findViewById(R.id.fabButton);
mFabButton.setOnClickListener(this);
mFabButton.setRippleColor(getActivity().getResources().getColor(R.color.accentColor));
//layout manager
notice = (TextView) mView.findViewById(R.id.notice);
layoutManager = new LinearLayoutManager(getActivity());
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
postsList.setLayoutManager(layoutManager);
mPostsItems = new ArrayList<PostsItem>();
mHomeListAdapter = new HomeListAdapter(getActivity(), mPostsItems);
postsList.setAdapter(mHomeListAdapter);
mSwipeRefreshLayout = (SwipeRefreshLayout) mView.findViewById(R.id.swipeHome);
mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
M.L("called");
// Refresh items
setCurrentPage(1);
getPosts();
}
});
//setting up our OnScrollListener
postsList.addOnScrollListener(new HidingScrollListener(layoutManager) {
@Override
public void onHide() {
FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) mFabButton.getLayoutParams();
int fabBottomMargin = lp.bottomMargin;
mFabButton.animate()
.translationY(mFabButton.getHeight() + fabBottomMargin)
.setInterpolator(new AccelerateInterpolator(2))
.start();
}
@Override
public void onShow() {
mFabButton.animate()
.translationY(0)
.setInterpolator(new DecelerateInterpolator(2))
.start();
}
@Override
public void onLoadMore(int currentPage) {
}
});
}
@Override
public void onDestroy() {
super.onResume();
}
public void getPosts() {
PostsAPI mPostsAPI = APIService.createService(PostsAPI.class, M.getToken(getActivity()));
pBar.setVisibility(View.VISIBLE);
mFabButton.setVisibility(View.GONE);
postsList.setVisibility(View.GONE);
mPostsAPI.getPosts(getCurrentPage(), new Callback<List<PostsItem>>() {
@Override
public void success(List<PostsItem> postsItems, retrofit.client.Response response) {
updateView(postsItems);
notice.setVisibility(View.GONE);
pBar.setVisibility(View.GONE);
mFabButton.setVisibility(View.VISIBLE);
postsList.setVisibility(View.VISIBLE);
}
@Override
public void failure(RetrofitError error) {
M.T(getActivity(), getString(R.string.ServerError));
pBar.setVisibility(View.GONE);
notice.setVisibility(View.VISIBLE);
}
});
}
private void updateView(List<PostsItem> postsItems) {
if (getCurrentPage() != 1) {
List<PostsItem> oldItems = mHomeListAdapter.getPosts();
oldItems.addAll(postsItems);
mHomeListAdapter.setPosts(oldItems);
} else {
mHomeListAdapter.setPosts(postsItems);
}
if (mSwipeRefreshLayout.isRefreshing()) {
mSwipeRefreshLayout.setRefreshing(false);
}
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.fabButton) {
mIntent = new Intent(getActivity(), SendPhotoActivity.class);
startActivity(mIntent);
}
}
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
}
RecyclerItemClass
public class PostsItem {
private int id;
public String status;
public String image;
private String date;
private String place;
private String youtube;
//private String link;
private int views;
private int ownerID;
private int privacy;
private int likes;
private int comments,totalComments;
private String ownerName;
private String ownerUsername;
private String ownerPicture;
private boolean liked;
private LinkModel link;
public PostsItem() {
}
public int getTotalComments() {
return totalComments;
}
public void setTotalComments(int totalComments) {
this.totalComments = totalComments;
}
public String getPlace() {
return place;
}
public void setPlace(String place) {
this.place = place;
}
public String getYoutube() {
return youtube;
}
public void setYoutube(String youtube) {
this.youtube = youtube;
}
public LinkModel getLink() {
return link;
}
public void setLink(LinkModel link) {
this.link = link;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public String getOwnerUsername() {
return ownerUsername;
}
public void setOwnerUsername(String ownerUsername) {
this.ownerUsername = ownerUsername;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getViews() {
return views;
}
public void setViews(int views) {
this.views = views;
}
public int getLikes() {
return likes;
}
public void setLikes(int uses) {
this.likes = uses;
}
public int getComments() {
return comments;
}
public void setComments(int uses) {
this.comments = uses;
}
public String getOwnerName() {
return ownerName;
}
public void setOwnerName(String ownerName) {
this.ownerName = ownerName;
}
public int getPrivacy() {
return privacy;
}
public void setPrivacy(int privacy) {
this.privacy = privacy;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getOwnerPicture() {
return ownerPicture;
}
public void setOwnerPicture(String ownerPicture) {
this.ownerPicture = ownerPicture;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public int getOwnerID() {
return ownerID;
}
public void setOwnerID(int ownerID) {
this.ownerID = ownerID;
}
public boolean isLiked() {
return liked;
}
public void setLiked(boolean liked) {
this.liked = liked;
}
}
沒有jux試圖展示如何我發現它在我的應用程序 – sayela
實施sharedpreference系列化你的帖子'response'到'Base64編碼String',然後保存爲'SharedPreferences'。 – Shark
我編輯了你的問題,因爲我們不關心那些額外的評論。如果你現在閱讀它,你可以清楚地看到你所描述的沒有問題或問題。那麼,究竟是什麼問題呢? –