Advertisement
Guest User

Adapter

a guest
Aug 24th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.56 KB | None | 0 0
  1. package com.example.moviecatalogue;
  2.  
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.content.res.TypedArray;
  6. import android.graphics.Color;
  7. import android.view.LayoutInflater;
  8. import android.view.View;
  9. import android.view.ViewGroup;
  10. import android.widget.BaseAdapter;
  11. import android.widget.Button;
  12. import android.widget.ImageView;
  13. import android.widget.TextView;
  14. import java.util.ArrayList;
  15.  
  16. public class MovieAdapter extends BaseAdapter
  17. {
  18. private Context context;
  19. private ArrayList<Movie> movies;
  20. private String[] judul;
  21. private String[] description;
  22. private TypedArray Poster;
  23.  
  24. public MovieAdapter(Context context)
  25. {
  26. this.context = context;
  27. movies = new ArrayList<>( );
  28. getDataFromArray();
  29. }
  30.  
  31. public void setMovies(ArrayList<Movie> movies)
  32. {
  33. this.movies = movies;
  34. }
  35.  
  36. @Override
  37. public int getCount()
  38. {
  39. return movies.size();
  40. }
  41.  
  42. @Override
  43. public Object getItem(int i)
  44. {
  45. return movies.get( i );
  46. }
  47.  
  48. @Override
  49. public long getItemId(int i)
  50. {
  51. return i;
  52. }
  53.  
  54. @Override
  55. public View getView(final int position, View view, ViewGroup viewGroup)
  56. {
  57. ViewHolder holder;
  58. if (view == null){
  59. view = (View) LayoutInflater.from( context ).inflate( R.layout.item_movie,viewGroup,false );
  60. holder = new ViewHolder(view);
  61. holder.homeDetailButton = (Button) view.findViewById(R.id.homeDetailButton);
  62. holder.homeDetailButton.setOnClickListener(buttonClickListener);
  63. view.setTag(holder);
  64. }
  65. else
  66. {
  67. holder = (ViewHolder) view.getTag();
  68. }
  69.  
  70. holder.homeDetailButton.setTag(position);
  71.  
  72. Movie movie = (Movie) getItem( position );
  73. holder.bind( movie );
  74. return view;
  75. }
  76.  
  77. private View.OnClickListener buttonClickListener = new View.OnClickListener()
  78. {
  79. @Override
  80. public void onClick(View view)
  81. {
  82. int position = (Integer) view.getTag();
  83.  
  84. Movie movie = new Movie();
  85. movie.setName(judul[position]);
  86. movie.setDescription(description[position]);
  87.  
  88. movie.setPhoto( Poster.getResourceId( position, -1 ) );
  89.  
  90. Intent movieDetail = new Intent( context, Detail.class );
  91. movieDetail.putExtra( Detail.EXTRA_MOVIE, movie );
  92. context.startActivity(movieDetail);
  93. }
  94. };
  95.  
  96. private void getDataFromArray()
  97. {
  98. judul = context.getResources().getStringArray( R.array.data_name );
  99. description = context.getResources().getStringArray( R.array.data_description );
  100. Poster = context.getResources().obtainTypedArray( R.array.data_photo );
  101. }
  102.  
  103.  
  104. private class ViewHolder
  105. {
  106. private TextView judul;
  107. private TextView desc;
  108. private ImageView poster;
  109. public Button homeDetailButton;
  110. public final Integer position;
  111.  
  112.  
  113. ViewHolder(View view)
  114. {
  115. judul = view.findViewById( R.id.txt_name );
  116. desc = view.findViewById( R.id.txt_description );
  117. poster = view.findViewById( R.id.img_photo );
  118. position = 0;
  119. }
  120.  
  121. void bind(Movie movie)
  122. {
  123. judul.setText( movie.getName() );
  124. judul.setTextColor( Color.RED);
  125. desc.setText( movie.getDescription() );
  126. poster.setImageResource( movie.getPhoto() );
  127. }
  128. }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement