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.