diff --git a/app/src/main/java/com/sdsmdg/hareshkh/lectureassignment/MainActivity.java b/app/src/main/java/com/sdsmdg/hareshkh/lectureassignment/MainActivity.java index a42b22d..8b0b860 100644 --- a/app/src/main/java/com/sdsmdg/hareshkh/lectureassignment/MainActivity.java +++ b/app/src/main/java/com/sdsmdg/hareshkh/lectureassignment/MainActivity.java @@ -2,12 +2,21 @@ import android.support.v7.app.AppCompatActivity; import android.os.Bundle; +import android.support.v7.widget.RecyclerView; + +import java.util.ArrayList; public class MainActivity extends AppCompatActivity { + ArrayList myMovies; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); + + RecyclerView movieList= (RecyclerView) findViewById(R.id.my_recycler_view); + + } } diff --git a/app/src/main/java/com/sdsmdg/hareshkh/lectureassignment/movies.java b/app/src/main/java/com/sdsmdg/hareshkh/lectureassignment/MovieClass.java similarity index 76% rename from app/src/main/java/com/sdsmdg/hareshkh/lectureassignment/movies.java rename to app/src/main/java/com/sdsmdg/hareshkh/lectureassignment/MovieClass.java index 3ee671a..53f4e60 100644 --- a/app/src/main/java/com/sdsmdg/hareshkh/lectureassignment/movies.java +++ b/app/src/main/java/com/sdsmdg/hareshkh/lectureassignment/MovieClass.java @@ -1,12 +1,12 @@ package com.sdsmdg.hareshkh.lectureassignment; -public class movies { +public class MovieClass { - String title; - String year; - String imageResId; + private String title; + private String year; + private String imageResId; - public movies(String title, String year, String imageResId) { + public MovieClass(String title, String year, String imageResId) { this.title = title; this.year = year; this.imageResId = imageResId; diff --git a/app/src/main/java/com/sdsmdg/hareshkh/lectureassignment/MoviesAdapter.java b/app/src/main/java/com/sdsmdg/hareshkh/lectureassignment/MoviesAdapter.java new file mode 100644 index 0000000..c3a6b3a --- /dev/null +++ b/app/src/main/java/com/sdsmdg/hareshkh/lectureassignment/MoviesAdapter.java @@ -0,0 +1,72 @@ +package com.sdsmdg.hareshkh.lectureassignment; + +import android.content.Context; +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; +import android.support.v7.widget.RecyclerView; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ImageView; +import android.widget.TextView; + +import java.util.List; + +public class MoviesAdapter extends RecyclerView.Adapter { + + private List movies; + private Context ctx; + + public MoviesAdapter(List movies, Context ctx) { + this.movies = movies; + this.ctx = ctx; + } + + public Context getCtx() { + return ctx; + } + + public class ViewHolder extends RecyclerView.ViewHolder { + + public TextView movieName; + public TextView movieYear; + public ImageView movieImage; + + public ViewHolder(View itemView) { + super(itemView); + movieName= (TextView) itemView.findViewById(R.id.movie_name); + movieYear= (TextView) itemView.findViewById(R.id.movie_year); + movieImage= (ImageView) itemView.findViewById(R.id.movie_image); + } + } + + @Override + public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { + LayoutInflater inflater=LayoutInflater.from(getCtx()); + + View movieView= inflater.inflate(R.layout.row_layout,parent,false); + + ViewHolder mHolder= new ViewHolder(movieView); + return mHolder; + } + + @Override + public void onBindViewHolder(ViewHolder holder, int position) { + + MovieClass movie=movies.get(position); + holder.movieName.setText(movie.getTitle()); + holder.movieYear.setText(movie.getYear()); + + Bitmap bitmap= BitmapFactory.decodeResource(getCtx().getResources(), + Utils.getImageResourceFromString(movie.getImageResId(),getCtx())); + Bitmap bMapScaled=Utils.scaleToFitWidth(bitmap, (int) Utils.convertToPixel(500,getCtx())); + + holder.movieImage.setImageBitmap(bMapScaled); + + } + + @Override + public int getItemCount() { + return movies.size(); + } +} diff --git a/app/src/main/java/com/sdsmdg/hareshkh/lectureassignment/Utils.java b/app/src/main/java/com/sdsmdg/hareshkh/lectureassignment/Utils.java index b5dd071..1109fee 100644 --- a/app/src/main/java/com/sdsmdg/hareshkh/lectureassignment/Utils.java +++ b/app/src/main/java/com/sdsmdg/hareshkh/lectureassignment/Utils.java @@ -1,6 +1,10 @@ package com.sdsmdg.hareshkh.lectureassignment; +import android.content.Context; +import android.content.res.Resources; import android.graphics.Bitmap; +import android.util.DisplayMetrics; +import android.util.TypedValue; public class Utils { @@ -10,4 +14,14 @@ public static Bitmap scaleToFitWidth(Bitmap b, int width) return Bitmap.createScaledBitmap(b, width, (int) (b.getHeight() * factor), true); } + public static int getImageResourceFromString(String img_path, Context context){ + int imageResource = context.getResources().getIdentifier(img_path, null, context.getPackageName()); + return imageResource; + } + + public static float convertToPixel(float myDPs,Context ctx){ + Resources r = ctx.getResources(); + float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, myDPs, r.getDisplayMetrics()); + return px; + } } diff --git a/app/src/main/res/layout/row_layout.xml b/app/src/main/res/layout/row_layout.xml index 716ef1e..2d93f93 100644 --- a/app/src/main/res/layout/row_layout.xml +++ b/app/src/main/res/layout/row_layout.xml @@ -1,9 +1,52 @@ - + - - \ No newline at end of file + android:layout_height="match_parent"> + + + + + + + + + + + + + +