AsfanUlla

Loading videos

Sep 25th, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.66 KB | None | 0 0
  1. package ga.asfanulla.openvid2.fragments;
  2.  
  3. import android.app.Activity;
  4. import android.app.ProgressDialog;
  5. import android.content.Intent;
  6. import android.os.Bundle;
  7. import android.support.v4.app.Fragment;
  8. import android.view.LayoutInflater;
  9. import android.view.View;
  10. import android.view.ViewGroup;
  11. import android.widget.AdapterView;
  12. import android.widget.ArrayAdapter;
  13. import android.widget.ListView;
  14. import android.widget.TextView;
  15. import android.widget.Toast;
  16.  
  17. import com.android.volley.RequestQueue;
  18. import com.android.volley.Response;
  19. import com.android.volley.VolleyError;
  20. import com.android.volley.toolbox.StringRequest;
  21. import com.android.volley.toolbox.Volley;
  22.  
  23. import org.json.JSONArray;
  24. import org.json.JSONException;
  25. import org.json.JSONObject;
  26.  
  27. import java.util.ArrayList;
  28. import java.util.HashMap;
  29. import java.util.List;
  30.  
  31. import ga.asfanulla.openvid2.R;
  32. import ga.asfanulla.openvid2.VideoActivity;
  33.  
  34. import static ga.asfanulla.openvid2.Constants.FIRST_COLUMN;
  35.  
  36.  
  37. public class OneFragment extends Fragment {
  38.  
  39.     private final String JSONUrl = "http://codetest.000webhostapp.com/samplvid/data.php?cat-1";
  40.     private final String TAG_VIDEOS = "cat1";
  41.     private final String TAG_URL = "video_url";
  42.     private final String TAG_TITLE = "video_title";
  43.  
  44.     private List<String> videoTitles = new ArrayList<String>();
  45.     private List<String> videoURLs = new ArrayList<String>();
  46.     private ArrayAdapter adapter;
  47.  
  48.     String name="";
  49.     String VidUrl="";
  50.  
  51.  
  52.     private ArrayList<HashMap<String, String>> list;
  53.     Activity activity;
  54.     TextView txtFirst;
  55.  
  56.     private ProgressDialog loading;
  57.  
  58.     public OneFragment() {
  59.         // Required empty public constructor
  60.     }
  61.  
  62.     @Override
  63.     public void onCreate(Bundle savedInstanceState) {
  64.         super.onCreate(savedInstanceState);
  65.         getData();
  66.  
  67.  
  68.     }
  69.  
  70.     private void getData() {
  71.  
  72.         loading = ProgressDialog.show(getActivity(),"Please wait...","Fetching...",false,false);
  73.  
  74.         String url = JSONUrl;
  75.  
  76.         StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
  77.             @Override
  78.             public void onResponse(String response) {
  79.                 loading.dismiss();
  80.                 showJSON2(response);
  81.             }
  82.         },
  83.                 new Response.ErrorListener() {
  84.                     @Override
  85.                     public void onErrorResponse(VolleyError error) {
  86.                         Toast.makeText(getActivity(),error.getMessage().toString(), Toast.LENGTH_LONG).show();
  87.                     }
  88.                 });
  89.  
  90.         RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
  91.         requestQueue.add(stringRequest);
  92.     }
  93.  
  94.     private void showJSON2(String response){
  95.  
  96.  
  97.  
  98.         try {
  99.             JSONObject jsonObject = new JSONObject(response);
  100.             JSONArray result = jsonObject.getJSONArray(TAG_VIDEOS);
  101.  
  102.             ListView listView = (ListView) getView().findViewById(R.id.listview);
  103.  
  104.             list = new ArrayList<HashMap<String, String>>();
  105.             for (int i=0; i<result.length(); i++) {
  106.                 JSONObject notice = result.getJSONObject(i);
  107.                 name = notice.getString(TAG_TITLE);
  108.                 VidUrl = notice.getString(TAG_URL);
  109.  
  110.                 HashMap<String, String> temp= new HashMap<String, String>();
  111.                 temp.put(FIRST_COLUMN, name);
  112.                 temp.put(SECOND_COLUMN, VidUrl);
  113.                 list.add(temp);
  114.  
  115.             }
  116.  
  117.             ListViewAdapters adapter = new ListViewAdapters(getActivity(), list);
  118.             listView.setAdapter(adapter);
  119.  
  120.             listView.setOnItemClickListener(new AdapterView.OnItemClickListener()
  121.             {
  122.                 @Override
  123.                 public void onItemClick(AdapterView<?> parent, final View view, int position, long id)
  124.                 {
  125.                     int pos=position+1;
  126.                    // Toast.makeText(getActivity(), Integer.toString(pos)+" Clicked", Toast.LENGTH_SHORT).show();
  127.                     Intent intent = new Intent(getActivity(), VideoActivity.class);
  128.                     intent.putExtra("videoUrl", VidUrl);
  129.                     intent.putExtra("videoTitle", FIRST_COLUMN);
  130.                     startActivity(intent);
  131.                 }
  132.  
  133.             });
  134.  
  135.         } catch (JSONException e) {
  136.             e.printStackTrace();
  137.         }
  138.  
  139.  
  140.  
  141.     }
  142.  
  143.     @Override
  144.     public View onCreateView(LayoutInflater inflater, ViewGroup container,
  145.                              Bundle savedInstanceState) {
  146.         // Inflate the layout for this fragment
  147.         return inflater.inflate(R.layout.fragment_one, container, false);
  148.     }
  149.  
  150. }
Add Comment
Please, Sign In to add comment