I'm building an app and I'm stuck on youtube part.I need a solution on how to open a youtube video by clicking on a item of listview.I'm using Fragments and I already retrieved data from Firebase.Those data are Albums and tracks of said albums.When you click on any album, it opens a new Activity with tracks.Now, I want when user clicks on any of tracks, to open a video of that track. I need simplest code, nothing too complicated.I'll post my code.I hope you guys and girls can help my, cause I'm doing this for the first time and I really need some help.Thank you in advance.
public class TracksActivity extends FragmentActivity implements ValueEventListener {
public static final String TRACKS = "Tracks";
public static final String DB_NAME = "MetalApp";
public static final String ALBUM_KEY = "SelectedAlbumKey";
public static final String LOG_TAG = "FB_database_tag";
private int targetPosition = -1;
private DatabaseReference mDatabaseReference;
private TrackAdapter tAdapter;
@BindView(R.id.lvTracks)
ListView lvTracks;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.track_fragment);
ButterKnife.bind(this);
}
@Override
public View onCreateView(String name, Context context, AttributeSet attrs) {
Intent podaci = getIntent();
targetPosition = podaci.getIntExtra(ALBUM_KEY, -1);
this.mDatabaseReference = FirebaseDatabase.getInstance().getReference(DB_NAME).child(TRACKS);
this.mDatabaseReference.addValueEventListener(this);
return super.onCreateView(name, context, attrs);
}
@Override
public void onDataChange(DataSnapshot s) {
final List<Track> tracskList = new ArrayList<>();
HashMap<String, HashMap> tracksInDatabase = (HashMap<String, HashMap>) s.getValue();
Set<String> keys = tracksInDatabase.keySet();
List<String> tempKeys = new ArrayList<>();
tempKeys.addAll(keys);
Collections.sort(tempKeys, new Comparator<String>() {
@Override
public int compare(String s, String t1) {
return s.compareToIgnoreCase(t1);
}
});
int currentPosition = 0;
for (String albumTrack : tempKeys) {
if (currentPosition == targetPosition) {
HashMap<String, HashMap> trackList = tracksInDatabase.get(albumTrack);
List<Track> tracks = mapTracks(trackList);
Collections.sort(tracks, new Comparator<Track>() {
@Override
public int compare(Track track, Track t1) {
return track.getId().compareTo(t1.getId());
}
});
tAdapter = new TrackAdapter(tracks);
this.lvTracks.setAdapter(tAdapter);
}
currentPosition++;
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.e(LOG_TAG, databaseError.getMessage());
}
private List<Track> mapTracks(HashMap<String, HashMap> m) {
List<Track> r = new ArrayList<>();
Set<String> keys = m.keySet();
for (String key : keys) {
HashMap<String, Object> album = m.get(key);
String id = (String) album.get("id");
String title = (String) album.get("title");
String url = (String) album.get("url");
Track t = new Track(id, title, url);
r.add(t);
}
return r;
}
@OnItemClick(R.id.lvTracks)
public void onItemClick(int position){
String[] arrayList = this.getResources().getStringArray(R.array.tracks);
String tr = arrayList[position];
Uri uri = Uri.parse(tr);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
this.startActivity(intent);
}
}
Method onItemClick is probably wrong, so I would ask you to help me with that.
This is Activity that should contain code for youtube. Also, my xml. is fragment.
public class YT_Activity extends AppCompatActivity {
@BindView(R.id.f_Youtube)
Fragment f_Yotube;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_yt_);
ButterKnife.bind(this);
}
}
What I have tried:
I tryied using google to find answer, but I didn't find what I was looking for.Same thing on Youtube.