我有一个视频列表,在每个视频视图中我都有一个图标,我试图在用户点击图标时将其添加到另一个活动(发送视频的数据),问题是,我需要从另一个方法(从OnClick到OnItemClick)调用一个方法,我不能,因为我没有我需要的所有参数。
的OnClick:
@Override
public void onClick(View v) {
switch (v.getId()){
//Case the search button was clicked.
case R.id.yt_video_btn:
try {
final String keyWord = mVideoEdt.getText().toString().trim();
hideKeyboard();
if (keyWord.length() > 0) {
AppUtils.showToast(AppConstants.SEARCH_VIDEO_MSG);
AppUtils.showToast(AppConstants.DIALOG_TITLE);
mServiceTask = new ServiceTask(SEARCH_VIDEO);
mServiceTask.setmServerResponseListener(this);
mServiceTask.execute(new Object[]{keyWord});
} else {
AppUtils.showToast("Empty filed");
}
}catch (Exception e){}
break;
//TODO: when add_favorite clicked, send details of video to Favorites activity, and show it there.
//Case the star symbol was clicked.
case R.id.add_favorite:
AppUtils.showToast("Added to favorites");
}
}
OnItemClick:
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
SearchResult result = (SearchResult)parent.getItemAtPosition(position);
VIDEO_ID = result.getId().getVideoId();
Intent videoIntent = YouTubeStandalonePlayer.createVideoIntent(this, AppConstants.KEY, VIDEO_ID);
startActivity(videoIntent);
}
如果onClick中的案例2被激活,我需要从onItemClick发送SearchResult。
希望你们理解我,对不起英语不好并提前感谢!
信息搜索结果:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder mHolder;
if(convertView != null){
mHolder = (ViewHolder)convertView.getTag();
}else{
mHolder = new ViewHolder();
convertView = mLayoutInflater.inflate(R.layout.view_video_item,null);
mHolder.mVideoThumbnail = (ImageView)convertView.findViewById(R.id.video_thumbnail);
mHolder.mVideoTitle = (TextView)convertView.findViewById(R.id.video_title);
convertView.setTag(mHolder);
}
//Setting the data
SearchResult result = mVideoList.get(position);
mHolder.mVideoTitle.setText(result.getSnippet().getTitle());
//Loading the image
Picasso.with(mActivity).load(result.getSnippet().getThumbnails().getMedium().getUrl()).into(mHolder.mVideoThumbnail);
return convertView;
}
答案 0 :(得分:2)
头等
Intent intent = new Intent(MainCativity.this, SecondActivity.class);
i.putExtra("name", "Xyz");
第二课
Bundle bundle = getIntent().getExtras();
String name= bundle.getString("name");
答案 1 :(得分:0)
这已在此处提出:How do I pass data between Activities in Android application?
在Activites之间传递数据是通过意图通过向其添加数据来完成的。
在onClickListener中可以像这样使用它来打开新活动。
传递数据
Intent i = new Intent(ctx, Activity.class);
i.putExtra("data", "somedata");
然后,您可以通过它的
检索新活动中的数据检索数据
Bundle bundle = getIntent().getExtras();
//check if the bundle is != null
if (bundle != null) {
String data = bundle.getString("data");
}
通过意图传递对象
您的SearchResult对象必须实现Serializable 。这只适用于Strings,Ints,...如果这也包含复杂的数据类型,你应该看一下Parcable接口。
i.putExtra("searchResult", searchResult);
如果您有片段,您也可以通过Bundle将数据传递给它。对于可序列化的对象,这样做:
Bundle bundle = new Bundle();
bundle.putSerializable("data", this);
从捆绑中获取对象
Bundle bundle = getIntent().getExtras();
//check if the bundle is != null
if (bundle != null) {
SearchResult searchResult = (SearchResult) bundle.getSerializable("searchResult");
}