Advertisement
III-sonic

myctalog

Nov 20th, 2017
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.73 KB | None | 0 0
  1. package com.skmobile.catalogmovie;
  2.  
  3. import android.app.LoaderManager;
  4. import android.content.Loader;
  5. import android.support.v7.app.AppCompatActivity;
  6. import android.os.Bundle;
  7. import android.text.TextUtils;
  8. import android.view.View;
  9. import android.widget.Button;
  10. import android.widget.EditText;
  11. import android.widget.ListView;
  12.  
  13. import java.util.ArrayList;
  14.  
  15. public class MainActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<ArrayList<MovieItems>>{
  16.  
  17. ListView listView;
  18. MovieThread thread;
  19. EditText editTitle;
  20. Button btnCari;
  21.  
  22. static final String EXTRAS_TITLE = "EXTRAS_TITLE";
  23.  
  24. @Override
  25. protected void onCreate(Bundle savedInstanceState) {
  26. super.onCreate(savedInstanceState);
  27. setContentView(R.layout.activity_main);
  28. this.setTitle(" Catalogue Movie");
  29.  
  30. getSupportActionBar().setDisplayShowHomeEnabled(true);
  31. getSupportActionBar().setLogo(R.drawable.ic_movie_black_24dp);
  32. getSupportActionBar().setDisplayUseLogoEnabled(true);
  33.  
  34. thread = new MovieThread(this);
  35. thread.notifyDataSetChanged();
  36. listView = (ListView)findViewById(R.id.list_view);
  37.  
  38. listView.setAdapter(thread);
  39.  
  40. editTitle = (EditText)findViewById(R.id.edt_search);
  41. btnCari = (Button)findViewById(R.id.btn_search);
  42.  
  43. btnCari.setOnClickListener(myListener);
  44.  
  45. String title = editTitle.getText().toString();
  46. Bundle bundle = new Bundle();
  47. bundle.putString(EXTRAS_TITLE, title);
  48.  
  49. getLoaderManager().initLoader(0, bundle, this);
  50.  
  51. }
  52.  
  53. @Override
  54. public Loader<ArrayList<MovieItems>> onCreateLoader(int id, Bundle args) {
  55.  
  56. String kumpulanTitle = "";
  57. if (args != null ){
  58. kumpulanTitle = args.getString(EXTRAS_TITLE);
  59. }
  60.  
  61. return new MyTaskMovie(this , kumpulanTitle);
  62. }
  63.  
  64. @Override
  65. public void onLoadFinished(Loader<ArrayList<MovieItems>> loader, ArrayList<MovieItems> data) {
  66. thread.setData(data);
  67.  
  68. }
  69.  
  70. @Override
  71. public void onLoaderReset(Loader<ArrayList<MovieItems>> loader) {
  72. thread.setData(null);
  73.  
  74. }
  75.  
  76. View.OnClickListener myListener = new View.OnClickListener() {
  77. @Override
  78. public void onClick(View v) {
  79. String title = editTitle.getText().toString();
  80.  
  81. if (TextUtils.isEmpty(title))return;
  82.  
  83. Bundle bundle = new Bundle();
  84. bundle.putString(EXTRAS_TITLE, title);
  85. getLoaderManager().restartLoader(0, bundle, MainActivity.this);
  86. }
  87. };
  88. }
  89.  
  90.  
  91. ================================================================================================
  92. package com.skmobile.catalogmovie;
  93.  
  94. import org.json.JSONException;
  95. import org.json.JSONObject;
  96.  
  97. /**
  98. * Created by ADMIN on 19/11/2017.
  99. */
  100.  
  101. public class MovieItems {
  102.  
  103. private String Title;
  104. private String image;
  105. private String discripsi;
  106. private String date;
  107.  
  108. private String AltTitle;
  109.  
  110.  
  111. public MovieItems(JSONObject object) {
  112. try {
  113.  
  114. String title = object.getJSONArray("change_keys").getJSONObject(0).getString("title");
  115. String AltTitle = object.getJSONArray("change_keys").getJSONObject(0).getString("alternative_title");
  116. String image = object.getJSONObject("images").getString("base_url");
  117. String description = object.getJSONArray("change_keys").getJSONObject(0).getString("biography");
  118. String date = object.getJSONArray("change_keys").getJSONObject(0).getString("air_date");
  119.  
  120. this.Title = title;
  121. this.AltTitle = AltTitle;
  122. this.discripsi = description;
  123. this.date = date;
  124. this.image = image;
  125.  
  126.  
  127.  
  128. } catch (JSONException e) {
  129. e.printStackTrace();
  130. }
  131. }
  132.  
  133. public String getTitle() {
  134. return Title;
  135. }
  136.  
  137. public void setTitle(String title) {
  138. Title = title;
  139. }
  140. public String getImage() {
  141. return image;
  142. }
  143.  
  144. public void setImage(String image) {
  145. this.image = image;
  146. }
  147.  
  148. public String getDiscripsi() {
  149. return discripsi;
  150. }
  151.  
  152. public void setDiscripsi(String discripsi) {
  153. this.discripsi = discripsi;
  154. }
  155.  
  156. public String getDate() {
  157. return date;
  158. }
  159.  
  160. public void setDate(String date) {
  161. this.date = date;
  162. }
  163.  
  164. public String getAltTitle() {
  165. return AltTitle;
  166. }
  167.  
  168. public void setAltTitle(String altTitle) {
  169. AltTitle = altTitle;
  170. }
  171.  
  172.  
  173. }
  174.  
  175. ==================================================
  176. package com.skmobile.catalogmovie;
  177.  
  178. import android.content.Context;
  179. import android.view.LayoutInflater;
  180. import android.view.View;
  181. import android.view.ViewGroup;
  182. import android.widget.BaseAdapter;
  183. import android.widget.RelativeLayout;
  184. import android.widget.TextView;
  185.  
  186. import com.bumptech.glide.Glide;
  187.  
  188. import java.util.ArrayList;
  189.  
  190. import de.hdodenhof.circleimageview.CircleImageView;
  191.  
  192. /**
  193. * Created by ADMIN on 19/11/2017.
  194. */
  195.  
  196. public class MovieThread extends BaseAdapter {
  197.  
  198. private ArrayList<MovieItems> mDataMovie = new ArrayList<>();
  199. private LayoutInflater mInflater;
  200. private Context context;
  201.  
  202. public MovieThread (Context context){
  203. this.context = context;
  204. mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  205.  
  206. }
  207.  
  208. public void setData(ArrayList<MovieItems> items) {
  209. mDataMovie = items;
  210. notifyDataSetChanged();
  211. }
  212. public void addItem(final MovieItems item){
  213. mDataMovie.add(item);
  214. notifyDataSetChanged();
  215. }
  216. public void clearData(){
  217.  
  218. mDataMovie.clear();
  219. }
  220.  
  221. @Override
  222. public int getItemViewType(int position) {
  223. return 0;
  224. }
  225. @Override
  226. public int getViewTypeCount() {
  227. return 1;
  228. }
  229.  
  230. @Override
  231. public int getCount() {
  232. return mDataMovie.size();
  233. }
  234.  
  235. @Override
  236. public MovieItems getItem(int position) {
  237. return mDataMovie.get(position);
  238. }
  239.  
  240. @Override
  241. public long getItemId(int position) {
  242. return position;
  243. }
  244.  
  245. @Override
  246. public View getView(int position, View convertView, ViewGroup parent) {
  247. ViewHolder holder = null;
  248. if (convertView == null) {
  249. holder = new ViewHolder();
  250. convertView = mInflater.inflate(R.layout.movie_list, null);
  251. holder.textViewTitle = (TextView)convertView.findViewById(R.id.tv_item_title);
  252. holder.textViewDescription = (TextView)convertView.findViewById(R.id.tv_item_discripsi);
  253. holder.textViewDate = (TextView)convertView.findViewById(R.id.tv_item_date);
  254. holder.ViewImage = (CircleImageView) convertView.findViewById(R.id.img_item_image);
  255. Glide.with(context).load("http://image.tmdb.org/t/p/w92/" + mDataMovie.get(position).getImage()).into(holder.ViewImage);
  256.  
  257. convertView.setTag(holder);
  258.  
  259. }else {
  260. holder = (ViewHolder)convertView.getTag();
  261. }
  262. holder.textViewTitle.setText(mDataMovie.get(position).getTitle());
  263. holder.textViewDescription.setText(mDataMovie.get(position).getDiscripsi());
  264. holder.textViewDate.setText(mDataMovie.get(position).getDate());
  265. holder.ViewImage.setImageResource(R.drawable.ic_not_interested_black_48dp);
  266. return convertView;
  267. }
  268.  
  269. private static class ViewHolder{
  270. TextView textViewTitle;
  271. CircleImageView ViewImage;
  272. TextView textViewDescription;
  273. TextView textViewDate;
  274. RelativeLayout relativeLayout;
  275.  
  276.  
  277. }
  278. }
  279. =====================================================
  280.  
  281. package com.skmobile.catalogmovie;
  282.  
  283. import android.content.AsyncTaskLoader;
  284. import android.content.Context;
  285. import android.util.Log;
  286. import android.os.Build;
  287.  
  288. import com.loopj.android.http.AsyncHttpResponseHandler;
  289. import com.loopj.android.http.SyncHttpClient;
  290.  
  291. import org.json.JSONArray;
  292. import org.json.JSONObject;
  293.  
  294. import java.util.ArrayList;
  295.  
  296. import cz.msebera.android.httpclient.Header;
  297.  
  298. /**
  299. * Created by ADMIN on 19/11/2017.
  300. */
  301.  
  302. public class MyTaskMovie extends AsyncTaskLoader<ArrayList<MovieItems>> {
  303.  
  304. private ArrayList<MovieItems> mMovieData;
  305. private boolean mResult;
  306. private String mKumpulanMovie;
  307.  
  308. public MyTaskMovie(final Context context, String KumpulanMovie){
  309. super(context);
  310.  
  311. onContentChanged();
  312. this.mKumpulanMovie = KumpulanMovie;
  313. }
  314.  
  315. @Override
  316. protected void onStartLoading(){
  317. if (takeContentChanged())
  318. forceLoad();
  319. else if (mResult)
  320. deliverResult(mMovieData);
  321. }
  322.  
  323. @Override
  324. public void deliverResult(final ArrayList<MovieItems> data){
  325. mMovieData = data;
  326. mResult = true;
  327. super.deliverResult(data);
  328. }
  329. @Override
  330. protected void onReset(){
  331. super.onReset();
  332. onStopLoading();
  333. if(mResult){
  334. onReleaseResources(mMovieData);
  335. mMovieData = null;
  336. mResult = false;
  337. }
  338. }
  339.  
  340. private static final String API_KEY = "e12d991c84852d5502f6066cfb2689bc";
  341.  
  342.  
  343. @Override
  344. public ArrayList<MovieItems> loadInBackground() {
  345. SyncHttpClient client = new SyncHttpClient();
  346.  
  347. final ArrayList<MovieItems> movieItemses = new ArrayList<>();
  348. String url = "https://api.themoviedb.org/3/search/movie?api_key="+API_KEY+"&language=en-US&query="+mKumpulanMovie;
  349.  
  350. client.get(url, new AsyncHttpResponseHandler() {
  351. @Override
  352. public void onStart(){
  353. super.onStart();
  354. setUseSynchronousMode(true);
  355. }
  356.  
  357. @Override
  358. public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
  359. try {
  360. String result = new String(responseBody);
  361. JSONObject responeseObject = new JSONObject(result);
  362. JSONArray list = responeseObject.getJSONArray("list");
  363.  
  364. for (int i = 0; i < list.length(); i++){
  365. JSONObject movie = list.getJSONObject(i);
  366. MovieItems movieItems = new MovieItems(movie);
  367. movieItemses.add(movieItems);
  368. }
  369. }catch (Exception e){
  370. e.printStackTrace();
  371. }
  372.  
  373. }
  374.  
  375. @Override
  376. public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
  377.  
  378. }
  379. });
  380. return movieItemses;
  381. }
  382. protected void onReleaseResources(ArrayList<MovieItems> data){
  383.  
  384. }
  385. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement