I’m trying to open videos when the user clicks on an item from a list. I have the code for the listview working, and the code to open videos. I just can’t figure out how to combine the two.
Here’s my code to open videos.
String video_path = "http://www.youtube.com/watch?v=qrEUBl2pacU";
Uri uri = Uri.parse(video_path);
uri = Uri.parse("vnd.youtube: " + uri.getQueryParameter("v"));
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
Is it possible to put this code into onItemClick?
Edit: Here’s the whole class. I have a feeling that my code is incorrect, as it’s all in onCreate. But, I followed a tutorial, and that’s where is was.
public class Videos extends Activity{
private ListView lv;
String videoNames[] = {"Doctor Who Trailer", "T+S - Northshore", "Foo Fighters - Everlong"};
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.videos);
lv = (ListView) findViewById(R.id.videosListView);
lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, videoNames));
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?>Videos, View v, int position, long id) {
String video_path = "http://www.youtube.com/watch?v=qrEUBl2pacU";
Uri uri = Uri.parse(video_path);
uri = Uri.parse("vnd.youtube: " + uri.getQueryParameter("v"));
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
};
});
}
}
This is what your onclicklistener should look like. Try this code:
UPDATE: I would suggest using a Hashmap for a key value pair. Something like (I didn’t compile this):
You then could get all of listview set by using hm.keySet().
Lastly use the Hashmap to lookup the youtube link:
….