1

I am trying to implement the custom list view in tabbed activity for youtube video playing app. I could not able to call custom listview adapter in fragment java class. I have tried to pass getActivity() to custom adapter but the app crashes. If any one know the solution please help me.

The java class are as follows:

HomeFragment.java

package layout;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.ArrayList;
import java.util.List;

import in.testapp.app1.R;

    public class HomeFragment extends Fragment {
        static class Adapter extends FragmentPagerAdapter {
            private final List<Fragment> mFragmentList = new ArrayList();
            private final List<String> mFragmentTitleList = new ArrayList();

            public Adapter(FragmentManager manager) {
                super(manager);
            }

            public Fragment getItem(int position) {
                return (Fragment) this.mFragmentList.get(position);
            }

            public int getCount() {
                return this.mFragmentList.size();
            }

            public void addFragment(Fragment fragment, String title) {
                this.mFragmentList.add(fragment);
                this.mFragmentTitleList.add(title);
            }

            public CharSequence getPageTitle(int position) {
                return (CharSequence) this.mFragmentTitleList.get(position);
            }
        }

        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setRetainInstance(true);
        }

        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.fragment_home, container, false);
            ViewPager viewPager = (ViewPager) view.findViewById(R.id.viewpager);
            setupViewPager(viewPager);
            ((TabLayout) view.findViewById(R.id.tablayout)).setupWithViewPager(viewPager);
            return view;
        }

        private void setupViewPager(ViewPager viewPager) {
            Adapter adapter = new Adapter(getChildFragmentManager());
            adapter.addFragment(new YouTubeFragment(), "Tab 1");
            adapter.addFragment(new SecondFragment(), "Tab 2");
            viewPager.setAdapter(adapter);
        }
    }

YouTubeFragment.java

package layout;

import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings.PluginState;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.RetryPolicy;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

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

import java.util.ArrayList;

import in.testapp.app1.AppUtils;
import in.testapp.app1.ChannelActivity;
import in.testapp.app1.CustomListAdapter;
import in.testapp.app1.R;
import in.testapp.app1.VideoDetails;

public class YouTubeFragment extends Fragment {
    String TAG="MainActivity2";
    ListView lvVideo;
    ArrayList<VideoDetails> videoDetailsArrayList;
    CustomListAdapter customListAdapter;
    //{youtube_api_key} is replaced by actual key
    String URL="https://www.googleapis.com/youtube/v3/search?part=snippet&channelId=UC5Eg6bkwsdCd-ZlcB3nt1dg&maxResults=25&key={youtube_api_key}";

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_you_tube, container, false);
        lvVideo=(ListView)view.findViewById(R.id.videoList);
        videoDetailsArrayList=new ArrayList<>();

        //App crashes here in the beolw line
        customListAdapter=new CustomListAdapter(getActivity(),videoDetailsArrayList);
        //App crashes here in the above line

        showVideo();
        return view;
    }
    private void showVideo() {
        RequestQueue requestQueue= Volley.newRequestQueue(getContext());
        StringRequest stringRequest=new StringRequest(Request.Method.GET, URL, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                try {
                    JSONObject jsonObject=new JSONObject(response);
                    JSONArray jsonArray=jsonObject.getJSONArray("items");
                    for(int i=1;i<jsonArray.length();i++){
                        JSONObject jsonObject1 = jsonArray.getJSONObject(i);
                        JSONObject jsonVideoId=jsonObject1.getJSONObject("id");
                        JSONObject jsonsnippet= jsonObject1.getJSONObject("snippet");
                        JSONObject jsonObjectdefault = jsonsnippet.getJSONObject("thumbnails").getJSONObject("medium");
                        VideoDetails videoDetails=new VideoDetails();

                        String videoid=jsonVideoId.getString("videoId");

                        Log.e(TAG," New Video Id" +videoid);
                        videoDetails.setURL(jsonObjectdefault.getString("url"));
                        videoDetails.setVideoName(jsonsnippet.getString("title"));
                        videoDetails.setVideoDesc(jsonsnippet.getString("description"));
                        videoDetails.setVideoId(videoid);

                        videoDetailsArrayList.add(videoDetails);
                    }
                    lvVideo.setAdapter(customListAdapter);
                    customListAdapter.notifyDataSetChanged();
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        });
        int socketTimeout = 30000;
        RetryPolicy policy = new DefaultRetryPolicy(socketTimeout, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
        stringRequest.setRetryPolicy(policy);
        requestQueue.add(stringRequest);

    }
}

CustomListAdapter.java

package in.testapp.app1;
import android.app.Activity;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import java.util.ArrayList;
public class CustomListAdapter extends BaseAdapter {
    Activity activity;
    ImageLoader imageLoader = AppController.getInstance().getImageLoader();
    private LayoutInflater inflater;
    ArrayList<VideoDetails> singletons;
    public CustomListAdapter(Activity activity, ArrayList<VideoDetails> singletons) {
        this.activity = activity;
        this.singletons = singletons;
    }
    public int getCount() {
        return this.singletons.size();
    }
    public Object getItem(int i) {
        return this.singletons.get(i);
    }
    public long getItemId(int i) {
        return (long) i;
    }
    public View getView(int i, View convertView, ViewGroup viewGroup) {
        if (this.inflater == null) {
            this.inflater = (LayoutInflater) this.activity.getLayoutInflater();
                   // getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }
        if (convertView == null) {
            convertView = this.inflater.inflate(R.layout.videolist, null);
        }
        if (this.imageLoader == null) {
            this.imageLoader = AppController.getInstance().getImageLoader();
        }
        NetworkImageView networkImageView = (NetworkImageView) convertView.findViewById(R.id.video_image);
        final TextView imgtitle = (TextView) convertView.findViewById(R.id.video_title);
        final TextView imgdesc = (TextView) convertView.findViewById(R.id.video_descriptio);
        final TextView tvURL=(TextView)convertView.findViewById(R.id.tv_url);
        final  TextView tvVideoID=(TextView)convertView.findViewById(R.id.tv_videoId);
       ((LinearLayout) convertView.findViewById(R.id.asser)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(view.getContext(), VideoActivity.class);
                intent.putExtra("videoId",tvVideoID.getText().toString());
                view.getContext().startActivity(intent);
            }
        });
        VideoDetails singleton = (VideoDetails) this.singletons.get(i);
        networkImageView.setImageUrl(singleton.getURL(), this.imageLoader);
        tvVideoID.setText(singleton.getVideoId());
        imgtitle.setText(singleton.getVideoName());
        imgdesc.setText(singleton.getVideoDesc());
        return convertView;
    }
}

VideoDeatils.java

package in.testapp.app1;

public class VideoDetails {
    String VideoName;
    String VideoDesc;
    String URL;
    String VideoId;


public void setVideoName(String VideoName){
    this.VideoName=VideoName;
}

public String getVideoName(){
    return VideoName;
}

public void setVideoDesc(String VideoDesc){
    this.VideoDesc=VideoDesc;
}

public String getVideoDesc(){
    return VideoDesc;
}

public void setURL(String URL){
    this.URL=URL;
}

public String getURL(){
    return URL;
}

public void setVideoId(String VideoId){
    this.VideoId=VideoId;
}
public String getVideoId(){
    return VideoId;
}

}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Kishan K
  • 21
  • 1
  • 6
  • Can you share error log? – Ramesh sambu Dec 27 '17 at 13:57
  • @Kishan K Off topic - have you tried using recyclerview, there are some good tutorials and explanations. It is the future (present) of lists, it reuses cells, allows for great animations, and decouples list from its container. I have used listview for a while in the past, but recyclerview is something I am glad to have started using. – Banana Dec 27 '17 at 14:03
  • I beginner to android. I am using android studio to build the app. where to see error log. – Kishan K Dec 27 '17 at 14:10
  • see in logcat and copy paste the error or crash report – Mukeshkumar S Dec 27 '17 at 14:29

0 Answers0