public class MainActivity extends AppCompatActivity {
ArrayList<Movie> movies = new ArrayList<>();
ProgressDialog progressDialog;
RecyclerView movieRecycler;
MovieAdapter movieAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getPosts();
}
public void getPosts() {
showDialog();
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.GET, URL.URL, null, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.e("hzm", response.toString());
for (int i = 0; i < response.length(); i++) {
try {
JSONObject object = response.getJSONObject(i);
String title = object.getString("title");
String image = object.getString("image");
Double rating = object.getDouble("rating");
int releaseYear = object.getInt("releaseYear");
String genreString = "";
JSONArray genres = object.getJSONArray("genre");
for (int j=0; j<genres.length();j++){
genreString += genres.getString(j);
if(j!=genres.length()-1){
genreString += ", ";
}
}
movies.add(new Movie(title,image,rating,releaseYear,genreString));
hideDialog();
} catch (JSONException e) {
e.printStackTrace();
hideDialog();
}
}
movieRecycler = findViewById(R.id.postRecycler);
movieRecycler.setLayoutManager(new LinearLayoutManager(MainActivity.this));
movieAdapter = new MovieAdapter(movies, MainActivity.this);
movieRecycler.setAdapter(movieAdapter);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
hideDialog();
Log.e("hzm", error.getMessage());
}
});
AppController.getInstance().addToRequestQueue(jsonArrayRequest);
}
private void showDialog() {
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setCancelable(false);
progressDialog.setTitle("Loading... ");
progressDialog.show();
}
private void hideDialog() {
progressDialog.hide();
}
}
public class URL {
public static final String URL = "https://api.androidhive.info/json/movies.json?fbclid=IwAR2PXO8XpcGBxNxtVIew2xWJrqP23FySE46CEI7NcXTMBOPKeg1ul8yDWdE";
}
public class Movie {
private String title;
private String image;
private Double rating;
private int releaseYear;
private String genres;
public Movie(String title, String image, Double rating, int releaseYear, String genres) {
this.title = title;
this.image = image;
this.rating = rating;
this.releaseYear = releaseYear;
this.genres = genres;
}
}
public class AppController extends Application {
public static final String TAG = AppController.class.getSimpleName();
private RequestQueue mRequestQueue;
private static AppController mInstance;
@Override
public void onCreate() {
super.onCreate();
mInstance = this;
}
public static synchronized AppController getInstance() {
return mInstance;
}
public RequestQueue getRequestQueue() {
if (mRequestQueue == null) {
mRequestQueue = Volley.newRequestQueue(getApplicationContext());
}
return mRequestQueue;
}
public <T> void addToRequestQueue(Request<T> req, String tag) {
req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
getRequestQueue().add(req);
}
public <T> void addToRequestQueue(Request<T> req) {
req.setTag(TAG);
getRequestQueue().add(req);
}
public void cancelPendingRequests(Object tag) {
if (mRequestQueue != null) {
mRequestQueue.cancelAll(tag);
}
}
}
public class MovieAdapter extends RecyclerView.Adapter<MovieAdapter.MyViewHolder> {
ArrayList<Movie> data;
Activity activity;
public MovieAdapter(ArrayList<Movie> data, Activity activity) {
this.data = data;
this.activity = activity;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View itemView = (View) LayoutInflater.from(activity).inflate(R.layout.movie_item, viewGroup, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, int i) {
myViewHolder.tvTitle.setText(data.get(i).getTitle() + "");
myViewHolder.tvRating.setText(data.get(i).getRating() + "");
myViewHolder.tvReleaseYear.setText(data.get(i).getReleaseYear()+"");
myViewHolder.tvGenres.setText(data.get(i).getGenres());
Picasso.get().load(data.get(i).getImage()).into(myViewHolder.tvImage);
//myViewHolder.tvImage.set(data.get(i).getGenres());
myViewHolder.linear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
}
@Override
public int getItemCount() {
return data.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView tvTitle;
public TextView tvRating;
public TextView tvReleaseYear;
public TextView tvGenres;
public ImageView tvImage;
public LinearLayout linear;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
tvTitle = itemView.findViewById(R.id.tvTitle);
tvRating = itemView.findViewById(R.id.tvRating);
tvReleaseYear = itemView.findViewById(R.id.tvYear);
tvGenres = itemView.findViewById(R.id.tvGenres);
tvImage = itemView.findViewById(R.id.tvImage);
linear = itemView.findViewById(R.id.linear);
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:id="@+id/linear"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:padding="10dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:elevation="20dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="@+id/tvImage"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center"
android:layout_margin="20dp"/>
<LinearLayout
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="vertical">
<TextView
android:layout_weight="1"
android:textColor="@android:color/black"
android:textStyle="bold"
android:textSize="18dp"
android:id="@+id/tvTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Title" />
<TextView
android:textColor="@android:color/black"
android:layout_weight="1"
android:id="@+id/tvRating"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Rating" />
<TextView
android:layout_weight="1"
android:id="@+id/tvGenres"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Genres" />
<TextView
android:textAlignment="textEnd"
android:layout_gravity="end"
android:layout_weight="1"
android:id="@+id/tvYear"
android:layout_marginRight="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/postRecycler"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.it.lec9_v1">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:name=".app.AppController"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>