# Data formatting using string spanning

I would like to know how to make my code better. I'm still a newbie in Android, so any tips are appreciated.

The code is an AsyncTask class extender that formats data into a nice looking Spannable String. It works, but it's quite slow, even if ran on a background thread.

//SlideContentType is an enumerable with 4 options: Movie, Action, Game, Music
//SlideContent is a class that stores info about a specific movie/song/game etc...

@Override
protected Pair<SpannableStringBuilder, String> doInBackground(Pair<SlideContent.SlideContentType, List<SlideContent>>... pairs){

//Get a reference to the list of all Slide Contents containing movies/games/etc.
List<SlideContent> list = (List<SlideContent>) pairs[0].second;

//Select a random item in the list
Random random = new Random();
random.setSeed(System.nanoTime());
int randomNr = random.nextInt(list.size());
SlideContent currentContent = list.get(randomNr);

//The link and the content are released as a pair

//End result variable
SpannableStringBuilder coloredString = new SpannableStringBuilder();

//Do the appropriate actions
if(pairs[0].first == SlideContent.SlideContentType.Movie){

//Format the information from the Slide Content into a nice looking Spannable String
SpannableString text = new SpannableString(currentContent.movieTitle);
text.setSpan(new StyleSpan(Typeface.BOLD), 0, text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
coloredString.append(text);

coloredString.append(" (" + currentContent.movieYear + ")\n");

text = new SpannableString(currentContent.movieGenres);
text.setSpan(new RelativeSizeSpan(0.7f), 0, text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
text.setSpan(new ForegroundColorSpan(Color.GRAY), 0, text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
coloredString.append(text);
coloredString.append("\n");

text = new SpannableString(currentContent.movieDescription);
text.setSpan(new RelativeSizeSpan(0.8f), 0, text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
coloredString.append(text);
coloredString.append("\n");

text = new SpannableString("Movie rated " + currentContent.movieRating + " out of 10.");
text.setSpan(new RelativeSizeSpan(0.7f), 0, text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
text.setSpan(new ForegroundColorSpan(Color.GRAY), 0, text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
coloredString.append(text);

}
//Return a pair of the Spannable String Builder and the link to the related website
}
}


Random random = new Random();
random.setSeed(System.nanoTime());


Don't put this inside here. I'm assuming that this method will end up being called more than once. If so, you don't want to be seeding and re-seeding the Random every call.

random should be moved to a field of the class. Also, I don't think you need to manually call Random.setSeed. See here for more details.

I don't know my Java 100%, but I think this might be a typo:

Pair<SlideContent.SlideContentType, List<SlideContent>>... pairs


I think it should be:

Pair<SlideContent.SlideContentType>, List<SlideContent>... pairs