Android 如何在单击时将特定视频url从片段动态发送到其他活动
我使用列表视图在一个片段中动态加载了三个视频,并设法将视频URL onclick发送到播放视频的其他活动,但这里的问题是无论我单击哪个视频,只有第一个视频URL的URL被传递 代码-Android 如何在单击时将特定视频url从片段动态发送到其他活动,android,Android,我使用列表视图在一个片段中动态加载了三个视频,并设法将视频URL onclick发送到播放视频的其他活动,但这里的问题是无论我单击哪个视频,只有第一个视频URL的URL被传递 代码- import static ga.asfanulla.openvid2.Constants.FIRST_COLUMN; public class OneFragment extends Fragment { private final String JSONUrl = "http://codetest.
import static ga.asfanulla.openvid2.Constants.FIRST_COLUMN;
public class OneFragment extends Fragment {
private final String JSONUrl = "http://codetest.000webhostapp.com/samplvid/data.php?cat-1";
private final String TAG_VIDEOS = "cat1";
private final String TAG_URL = "video_url";
private final String TAG_TITLE = "video_title";
private List<String> videoTitles = new ArrayList<String>();
private List<String> videoURLs = new ArrayList<String>();
private ArrayAdapter adapter;
String name="";
String VidUrl="";
private ArrayList<HashMap<String, String>> list;
Activity activity;
TextView txtFirst;
private ProgressDialog loading;
public OneFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getData();
}
private void getData() {
loading = ProgressDialog.show(getActivity(),"Please wait...","Fetching...",false,false);
String url = JSONUrl;
StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
loading.dismiss();
showJSON2(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getActivity(),error.getMessage().toString(), Toast.LENGTH_LONG).show();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
requestQueue.add(stringRequest);
}
private void showJSON2(String response){
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray result = jsonObject.getJSONArray(TAG_VIDEOS);
ListView listView = (ListView) getView().findViewById(R.id.listview);
list = new ArrayList<HashMap<String, String>>();
for (int i=0; i<result.length(); i++) {
JSONObject notice = result.getJSONObject(i);
name = notice.getString(TAG_TITLE);
VidUrl = notice.getString(TAG_URL);
HashMap<String, String> temp= new HashMap<String, String>();
temp.put(FIRST_COLUMN, name);
temp.put(SECOND_COLUMN, VidUrl);
list.add(temp);
}
ListViewAdapters adapter = new ListViewAdapters(getActivity(), list);
listView.setAdapter(adapter);
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", FIRST_COLUMN);
startActivity(intent);
}
});
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_one, container, false);
}
}
导入静态ga.asfanulla.openvid2.Constants.FIRST_列;
公共类OneFragment扩展了Fragment{
私有最终字符串JSONUrl=”http://codetest.000webhostapp.com/samplvid/data.php?cat-1";
私有最终字符串TAG_VIDEOS=“cat1”;
私有最终字符串TAG_URL=“video_URL”;
私有最终字符串TAG_TITLE=“video_TITLE”;
私有列表videoTitles=新的ArrayList();
私有列表videourl=newarraylist();
专用阵列适配器;
字符串名称=”;
字符串VidUrl=“”;
私有数组列表;
活动;
TextView txtFirst;
私有对话框加载;
公共OneFragment(){
//必需的空公共构造函数
}
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
getData();
}
私有void getData(){
loading=ProgressDialog.show(getActivity(),“请稍候…”,“正在获取…”,false,false);
字符串url=JSONUrl;
StringRequest StringRequest=新的StringRequest(url,new Response.Listener()){
@凌驾
公共void onResponse(字符串响应){
loading.dispose();
showJSON2(应答);
}
},
新的Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
Toast.makeText(getActivity(),error.getMessage().toString(),Toast.LENGTH_LONG).show();
}
});
RequestQueue RequestQueue=Volley.newRequestQueue(getActivity());
添加(stringRequest);
}
私有void showJSON2(字符串响应){
试一试{
JSONObject JSONObject=新JSONObject(响应);
JSONArray result=jsonObject.getJSONArray(TAG_视频);
ListView ListView=(ListView)getView().findViewById(R.id.ListView);
列表=新的ArrayList();
对于(int i=0;i问题:您没有将数据放入HashMap
,也没有VidUrl
放入map
HashMap<String, String> temp= new HashMap<String, String>();
temp.put(FIRST_COLUMN, name);
list.add(temp)
注意:您可以使用自己的POJO类创建自己的自定义适配器问题:您没有将数据放入HashMap
,也没有VidUrl
HashMap<String, String> temp= new HashMap<String, String>();
temp.put(FIRST_COLUMN, name);
list.add(temp)
注意:您可以使用自己的POJO类创建自己的自定义适配器您在哪里定义VidUrl
?@F43nd1r它从db加载并放入数组中,并使用listview适配器显示。您需要使用用作数据的数组或列表中的位置获取数据source@Pavneet_Singh我试过了,但没成功ion()似乎无法在碎片中工作参考我之前的评论和提示intent.putExtra(“videoUrl”,yourArray[position])
您在哪里定义VidUrl
?@F43nd1r它从数据库加载并放入数组中,并使用listview适配器显示。您需要使用数组或列表中用作数据的位置来获取数据source@Pavneet_Singh我试过了,但是得到了位置()似乎不完整请参考我之前的评论和提示intent.putExtra(“videoUrl”,yourArray[position]);
请您解释一下entry.getValue()
和entry.getKey()是什么
谢谢。我很抱歉代码不完整这里是完整的代码-哦,我完全忘记了,非常感谢你,也很抱歉给你添麻烦。我很高兴我能帮上忙,很高兴编码请你解释一下entry.getValue()
和entry.getKey()
谢谢。我很抱歉代码不完整。这是完整的代码-哦,完全忘记了。非常感谢您,也很抱歉给您带来麻烦。我很高兴能帮您,编码愉快
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);