Skip to content

Commit aa928b9

Browse files
committed
Adapter added
1 parent 3005cb4 commit aa928b9

File tree

5 files changed

+149
-11
lines changed

5 files changed

+149
-11
lines changed

app/src/main/java/com/sdsmdg/hareshkh/lectureassignment/MainActivity.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,21 @@
22

33
import android.support.v7.app.AppCompatActivity;
44
import android.os.Bundle;
5+
import android.support.v7.widget.RecyclerView;
6+
7+
import java.util.ArrayList;
58

69
public class MainActivity extends AppCompatActivity {
710

11+
ArrayList<MovieClass> myMovies;
12+
813
@Override
914
protected void onCreate(Bundle savedInstanceState) {
1015
super.onCreate(savedInstanceState);
1116
setContentView(R.layout.activity_main);
17+
18+
RecyclerView movieList= (RecyclerView) findViewById(R.id.my_recycler_view);
19+
20+
1221
}
1322
}

app/src/main/java/com/sdsmdg/hareshkh/lectureassignment/movies.java renamed to app/src/main/java/com/sdsmdg/hareshkh/lectureassignment/MovieClass.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package com.sdsmdg.hareshkh.lectureassignment;
22

3-
public class movies {
3+
public class MovieClass {
44

5-
String title;
6-
String year;
7-
String imageResId;
5+
private String title;
6+
private String year;
7+
private String imageResId;
88

9-
public movies(String title, String year, String imageResId) {
9+
public MovieClass(String title, String year, String imageResId) {
1010
this.title = title;
1111
this.year = year;
1212
this.imageResId = imageResId;
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package com.sdsmdg.hareshkh.lectureassignment;
2+
3+
import android.content.Context;
4+
import android.graphics.Bitmap;
5+
import android.graphics.BitmapFactory;
6+
import android.support.v7.widget.RecyclerView;
7+
import android.view.LayoutInflater;
8+
import android.view.View;
9+
import android.view.ViewGroup;
10+
import android.widget.ImageView;
11+
import android.widget.TextView;
12+
13+
import java.util.List;
14+
15+
public class MoviesAdapter extends RecyclerView.Adapter<MoviesAdapter.ViewHolder> {
16+
17+
private List<MovieClass> movies;
18+
private Context ctx;
19+
20+
public MoviesAdapter(List<MovieClass> movies, Context ctx) {
21+
this.movies = movies;
22+
this.ctx = ctx;
23+
}
24+
25+
public Context getCtx() {
26+
return ctx;
27+
}
28+
29+
public class ViewHolder extends RecyclerView.ViewHolder {
30+
31+
public TextView movieName;
32+
public TextView movieYear;
33+
public ImageView movieImage;
34+
35+
public ViewHolder(View itemView) {
36+
super(itemView);
37+
movieName= (TextView) itemView.findViewById(R.id.movie_name);
38+
movieYear= (TextView) itemView.findViewById(R.id.movie_year);
39+
movieImage= (ImageView) itemView.findViewById(R.id.movie_image);
40+
}
41+
}
42+
43+
@Override
44+
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
45+
LayoutInflater inflater=LayoutInflater.from(getCtx());
46+
47+
View movieView= inflater.inflate(R.layout.row_layout,parent,false);
48+
49+
ViewHolder mHolder= new ViewHolder(movieView);
50+
return mHolder;
51+
}
52+
53+
@Override
54+
public void onBindViewHolder(ViewHolder holder, int position) {
55+
56+
MovieClass movie=movies.get(position);
57+
holder.movieName.setText(movie.getTitle());
58+
holder.movieYear.setText(movie.getYear());
59+
60+
Bitmap bitmap= BitmapFactory.decodeResource(getCtx().getResources(),
61+
Utils.getImageResourceFromString(movie.getImageResId(),getCtx()));
62+
Bitmap bMapScaled=Utils.scaleToFitWidth(bitmap, (int) Utils.convertToPixel(500,getCtx()));
63+
64+
holder.movieImage.setImageBitmap(bMapScaled);
65+
66+
}
67+
68+
@Override
69+
public int getItemCount() {
70+
return movies.size();
71+
}
72+
}

app/src/main/java/com/sdsmdg/hareshkh/lectureassignment/Utils.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package com.sdsmdg.hareshkh.lectureassignment;
22

3+
import android.content.Context;
4+
import android.content.res.Resources;
35
import android.graphics.Bitmap;
6+
import android.util.DisplayMetrics;
7+
import android.util.TypedValue;
48

59
public class Utils {
610

@@ -10,4 +14,14 @@ public static Bitmap scaleToFitWidth(Bitmap b, int width)
1014
return Bitmap.createScaledBitmap(b, width, (int) (b.getHeight() * factor), true);
1115
}
1216

17+
public static int getImageResourceFromString(String img_path, Context context){
18+
int imageResource = context.getResources().getIdentifier(img_path, null, context.getPackageName());
19+
return imageResource;
20+
}
21+
22+
public static float convertToPixel(float myDPs,Context ctx){
23+
Resources r = ctx.getResources();
24+
float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, myDPs, r.getDisplayMetrics());
25+
return px;
26+
}
1327
}
Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,52 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3-
android:orientation="vertical" android:layout_width="match_parent"
4-
android:layout_height="match_parent">
2+
<android.support.v7.widget.CardView
3+
xmlns:android="http://schemas.android.com/apk/res/android"
4+
xmlns:card_view="http://schemas.android.com/apk/res-auto"
5+
android:layout_width="match_parent"
6+
android:layout_height="70dp"
7+
android:layout_margin="16dp"
8+
card_view:cardCornerRadius="8dp"
9+
card_view:cardElevation="8dp"
10+
android:clickable="true"
11+
android:foreground="?android:attr/selectableItemBackground">
512

6-
<android.support.v7.widget.CardView
13+
14+
<RelativeLayout
715
android:layout_width="match_parent"
8-
android:layout_height="match_parent" />
9-
</LinearLayout>
16+
android:layout_height="match_parent">
17+
18+
<ImageView
19+
android:layout_width="match_parent"
20+
android:layout_height="match_parent"
21+
android:gravity="center"
22+
android:scaleType="centerCrop"
23+
android:id="@+id/movie_image" />
24+
25+
<LinearLayout
26+
android:orientation="vertical"
27+
android:layout_width="match_parent"
28+
android:layout_height="match_parent">
29+
30+
<TextView
31+
android:text="Movie"
32+
android:layout_width="match_parent"
33+
android:layout_height="0dp"
34+
android:layout_weight="1"
35+
android:textSize="16dp"
36+
android:textStyle="bold"
37+
android:gravity="center"
38+
android:id="@+id/movie_name" />
39+
40+
<TextView
41+
android:text="Year"
42+
android:layout_width="match_parent"
43+
android:layout_height="0dp"
44+
android:layout_weight="1"
45+
android:gravity="center"
46+
android:id="@+id/movie_year" />
47+
48+
49+
</LinearLayout>
50+
</RelativeLayout>
51+
52+
</android.support.v7.widget.CardView>

0 commit comments

Comments
 (0)