Android Open Source - lastfm-android-music-discovery Album Tracks Fragment






From Project

Back to project page lastfm-android-music-discovery.

License

The source code is released under:

Apache License

If you think the Android project lastfm-android-music-discovery listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.mikeladze.musicdiscovery.album.fragment;
// www.  j  a v a2  s .  c  om
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.mikeladze.musicdiscovery.R;
import com.mikeladze.musicdiscovery.album.adapter.LastFMTrack;
import com.mikeladze.musicdiscovery.album.adapter.OnTrackClickListener;
import com.mikeladze.musicdiscovery.album.adapter.TrackListAdapter;
import com.mikeladze.musicdiscovery.base.fragment.BaseTabFragment;
import com.mikeladze.musicdiscovery.util.IntentUtils;

/**
 * This fragment displays the tracks on the album in a listview
 * 
 * @author Tim Mikeladze
 */
public class AlbumTracksFragment extends BaseTabFragment implements OnTrackClickListener {
  
  private static final String YOUTUBE_SEARCH_URL = "https://www.youtube.com/results?search_query=";
  private static final String TITLE = "Tracks";
  private ListView listView;
  private TrackListAdapter adapter;
  private ArrayList<LastFMTrack> tracksList;
  
  @Override
  /**
   * Create the view
   */
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_generic_list, container, false);
    
    listView = (ListView) v.findViewById(R.id.view_list);
    tracksList = new ArrayList<LastFMTrack>();
    adapter = new TrackListAdapter(getActivity(), R.layout.view_track_list_item, tracksList);
    adapter.setOnTrackClickListener(this);
    
    return v;
  }
  
  @Override
  public String getTitle() {
    return TITLE;
  }
  
  /**
   * Display album tracks.
   * 
   * @param tracks the tracks
   */
  public void displayAlbumTracks(String artist, JSONArray tracks) {
    try {
      for (int i = 0; i < tracks.length(); i++) {
        
        tracksList.add(new LastFMTrack(tracks.getJSONObject(i)
                            .getString("name"), artist));
      }
      listView.setAdapter(adapter);
    } catch (JSONException e) {
      e.printStackTrace();
    }
  }
  
  @Override
  /**
   * Handles track clicking. Starts new intent in youtube or falls back to browser if youtube is not available to play the track.
   */
  public void onTrackClick(int position) {
    String query = adapter.getItem(position)
                .getTitle() + " " + adapter.getItem(position)
                              .getArtist();
    
    Intent intent = new Intent(Intent.ACTION_SEARCH);
    intent.setPackage("com.google.android.youtube");
    
    if (IntentUtils.isAvailable(getActivity(), intent)) {
      intent.putExtra("query", query);
    } else {
      intent = new Intent("android.intent.action.VIEW", Uri.parse(YOUTUBE_SEARCH_URL + query));
    }
    
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);
  }
}




Java Source Code List

com.mikeladze.musicdiscovery.album.activity.AlbumActivity.java
com.mikeladze.musicdiscovery.album.adapter.LastFMTrack.java
com.mikeladze.musicdiscovery.album.adapter.OnTrackClickListener.java
com.mikeladze.musicdiscovery.album.adapter.TrackListAdapter.java
com.mikeladze.musicdiscovery.album.fragment.AlbumInfoFragment.java
com.mikeladze.musicdiscovery.album.fragment.AlbumTracksFragment.java
com.mikeladze.musicdiscovery.artist.activity.ArtistActivity.java
com.mikeladze.musicdiscovery.artist.fragment.ArtistAlbumsFragment.java
com.mikeladze.musicdiscovery.artist.fragment.ArtistInfoFragment.java
com.mikeladze.musicdiscovery.artist.fragment.SimilarArtistsFragment.java
com.mikeladze.musicdiscovery.base.activity.BaseFragmentActivity.java
com.mikeladze.musicdiscovery.base.adapter.SectionsPagerAdapter.java
com.mikeladze.musicdiscovery.base.fragment.BaseTabFragment.java
com.mikeladze.musicdiscovery.base.fragment.ImageGridViewFragment.java
com.mikeladze.musicdiscovery.http.LastFMResult.java
com.mikeladze.musicdiscovery.http.RestClient.java
com.mikeladze.musicdiscovery.main.activity.MainActivity.java
com.mikeladze.musicdiscovery.main.fragment.HypedArtistsFragment.java
com.mikeladze.musicdiscovery.main.fragment.TopArtistsFragment.java
com.mikeladze.musicdiscovery.search.activity.SearchResultsActivity.java
com.mikeladze.musicdiscovery.search.adapter.SearchResultsAdapter.java
com.mikeladze.musicdiscovery.util.IntentUtils.java
com.mikeladze.musicdiscovery.view.gridview.DynamicImageView.java
com.mikeladze.musicdiscovery.view.gridview.ImageGridAdapter.java
com.mikeladze.musicdiscovery.view.gridview.loader.FileCache.java
com.mikeladze.musicdiscovery.view.gridview.loader.ImageLoader.java
com.mikeladze.musicdiscovery.view.gridview.loader.MemoryCache.java
com.mikeladze.musicdiscovery.view.gridview.loader.Utils.java