Asfan Ulla Asfan Ulla - 27 days ago 6
Android Question

How to dynamically send particular video url from fragment to other activity onclick

I loaded three videos dynamically in a Fragment using list view and managed to send the video URL onclick to other activity where the video is played but the issue here is no matter which video I click the URL of only the first video URL gets passed.

Code -

listView.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, final View view, int position, long id)
{
int pos=position+1;
// Toast.makeText(getActivity(), Integer.toString(pos)+" Clicked", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getActivity(), VideoActivity.class);
intent.putExtra("videoUrl", VidUrl);
intent.putExtra("videoTitle", name);
startActivity(intent);
}

});

Answer Source

Issue: you are not putting data in HashMap , no VidUrl in map

HashMap<String, String> temp= new HashMap<String, String>();
temp.put(FIRST_COLUMN, name);
list.add(temp)

so do

JSONObject notice = result.getJSONObject(i);
name = notice.getString(TAG_TITLE);
VidUrl = notice.getString(TAG_URL);
// no VidUrl in hashmap so do
HashMap<String, String> temp= new HashMap<String, String>();
temp.put(name,VidUrl);
list.add(temp);

later fetch the HashMap and then fetch the value

Map.Entry<String,String> entry=list.get(position).entrySet().iterator().next();
//Fetch hashmap object using position ^^^^^^^^^^
// entryset will return list of all entries as Map.Entry
// fetch first entry using iterator().next()
// where via Entry we can access both key and value directly

Intent intent = new Intent(getActivity(), VideoActivity.class);
// i assume your value is the url and key is the title
intent.putExtra("videoUrl", entry.getValue());
intent.putExtra("videoTitle", entry.getKey());
startActivity(intent);

Note : you can create your own customize adapter using Your own POJO class