Skip to content

Instantly share code, notes, and snippets.

@ikydhana
Last active September 2, 2019 12:03
Show Gist options
  • Save ikydhana/41e98c4a76c2084ca3fd4142846ea489 to your computer and use it in GitHub Desktop.
Save ikydhana/41e98c4a76c2084ca3fd4142846ea489 to your computer and use it in GitHub Desktop.
Movie Catalog Android
package com.officialdevbjb.moviecatalog;
import android.content.Intent;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
Intent intent;
private String[] mJudul, mKet, mTahun;
private TypedArray mFoto;
private ArrayList<Movie> list;
private MovieAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
adapter = new MovieAdapter(this);
ListView listView = findViewById(R.id.lv_movie);
listView.setAdapter(adapter);
initialization();
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
intent = new Intent(MainActivity.this, MovieDetail.class);
intent.putExtra(MovieDetail.STATE_MOVIE, list.get(i));
startActivity(intent);
}
});
}
private void initialization() {
mJudul = getResources().getStringArray(R.array.movie_name);
mKet = getResources().getStringArray(R.array.movie_desc);
mFoto = getResources().obtainTypedArray(R.array.movie_foto);
mTahun = getResources().getStringArray(R.array.movie_tahun);
addItem();
}
private void addItem() {
list = new ArrayList<>();
for (int i = 0; i < mJudul.length; i++) {
Movie movie = new Movie();
movie.setPic(mFoto.getResourceId(i, -1));
movie.setJudul(mJudul[i]);
movie.setKeterangan(mKet[i]);
movie.setTahun(mTahun[i]);
list.add(movie);
}
adapter.setmovies(list);
}
}
package com.officialdevbjb.moviecatalog;
import android.os.Parcel;
import android.os.Parcelable;
public class Movie implements Parcelable {
private int pic;
private String judul, keterangan, tahun;
public int getPic() {
return pic;
}
public void setPic(int pic) {
this.pic = pic;
}
public String getJudul() {
return judul;
}
public void setJudul(String judul) {
this.judul = judul;
}
public String getKeterangan() {
return keterangan;
}
public void setKeterangan(String keterangan) {
this.keterangan = keterangan;
}
public String getTahun() {
return tahun;
}
public void setTahun(String tahun) {
this.tahun = tahun;
}
public Movie() {
}
@Override
public int describeContents(){
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(this.pic);
dest.writeString(this.judul);
dest.writeString(this.keterangan);
dest.writeString(this.tahun);
}
protected Movie(Parcel in){
this.pic = in.readInt();
this.judul = in.readString();
this.keterangan = in.readString();
this.tahun = in.readString();
}
public static final Creator<Movie> CREATOR = new Creator<Movie>() {
@Override
public Movie createFromParcel(Parcel source) {
return new Movie(source);
}
@Override
public Movie[] newArray(int size) {
return new Movie[size];
}
};
}
package com.officialdevbjb.moviecatalog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
public class MovieAdapter extends BaseAdapter {
private final Context context;
private ArrayList<Movie> movies;
void setmovies(ArrayList<Movie> movies) {
this.movies = movies;
}
MovieAdapter(Context context) {
this.context = context;
movies = new ArrayList<>();
}
@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(int i, View view, ViewGroup viewGroup) {
if (view == null) {
view = LayoutInflater.from(context).inflate(R.layout.item, viewGroup, false);
}
ViewHolder viewHolder = new ViewHolder(view);
Movie Movie = (Movie) getItem(i);
viewHolder.bind(Movie);
return view;
}
private class ViewHolder {
private TextView txtJudul;
private TextView txtKet;
private ImageView imgFoto;
ViewHolder(View view) {
txtJudul = view.findViewById(R.id.judul_movie);
txtKet = view.findViewById(R.id.keterangan);
imgFoto = view.findViewById(R.id.pic_movie);
}
void bind(Movie movie) {
txtJudul.setText(Movie.getJudul());
txtKet.setText(Movie.getKeterangan());
imgFoto.setImageResource(Movie.getPic());
}
}
}
package com.officialdevbjb.moviecatalog;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
public class MovieDetail extends AppCompatActivity {
public static final String STATE_MOVIE = "Movie";
TextView lblJudul, lblKeterangan, lblTahun;
ImageView picMovie;
String judul, keterangan, tahun;
int pic;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_movie_detail);
initialization();
}
private void initialization() {
lblJudul = findViewById(R.id.judul_movie);
lblKeterangan = findViewById(R.id.detailketerangan);
lblTahun = findViewById(R.id.tahun_film);
picMovie = findViewById(R.id.pic_movie);
takeMovie();
}
private void takeMovie() {
Movie movie = getIntent().getParcelableExtra(STATE_MOVIE);
judul = movie.getJudul();
keterangan = movie.getKeterangan();
tahun = movie.getTahun();
pic = movie.getPic();
lblJudul.setText(judul);
lblKeterangan.setText(keterangan);
lblTahun.setText(tahun);
picMovie.setImageResource(pic);
}
}
@aldiskatel
Copy link

void bind(Movie movie) { (line 63) txtJudul.setText(Movie.getJudul()); (line 64) txtKet.setText(Movie.getKeterangan()); (line 65) imgFoto.setImageResource(Movie.getPic()); (line 66) }

Disitu km hndk meambil data dari Movie yg sudah di inisiasi di line 63 (Movie movie)
jd km menariknya seharusnya tinggal

txtJudul.setText(movie.getJudul());

@ikydhana
Copy link
Author

ikydhana commented Sep 2, 2019

mantepp auk isii array nya dulu nah

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment