Skip to content

Instantly share code, notes, and snippets.

@amiraelsayedbadwy
Created April 8, 2016 16:42
Show Gist options
  • Save amiraelsayedbadwy/6076f211a9dd62ac4b28d05a62bc864d to your computer and use it in GitHub Desktop.
Save amiraelsayedbadwy/6076f211a9dd62ac4b28d05a62bc864d to your computer and use it in GitHub Desktop.
moviefragmnet
package com.example.amira.movies;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
/**
* A placeholder fragment containing a simple view.
*/
public class MovieFragment extends Fragment {
GridView grid;
String fetchmovies = null;
customadpater adapter;
ArrayList<String> myStringList = new ArrayList<>();
String []ress;
String []res1;
String []res2;
Double []res3;
private ArrayAdapter<String> Movies;
public MovieFragment() {
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Add this line in order for this fragment to handle menu events.
setHasOptionsMenu(true);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.movies, menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_refresh) {
new TaskManag().execute();
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
// Get a reference to the ListView, and attach this adapter to it.
grid = (GridView) rootView.findViewById(R.id.gridView1);
grid.setAdapter(adapter);
grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
Intent intent = new Intent(getActivity(), Details.class)
.putExtra("items", position);
// study intent //array[postion]
intent.putExtra("over",ress[position]);
intent.putExtra("title",res1[position]);
intent.putExtra("rate",res3[position]);
intent.putExtra("date",res2[position]);
intent.putExtra("image",myStringList);
startActivity(intent);
}
});
return rootView;
}
private void updatemovie() {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
}
@Override
public void onStart() {
super.onStart();
updatemovie();
super.onStart();
TaskManag taskManag = new TaskManag();
taskManag.execute();
}
public class TaskManag extends AsyncTask<Void, Void, ArrayList<String>> {
@Override
protected ArrayList<String> doInBackground(Void... params) {
HttpURLConnection urlConnection = null;
BufferedReader reader = null;
try {
String sortType = "popular";
String topRated = "top_rated";
String api_key = "da84c4afea059e8ae06f74c450ea8793";
sortType = "popular";
final String Movie_URL =
"https://api.themoviedb.org/3/movie/" + sortType + "?api_key=" + api_key;
URL url = new URL(Movie_URL);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.connect();
// Read the input stream into a String
InputStream inputStream = urlConnection.getInputStream();
StringBuffer buffer = new StringBuffer();
if (inputStream == null) {
// Nothing to do.
return null;
}
reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while ((line = reader.readLine()) != null) {
buffer.append(line + "\n");
}
if (buffer.length() == 0) {
// Stream was empty. No point in parsing.
return null;
}
fetchmovies = buffer.toString();
} catch (IOException e) {
return null;
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
if (reader != null) {
try {
reader.close();
} catch (final IOException e) {
// Log.e(LOG_TAG, "Error closing stream", e);
}
}
}
try {
Log.e("TAG", fetchmovies);
return Getmovies(fetchmovies);
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
String [] res=new String[100];
public ArrayList<String> Getmovies(String mo)
throws JSONException {
final String RESULTS = "results";
final String POSTER_PATH = "poster_path";
final String OVERVIEW = "overview";
final String RELEASE_DATE = "release_date";
final String ORIGINAL_TITLE = "original_title";
final String VOTE_AVG = "vote_average";
JSONObject obj = new JSONObject(mo);
JSONArray jsonArray = obj.getJSONArray(RESULTS);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject movieData = jsonArray.getJSONObject(i);
String moviePosterPath = movieData.getString(POSTER_PATH);
ress=new String[jsonArray.length()];
res1=new String[jsonArray.length()];
res2=new String[jsonArray.length()];
res3=new Double[jsonArray.length()];
res1[i] = movieData.getString(ORIGINAL_TITLE);/// kol wa7da leha rayy bet3ha le wa7dha
ress[i] = movieData.getString(OVERVIEW);
res2[i]= movieData.getString(RELEASE_DATE);
res3[i] = movieData.getDouble(VOTE_AVG);
myStringList.add(moviePosterPath);
}
return myStringList;
}
@Override
protected void onPostExecute(ArrayList<String> posterPaths) {
if (posterPaths != null) {
adapter = new customadpater(getActivity(), posterPaths);
grid.setAdapter(adapter);
// grid.setAdapter(adapter);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment