-4

I have a list of videos and on each video view I have an icon, I'm trying to make that when the user clicks on the icon, It would add the video to another activity (Sending the video's data), the problem is that for that I need to call a method from another method (From OnClick to OnItemClick) and I can't because I don't have all the parameters I need.

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);
}

I need to send the SearchResult from onItemClick if case 2 in onClick is activated.

Hope you guys understood me, sorry for bad English and thanks in advance!

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;
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
John Doe
  • 93
  • 2
  • 8
  • possible duplicate of [How do I pass data between activities in Android?](http://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-in-android) – mikepenz May 07 '15 at 10:08

2 Answers2

1

First class

Intent intent = new Intent(MainCativity.this, SecondActivity.class);
i.putExtra("name", "Xyz");

Second Class

Bundle bundle = getIntent().getExtras();
String name= bundle.getString("name");
  • Thanks, Could you just give me example in my code? I thought about, when icon is clicked, call onClick, in onClick flag is true, in onItemClick, if flag is true, send the SearchResult. but I couldn't really figure out how ( I tired what you gave me) – John Doe May 07 '15 at 10:09
0

This was already asked here: How do I pass data between Activities in Android application?

Passing data between Activites is done via the intent by adding data to it.

This can be cone like this inside your onClickListener which opens the new activity.

Pass Data

Intent i = new Intent(ctx, Activity.class);
i.putExtra("data", "somedata");

You can then retrieve the data inside the new Activity via it's bundle

Retrieve Data

Bundle bundle = getIntent().getExtras();
//check if the bundle is != null
if (bundle != null) {
    String data = bundle.getString("data");
}

Pass an Object via Intent

Your SearchResult object have to implements Serializable. This will only work for Strings, Ints, ... If this also contains complex data types you should take a look at the Parcable interface.

i.putExtra("searchResult", searchResult);

If you have an fragment you can also pass data to it via a Bundle. For a serializable object this is done like this:

Bundle bundle = new Bundle();
bundle.putSerializable("data", this);

Get Object from Bundle

Bundle bundle = getIntent().getExtras();
//check if the bundle is != null
if (bundle != null) {
    SearchResult searchResult = (SearchResult) bundle.getSerializable("searchResult");
}
Community
  • 1
  • 1
mikepenz
  • 12,708
  • 14
  • 77
  • 117
  • Thanks, Could you just give me example in my code? I thought about, when icon is clicked, call onClick, in onClick flag is true, in onItemClick, if flag is true, send the SearchResult. but I couldn't really figure out how ( I tired what you gave me) – John Doe May 07 '15 at 10:11
  • @JohnDoe you probably improve your question then. If you click on the icon you can start the second activity too? What's your main issue. Do you have issues to start the activity? To pass any data? Or do get the data from search? – mikepenz May 07 '15 at 10:14
  • I'm opening a Youtubestandalone intent, and before that I want to pass the SearchResult to Favorites intent. – John Doe May 07 '15 at 10:14
  • I'm having issue to pass the data. I have a view with an icon. The view itself is clickable and if you click on it, it would start a video. but If you click on the icon it would add the video to the favorites. I'm having trouble to send the SearchResult to the Favorites activity after clicking the icon. – John Doe May 07 '15 at 10:16
  • @JohnDoe you want to pass the whole object? ah now i see what's your problem – mikepenz May 07 '15 at 10:17
  • @JohnDoe please add the SearchResult object. It has to be Serializable so you can add it to the intent – mikepenz May 07 '15 at 10:19
  • Yes, I added the method where I get the SearchResult and setting the video details I want. – John Doe May 07 '15 at 10:25
  • @JohnDoe i've added some more information to my answer – mikepenz May 07 '15 at 10:25