0

I have read several questions on stackoverflow addressing this problem and implented solutions, but still I cannot update items. By pressing a menu icon, searchString() is invoked. Inside this method i want to update recyclerview item which not works. Here is related parts of my code:

 /* in my notation, pg is equivalent to page.*/ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_ebook_list, container, false); mEbookRecyclerView = (RecyclerView) view.findViewById(R.id.ebook_recycler_view); mLayoutManager = new LinearLayoutManager(getActivity()); mEbookRecyclerView.setLayoutManager(mLayoutManager); updateUI(); return view; } @Override public void onResume() { super.onResume(); updateUI(); } private void updateUI() { if (mAdapter == null) { EbookLab ebookLab = EbookLab.get(getActivity()); List<String> pgs = ebookLab.getPgs(); mAdapter = new EbookAdapter(pgs); mEbookRecyclerView.setAdapter(mAdapter); } } /* ******codes for preparation of menus which I have ignored *******/ private class EbookHolder extends RecyclerView.ViewHolder { private String mPg; public EbookHolder(LayoutInflater inflater, ViewGroup container) { super(inflater.inflate(R.layout.list_item_ebook, container, false)); mListWebView = (WebView) itemView.findViewById(R.id.list_web_View); mListWebView.getSettings().setLoadWithOverviewMode(true); } public void bindEbook(String pg) { mPg = pg; mListWebView.loadDataWithBaseURL("file:///android_asset/", mPg, "text/html", "UTF-8", null); } } private class EbookAdapter extends RecyclerView.Adapter<EbookHolder> { private List<String> mPgs; public EbookAdapter(List<String> pgs) { mPgs = pgs; } @Override public EbookHolder onCreateViewHolder(ViewGroup parent, int viewType) { LayoutInflater layoutInflater = LayoutInflater.from(getActivity()); return new EbookHolder(layoutInflater, parent); } @Override public void onBindViewHolder(EbookHolder holder, int position) { String pg = mPgs.get(position); holder.bindEbook(pg); } @Override public int getItemCount() { return mPgs.size(); } public void setPgs(List<String> pgs) { mPgs.clear(); mPgs.addAll(pgs); notifyDataSetChanged(); } } public void searchString(String qSrh) { EbookLab ebookLab = EbookLab.get(getActivity()); List<String> pgs = ebookLab.getPgs(); List<String> tmppgs = new ArrayList<String>(pgs); /** Some code for manipulation of tmppgs and updating recyclerview based on the tmppgs **/ mAdapter.setPgs(tmppgs); } 

Any helps is appreciated.

2
  • Can you try calling adapter.notifyDataSetChanged()? Commented Apr 12, 2016 at 13:55
  • Yes,I have tried before and failed. Commented Apr 12, 2016 at 14:02

3 Answers 3

4

use

notifyItemRangeChanged(0, pgsList.size());

instead

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you @I2S0215WMAB08. It worked and now the items are updated, but when I scroll down, first item is moved to the last position and last item to the first position. Do you know its reason?
Make sure that your array has right elements at the right position . Also if the answer worked , would you please accept it . Thanks!!
1

I would try to add a TextView displaying the mPg string in your View layout (and bind the value in the ViewHolder) to identify where the problem is.

If the item strings appear means that your setPgs updates properly the RecyclerView and the problem is in the WebView loadDataWithBaseURL part.

6 Comments

Thank you @ctarabusi. I have tried checking if (mPg!= null) and changing visibility of webview but without any success.
Did you try as I proposed to add a TextView on your R.layout.list_item_ebook with the pg as content?
I will try it. is there any reason that you think that the webview could be the origion of the problem?
I have implemented your suggestion. Both webview and textview are updated now. Is it a bug? or there is another reason?
I am glad it helped, you are going into the right direction (the problem was not the adapter side) I don't know it could the WebView#loadDataWithBaseURL not causing redraw if already called one time... try to have a look to the source code of the method
|
1

Try to direct update the pgsList and then call notifyDataSetChanged

At searchString function remove mAdapter.setPgs(tmppgs);

and update mAdapter direct like below,

public void searchString(String qSrh) { EbookLab ebookLab = EbookLab.get(getActivity()); List<String> pgs = ebookLab.getPgs(); List<String> tmppgs = new ArrayList<String>(pgs); // mAdapter.setPgs(tmppgs); pgsList.clear(); pgsList.addAll(tmppgs); mAdapter.notifyDataSetChanged(); } 

and update updateUI() function

 List<String> pgsList = new ArrayList<String>(); private void updateUI() { if (mAdapter == null) { EbookLab ebookLab = EbookLab.get(getActivity()); pgsList = ebookLab.getPgs(); mAdapter = new EbookAdapter(pgsList); mEbookRecyclerView.setAdapter(mAdapter); } } 

1 Comment

not worked. I have inserted a blank else{} to updateUI(). when I exit app using back button and then entering the app, the items are updated.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.