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...
public class PickCardAsyncTask extends AsyncTask<Pair<SlideContent.SlideContentType, List<SlideContent>>, Integer, Pair<SpannableStringBuilder, String>> {
@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
String websiteLink = currentContent.movieWebsite;
//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
return new Pair<>(coloredString, websiteLink);
}
}