Reputation: 103
I'm using the CustomizedList View code from this tutorial http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/
and I modified it by adding YouTube Videos Id's , but I have a problem when I click on list item and I want to start new activity to play Video , I don't know how to pass the video ID to the next Activity !
My problem is how to get the value of KEY_VID_ID in CustomizedListView Activity !!!
CuztomizedListView.java Code
public class CustomizedListView extends Activity {
// All static variables
static final String URL = "http://api.androidhive.info/music/music.xml";
// XML node keys
static final String KEY_SONG = "song"; // parent node
static final String KEY_ID = "id";
static final String KEY_TITLE = "title";
static final String KEY_ARTIST = "artist";
static final String KEY_DURATION = "duration";
static final String KEY_THUMB_URL = "thumb_url";
static final String KEY_VID_ID = "vid_id";
ListView list;
LazyAdapter adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL); // getting XML from URL
Document doc = parser.getDomElement(xml); // getting DOM element
NodeList nl = doc.getElementsByTagName(KEY_SONG);
// looping through all song nodes <song>
for (int i = 0; i < nl.getLength(); i++) {
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
// adding each child node to HashMap key => value
map.put(KEY_ID, parser.getValue(e, KEY_ID));
map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
map.put(KEY_ARTIST, parser.getValue(e, KEY_ARTIST));
map.put(KEY_DURATION, parser.getValue(e, KEY_DURATION));
map.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));
map.put(KEY_VID_ID, parser.getValue(e, KEY_VID_ID));
// adding HashList to ArrayList
songsList.add(map);
}
list=(ListView)findViewById(R.id.list);
// Getting adapter by passing xml data ArrayList
adapter=new LazyAdapter(this, songsList);
list.setAdapter(adapter);
// Click event for single list row
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent intent = new Intent(CustomizedListView.this, PlayVideo.class);
// intent.setData(Uri.parse());
startActivity(intent);
}
});
}
}
LazyAdapter
public class LazyAdapter extends BaseAdapter {
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater=null;
public ImageLoader imageLoader;
public LazyAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
activity = a;
data=d;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader=new ImageLoader(activity.getApplicationContext());
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.list_row, null);
TextView title = (TextView)vi.findViewById(R.id.title); // title
TextView artist = (TextView)vi.findViewById(R.id.artist); // artist name
TextView duration = (TextView)vi.findViewById(R.id.duration); // duration
ImageView thumb_image=(ImageView)vi.findViewById(R.id.list_image); // thumb image
HashMap<String, String> song = new HashMap<String, String>();
song = data.get(position);
// Setting all values in listview
title.setText(song.get(CustomizedListView.KEY_TITLE));
artist.setText(song.get(CustomizedListView.KEY_ARTIST));
duration.setText(song.get(CustomizedListView.KEY_DURATION));
imageLoader.DisplayImage(song.get(CustomizedListView.KEY_THUMB_URL), thumb_image);
return vi;
}
}
PlayVideo.java
public class PlayVideo extends YouTubeBaseActivity implements
YouTubePlayer.OnInitializedListener {
static private final String DEVELOPER_KEY = "AIzaSyBOQlVNfn6TY_TpyWYKMbwNPk09AhBRWXw";
static private final String VIDEO = "oZbeL1ciR4E";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.playvideo);
YouTubePlayerView youTubeView = (YouTubePlayerView)
findViewById(R.id.youtube_view);
youTubeView.initialize(DEVELOPER_KEY, this);
Bundle extras = getIntent().getExtras();
if (extras != null) {
String VIDEOs = extras.getString("videourl");
TextView text = (TextView) findViewById(R.id.youtube_text);
text.setText(VIDEOs);
}
}
@Override
public void onInitializationFailure(Provider provider,
YouTubeInitializationResult error) {
Toast.makeText(this, "Oh no! "+error.toString(),
Toast.LENGTH_LONG).show();
}
@Override
public void onInitializationSuccess(Provider provider, YouTubePlayer player,
boolean wasRestored) {
/* Bundle extras = getIntent().getExtras();
if (extras != null) {
String VIDEOs = extras.getString("videourl");
TextView text = (TextView) findViewById(R.id.youtube_text);
text.setText(VIDEOs);*/
player.loadVideo(VIDEO);
}
}
Upvotes: 0
Views: 4177
Reputation: 1
I think I know what are you asking. I have the same problem. You can make the value in a string like this for example: String url = getItem(pos).getLink(); I received the position and link and i made a string.
Here is my code example:
viewHolder.btnDownload = (Button) row.findViewById(R.id.btnDownload);
viewHolder.btnDownload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//getItemId(pos);
String url = getItem(position).getLink();
Upvotes: 0
Reputation: 3140
You can try this:
Intent intent = new Intent(CustomizedListView.this, PlayVideo.class);
intent.putExtra("ID",KEY_ID);
startActivity(intent);
and get it in PlayVideo.java like this
String Key_id = getIntent().getExtras().getString("ID");
now you have one query that how to get ID from list. then i would suggest that give your view that is in custom adapter class a tag which is your ID of video so that when you click on list it will give you clicked view where you can get tag of view so ultimately you will get ID of video. so that you can pass that video tag to another activity. just like this:
vi.setTag(song.get(CustomizedListView.KEY_ID));
in lazy loader class in getView() method. and then get tag in click event of list like this:
String tag = view.getTag().toString();
Hope it Helps!!
Upvotes: 1
Reputation: 4382
put this code at your CustomizedListView class
Intent intent = new Intent(CustomizedListView.this, PlayVideo.class);
truckIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
truckIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
truckIntent.putExtra(KEY_VID_ID, ID);
startActivity(intent);
put following code at your playvideo.class
String Key_id = getIntent().getExtras().getString("ID");
Upvotes: 0
Reputation: 2340
In first Activity
Intent i = new Intent(FirstActivity.this);
i.putExtra("STRING_I_NEED", strName);
i.startActivity();
In second activity
Intent n = getIntent();
String newString= extras.getString("STRING_I_NEED");
Upvotes: 0
Reputation: 3268
A good way to get ID of the videos from XML is adding the tag attribute into your items ( android:tag="v=ID"
). So, modify the ItemClickListener to:
// Click event for single list row
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent intent = new Intent(CustomizedListView.this, PlayVideo.class);
//Verify if we have the ID and then send to another activity.
if(view.getTag() != null)
intent.putExtra("id", "" + view.getTag());
// intent.setData(Uri.parse());
startActivity(intent);
}
});
And then, in you PlayVideo.class, in onCreate method you can have the Id doing:
String id = null;
Bundle extras = intent.getExtras()
if(extras != null && extras.containsKey("id"))
id = extras.getString("id");
Now the string id will contain the id you want.
Upvotes: 0
Reputation: 4114
Add the following code into your onItemClick method
Intent intent = new Intent(CustomizedListView.this, PlayVideo.class);
intent.putExtra("videoId", id);
startActivity(intent );
Upvotes: 0