Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.moviecatalogue;
- import android.content.Context;
- import android.content.Intent;
- import android.content.res.TypedArray;
- import android.graphics.Color;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.BaseAdapter;
- import android.widget.Button;
- import android.widget.ImageView;
- import android.widget.TextView;
- import java.util.ArrayList;
- public class MovieAdapter extends BaseAdapter
- {
- private Context context;
- private ArrayList<Movie> movies;
- private String[] judul;
- private String[] description;
- private TypedArray Poster;
- public MovieAdapter(Context context)
- {
- this.context = context;
- movies = new ArrayList<>( );
- getDataFromArray();
- }
- public void setMovies(ArrayList<Movie> movies)
- {
- this.movies = movies;
- }
- @Override
- public int getCount()
- {
- return movies.size();
- }
- @Override
- public Object getItem(int i)
- {
- return movies.get( i );
- }
- @Override
- public long getItemId(int i)
- {
- return i;
- }
- @Override
- public View getView(final int position, View view, ViewGroup viewGroup)
- {
- ViewHolder holder;
- if (view == null){
- view = (View) LayoutInflater.from( context ).inflate( R.layout.item_movie,viewGroup,false );
- holder = new ViewHolder(view);
- holder.homeDetailButton = (Button) view.findViewById(R.id.homeDetailButton);
- holder.homeDetailButton.setOnClickListener(buttonClickListener);
- view.setTag(holder);
- }
- else
- {
- holder = (ViewHolder) view.getTag();
- }
- holder.homeDetailButton.setTag(position);
- Movie movie = (Movie) getItem( position );
- holder.bind( movie );
- return view;
- }
- private View.OnClickListener buttonClickListener = new View.OnClickListener()
- {
- @Override
- public void onClick(View view)
- {
- int position = (Integer) view.getTag();
- Movie movie = new Movie();
- movie.setName(judul[position]);
- movie.setDescription(description[position]);
- movie.setPhoto( Poster.getResourceId( position, -1 ) );
- Intent movieDetail = new Intent( context, Detail.class );
- movieDetail.putExtra( Detail.EXTRA_MOVIE, movie );
- context.startActivity(movieDetail);
- }
- };
- private void getDataFromArray()
- {
- judul = context.getResources().getStringArray( R.array.data_name );
- description = context.getResources().getStringArray( R.array.data_description );
- Poster = context.getResources().obtainTypedArray( R.array.data_photo );
- }
- private class ViewHolder
- {
- private TextView judul;
- private TextView desc;
- private ImageView poster;
- public Button homeDetailButton;
- public final Integer position;
- ViewHolder(View view)
- {
- judul = view.findViewById( R.id.txt_name );
- desc = view.findViewById( R.id.txt_description );
- poster = view.findViewById( R.id.img_photo );
- position = 0;
- }
- void bind(Movie movie)
- {
- judul.setText( movie.getName() );
- judul.setTextColor( Color.RED);
- desc.setText( movie.getDescription() );
- poster.setImageResource( movie.getPhoto() );
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement