Code Monkey home page Code Monkey logo

Comments (4)

akoscz avatar akoscz commented on August 10, 2024

Sure I can try to help you out. Share some code so i can see what you are doing.

from youtubeplaylist.

payam30 avatar payam30 commented on August 10, 2024

Hi again Sir,
It's your code with a very small modification:

package YoutubePackage;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.DecimalFormat;

import peyam.com.peyamtv.R;

/**
 * Created by peyam on 2015-08-06.
 */
public class PersianMovies extends Fragment {

    private static final String TAG = "YouTubeFragment";

    public static String YOUTUBE_PLAYLIST;
    private static final String PLAYLIST_KEY = "PLAYLIST_KEY";
    private GridView mGridView;
    private Playlist mPlaylist;
    private PlaylistAdapter mAdapter;
    private String[] videoId = null;
    private int i = 0;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {


        View root = inflater.inflate(R.layout.youtube_videos, container, false);
        mGridView = (GridView) root.findViewById(R.id.gvMovies1);

        new GetYouTubePlaylistAsyncTask() {
            @Override
            public void onPostExecute(JSONObject result) {


                if (result == null) return;

                handlePlaylistResult(result);
            }
        }.execute(YOUTUBE_PLAYLIST, null);


        return root;
    }


    private void initListAdapter(Playlist playlist) {


        mAdapter = new PlaylistAdapter(playlist);
        mGridView.setAdapter(mAdapter);




    }




    private void handlePlaylistResult(JSONObject result) {


        try {
            if (mPlaylist == null) {
                mPlaylist = new Playlist(result);
                initListAdapter(mPlaylist);
            }

            final Playlist.Page page = mPlaylist.addPage(result);

            // fetch all the video details for the current page of Playlist Items
            new GetYouTubeVideoAsyncTask() {

                @Override
                public void onPostExecute(JSONObject result) {


                    if (result == null) {
                        return;
                    }

                    try {
                        JSONArray resultItems = result.getJSONArray("items");
                        PlaylistItem playlistItem;
                        for (int i = 0; i < page.items.size(); i++) {
                            playlistItem = page.items.get(i);
                            playlistItem.video = new Video(resultItems.getJSONObject(i));
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                    // make sure the UI gets updated
                    mAdapter.notifyDataSetChanged();
                }
            }.execute(page);

            if (!mAdapter.setIsLoading(false)) {
                mAdapter.notifyDataSetChanged();
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }


    protected class PlaylistAdapter extends BaseAdapter {
        private final DecimalFormat formatter = new DecimalFormat("#,###,###");
        private final LayoutInflater mInflater;
        private Playlist mPlaylist;
        private boolean mIsLoading = false;


        PlaylistAdapter(Playlist playlist) {


            mPlaylist = playlist;
            mInflater = getLayoutInflater(null);
        }


        public boolean setIsLoading(boolean isLoading) {


            if (mIsLoading != isLoading) {
                mIsLoading = isLoading;
                notifyDataSetChanged();
                return true;
            }
            return false;
        }


        @Override
        public int getCount() {


            return mPlaylist.getCount() + (mIsLoading ? 1 : 0);
        }


        @Override
        public PlaylistItem getItem(int i) {


            return mPlaylist.getItem(i);
        }


        @Override
        public long getItemId(int i) {


            return i;
        }


        @Override
        public View getView(int position, View convertView, ViewGroup viewGroup) {


            if (mIsLoading && position == (getCount() - 1)) {
                return mInflater.inflate(R.layout.youtube_movies_loading, null, false);
            }

            ViewHolder viewHolder;

            if (convertView == null || convertView.getTag() == null) {

                viewHolder = new ViewHolder();
                convertView = mInflater.inflate(R.layout.custom_row_movies, null, false);

                viewHolder.title = (TextView) convertView.findViewById(R.id.title);
                viewHolder.thumbnail = (ImageView) convertView.findViewById(R.id.ivThumb);
                viewHolder.duration = (TextView) convertView.findViewById(R.id.duration);
                viewHolder.viewCount = (TextView) convertView.findViewById(R.id.view);
                convertView.setTag(viewHolder);
            }

            viewHolder = (ViewHolder) convertView.getTag();
            final PlaylistItem item = getItem(position);
            if (item.title.length() >= 20) {
                viewHolder.title.setText(item.title.substring(0, 21));
            } else {
                viewHolder.title.setText(item.title);
            }


            // Loading Image
            Picasso.with(getActivity())
                    .load(item.thumbnailUrl)
                    .into(viewHolder.thumbnail);

            // set the click listener to play the video

            viewHolder.thumbnail.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {


                    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.youtube.com/watch?v=" + item.videoId)));

                }
            });

            // create and set the click listener for both the share icon and share text
//            View.OnClickListener shareClickListener = new View.OnClickListener() {
//                @Override
//                public void onClick(View view) {
//
//
//                    Intent sendIntent = new Intent();
//                    sendIntent.setAction(Intent.ACTION_SEND);
//                    sendIntent.putExtra(Intent.EXTRA_SUBJECT, "Watch \"" + item.title + "\" on YouTube");
//                    sendIntent.putExtra(Intent.EXTRA_TEXT, "http://www.youtube.com/watch?v=" + item.videoId);
//                    sendIntent.setType("text/plain");
//                    startActivity(sendIntent);
//                }
//            };

            if (item.video != null) {
                // set the video duration text
                viewHolder.duration.setText(item.video.duration);
                // set the video statistics
                viewHolder.viewCount.setText(formatter.format(item.video.viewCount));

            }

            // get the next playlist page if we're at the end of the current page and we have another page to get
            final String nextPageToken = mPlaylist.getNextPageToken(position);
            if (!isEmpty(nextPageToken) && position == getCount() - 1) {
                new GetYouTubePlaylistAsyncTask() {
                    @Override
                    public void onPostExecute(JSONObject result) {


                        handlePlaylistResult(result);
                    }
                }.execute(YOUTUBE_PLAYLIST, nextPageToken);

                setIsLoading(true);
            }

            return convertView;
        }


        private boolean isEmpty(String s) {


            if (s == null || s.length() == 0) {
                return true;
            }
            return false;
        }


        class ViewHolder {
            ImageView thumbnail;
            TextView title;
            TextView duration;
            TextView viewCount;

        }

    }

}

from youtubeplaylist.

akoscz avatar akoscz commented on August 10, 2024

If I understand your question right, you want the click listener to be on the entire cell in your grid view. In which case you should be able to the click listener on the convertView object

            convertView = mInflater.inflate(R.layout.custom_row_movies, null, false);
            // set the click listener to play the video
            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.youtube.com/watch?v=" + item.videoId)));
                }
            });

from youtubeplaylist.

akoscz avatar akoscz commented on August 10, 2024

Take a look at the most recent version of the code. I've updated it with usage of the RecyclerView. You can easily modify it to show a grid view.

from youtubeplaylist.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.