0

Let me try to make the situation that I'm facing clearly. I have a list of titles retrieved from XML file, and it is randomized to be shown in a TextView in a ViewPager.

I already print the list of titles, and the randomized title using System.out.println to view the title, before using setText to set it in the TextView.

Thing is, the randomized title is not shown in the current view in the ViewPager, it is shown in the next view, and when I swipe, the same thing goes on. In the current view, a different title is shown, different than the randomized title.

I need the text to show correctly in current view, as when user click on it, it should open a page with the text shown in the view.

Anyone know why I'm facing this problem and how can I fix it?

//Code Snippet is from a PagerAdapter
    try{
    randomNo = getRandomNumber(nl.getLength());
    System.out.println("AMOUNT OF ARTICLES : " + nl.getLength());
    System.out.println("RANDOM NUMBER : " + randomNo);

    showTitle = texts.get(randomNo);
    showArticle = urls.get(randomNo);

    System.out.println("RANDOM ARTICLE TITLE : " + showTitle);
    text5.setText(showTitle);  
    }

    catch(Exception e)
    {
        e.printStackTrace();
    }

UPDATE I figured out why the text does not show correctly.

Example, view1 is showing text1 and loads article 1, view2 is showing text2 and loads article2, etc. But, because it loads all the article links and titles on the first run, it displays the randomized titles it according to the randomized sequence.

Title1 Title2

Link1 Link2

So, View1 actually gets link2 (latest randomized link), but still stuck with text1. Every time I swipe, it will randomized again and caused the titles and links to jumble up.

Found a way to work around it based on the project requirement. I run the randomized code in my Fragment class instead of ViewPagerAdapter, that way it loads the randomized code just once, and this help to display the same titles and links in every view. I retrieved the title from the Fragment class and display it in my adapter. That way, every view is showing the same title and links until the following run.

Honey H
  • 299
  • 1
  • 6
  • 23

1 Answers1

0

Viewpager caches your view to save resources, so when you swipe it will eventually throws it away if it is farther away than 1 page left or right. So what you need to do is notify the viewpager, that you need an update. This is achived with this "hack":

Override getItemPosition in your PagerAdapter like this:

public int getItemPosition(Object object) {
    return POSITION_NONE;
}

Then every time you randomize your Textview you have to call notifyDataSetChanged() on the viewpager.

See this question for more detail.

Community
  • 1
  • 1
Patrick
  • 33,984
  • 10
  • 106
  • 126
  • Thanks for the suggestion. I tried it, and it did refresh the view pager. But it didn't help in my case. I kinda found out the problem to my situation and a way to work around it. I will use this suggestion if I ever need to refresh my viewpager. – Honey H Jun 28 '13 at 02:53
  • Eventually, this suggestion helped me solved another refreshing view pager problem that I faced with my ViewPager. Thanks. – Honey H Jun 28 '13 at 12:04