Last active
March 16, 2019 07:15
-
-
Save Lukmannudin/91b2f1931c55e0bde10387efaf5d2db9 to your computer and use it in GitHub Desktop.
smkcodingday3materi
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
apply plugin: 'com.android.application' | |
android { | |
compileSdkVersion 28 | |
defaultConfig { | |
applicationId "com.lukmannudin.assosiate.mvctutorial" | |
minSdkVersion 19 | |
targetSdkVersion 28 | |
versionCode 1 | |
versionName "1.0" | |
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" | |
} | |
buildTypes { | |
release { | |
minifyEnabled false | |
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' | |
} | |
} | |
} | |
dependencies { | |
implementation fileTree(include: ['*.jar'], dir: 'libs') | |
implementation 'com.android.support:appcompat-v7:28.0.0' | |
implementation 'com.android.support.constraint:constraint-layout:1.1.3' | |
implementation 'com.android.support:design:28.0.0' | |
testImplementation 'junit:junit:4.12' | |
androidTestImplementation 'com.android.support.test:runner:1.0.2' | |
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' | |
implementation 'com.github.bumptech.glide:glide:4.8.0' | |
implementation 'com.android.support:recyclerview-v7:28.0.0' | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
OVERVIEW PELATIHAN MVC-RECYCLERVIEW | |
1. MainActivity.java | |
2. Controller.java | |
3. Model.java | |
4. ListAdapter.java | |
5. DetailActivity.java | |
6. DataPokemon.java | |
Layout | |
1.activity_main.xml | |
2.activity_detail.xml | |
3.poke_list.xml | |
Gradle | |
1. (App)build.gradle (Untuk Konfigurasi Gradle dan library-library yang digunakan) | |
(Untuk referensi penggunaan activity) | |
AndroidManifest.xml | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:orientation="vertical" | |
tools:context=".DetailActivity"> | |
<ImageView | |
android:id="@+id/image_card" | |
android:layout_width="150dp" | |
android:layout_height="200dp" | |
android:layout_gravity="center" | |
android:layout_marginTop="80dp" /> | |
<LinearLayout | |
android:layout_marginTop="10dp" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:orientation="horizontal" | |
android:gravity="center" | |
> | |
<TextView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:textColor="@android:color/black" | |
android:text="Nama" /> | |
<TextView | |
android:layout_marginStart="10dp" | |
android:id="@+id/nama" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="Nama" /> | |
</LinearLayout> | |
<LinearLayout | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:orientation="horizontal" | |
android:gravity="center" | |
> | |
<TextView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:textColor="@android:color/black" | |
android:text="Rarity" /> | |
<TextView | |
android:layout_marginStart="10dp" | |
android:id="@+id/rarity" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="Rarity" /> | |
</LinearLayout> | |
<LinearLayout | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:orientation="horizontal" | |
android:gravity="center" | |
> | |
<TextView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:textColor="@android:color/black" | |
android:text="Set" /> | |
<TextView | |
android:layout_marginStart="10dp" | |
android:id="@+id/set" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="Set" /> | |
</LinearLayout> | |
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:orientation="vertical" | |
tools:context=".MainActivity"> | |
<android.support.v7.widget.RecyclerView | |
android:id="@+id/rvPoke" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
> | |
</android.support.v7.widget.RecyclerView> | |
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | |
package="com.lukmannudin.assosiate.mvctutorial"> | |
<uses-permission android:name="android.permission.INTERNET" /> | |
<application | |
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=".DetailActivity" android:parentActivityName=".MainActivity"></activity> | |
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Controller { | |
private MainActivity view; | |
private ArrayList<Model> myPoke = new ArrayList<>(); | |
public Controller(MainActivity view) { | |
this.view = view; | |
} | |
public void initData() { | |
myPoke.addAll(DataPokemon.getPokeData()); | |
view.showRecyclerList(); | |
} | |
public ArrayList<Model> getMyPoke() { | |
return myPoke; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DataPokemon { | |
public static String[][] data = new String[][]{ | |
{ | |
"Cubone", | |
"https://images.pokemontcg.io/dp6/90.png", | |
"Common", | |
"Legends Awakened" | |
}, | |
{ | |
"Windstorm", | |
"https://images.pokemontcg.io/ex14/85.png", | |
"Uncommon", | |
"Crystal Guardians" | |
}, | |
{ | |
"Turtwig", | |
"https://images.pokemontcg.io/pop9/17.png", | |
"Common", | |
"POP Series 9" | |
}, | |
{ | |
"Bellossom", | |
"https://images.pokemontcg.io/xy7/4.png", | |
"Uncommon", | |
"Ancient Origins" | |
}, | |
{ | |
"Hariyama ex", | |
"https://images.pokemontcg.io/ex8/100.png", | |
"Rare Holo EX", | |
"Deoxys" | |
}, | |
{ | |
"Deoxys", | |
"https://images.pokemontcg.io/xyp/XY05.png", | |
"Rare", | |
"XY Black Star Promos" | |
}, | |
{ | |
"Aggron", | |
"https://images.pokemontcg.io/ex16/1.png", | |
"Rare Holo", | |
"Power Keepers" | |
}, | |
{ | |
"Misdreavus", | |
"https://images.pokemontcg.io/dp6/107.png", | |
"Common", | |
"Legends Awakened" | |
}, | |
{ | |
"Greninja", | |
"https://images.pokemontcg.io/xy0/14.png", | |
"", | |
"Kalos Starter Set" | |
}, | |
{ | |
"Magneton", | |
"https://images.pokemontcg.io/ex16/16.png", | |
"Rare", | |
"Power Keepers" | |
} | |
}; | |
public static ArrayList<Model> getPokeData(){ | |
Model pokeModel = null; | |
ArrayList<Model> list = new ArrayList<>(); | |
for (String[] aData:data){ | |
pokeModel = new Model(); | |
pokeModel.setName(aData[0]); | |
pokeModel.setImage(aData[1]); | |
pokeModel.setRarity(aData[2]); | |
pokeModel.setSet(aData[3]); | |
list.add(pokeModel); | |
} | |
return list; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ListAdapter extends RecyclerView.Adapter<ListAdapter.ViewHolder> { | |
private Context context; | |
private ArrayList<Model> listData; | |
public ListAdapter(Context context, ArrayList<Model> listData) { | |
this.context = context; | |
this.listData = listData; | |
} | |
@NonNull | |
@Override | |
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int position) { | |
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.poke_list, parent, false); | |
ViewHolder viewHolder = new ViewHolder(view); | |
return viewHolder; | |
} | |
@Override | |
public void onBindViewHolder(@NonNull ViewHolder viewHolder, final int position) { | |
viewHolder.tvName.setText(listData.get(position).getName()); | |
viewHolder.tvRarity.setText(listData.get(position).getRarity()); | |
viewHolder.tvSet.setText(listData.get(position).getSet()); | |
Glide.with(context) | |
.load(listData.get(position).getImage()) | |
.into(viewHolder.ivPoke); | |
viewHolder.itemView.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
Intent intent = new Intent(context,DetailActivity.class); | |
intent.putExtra("name",listData.get(position).getName()); | |
intent.putExtra("rarity",listData.get(position).getRarity()); | |
intent.putExtra("set",listData.get(position).getSet()); | |
intent.putExtra("image",listData.get(position).getImage()); | |
context.startActivity(intent); | |
} | |
}); | |
} | |
@Override | |
public int getItemCount() { | |
return listData.size(); | |
} | |
public class ViewHolder extends RecyclerView.ViewHolder { | |
TextView tvName; | |
ImageView ivPoke; | |
TextView tvRarity; | |
TextView tvSet; | |
public ViewHolder(@NonNull View itemView) { | |
super(itemView); | |
tvName = itemView.findViewById(R.id.name); | |
tvRarity = itemView.findViewById(R.id.rarity); | |
tvSet = itemView.findViewById(R.id.set); | |
ivPoke = itemView.findViewById(R.id.ivPoke); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ListAdapter extends RecyclerView.Adapter<ListAdapter.ViewHolder> { | |
private Context context; | |
private ArrayList<Model> listData; | |
public ListAdapter(Context context, ArrayList<Model> listData) { | |
this.context = context; | |
this.listData = listData; | |
} | |
@NonNull | |
@Override | |
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int position) { | |
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.poke_list, parent, false); | |
ViewHolder viewHolder = new ViewHolder(view); | |
return viewHolder; | |
} | |
@Override | |
public void onBindViewHolder(@NonNull ViewHolder viewHolder, final int position) { | |
viewHolder.tvName.setText(listData.get(position).getName()); | |
viewHolder.tvRarity.setText(listData.get(position).getRarity()); | |
viewHolder.tvSet.setText(listData.get(position).getSet()); | |
Glide.with(context) | |
.load(listData.get(position).getImage()) | |
.into(viewHolder.ivPoke); | |
viewHolder.itemView.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
Intent intent = new Intent(context,DetailActivity.class); | |
intent.putExtra("name",listData.get(position).getName()); | |
intent.putExtra("rarity",listData.get(position).getRarity()); | |
intent.putExtra("set",listData.get(position).getSet()); | |
intent.putExtra("image",listData.get(position).getImage()); | |
context.startActivity(intent); | |
} | |
}); | |
} | |
@Override | |
public int getItemCount() { | |
return listData.size(); | |
} | |
public class ViewHolder extends RecyclerView.ViewHolder { | |
TextView tvName; | |
ImageView ivPoke; | |
TextView tvRarity; | |
TextView tvSet; | |
public ViewHolder(@NonNull View itemView) { | |
super(itemView); | |
tvName = itemView.findViewById(R.id.name); | |
tvRarity = itemView.findViewById(R.id.rarity); | |
tvSet = itemView.findViewById(R.id.set); | |
ivPoke = itemView.findViewById(R.id.ivPoke); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MainActivity extends AppCompatActivity { | |
RecyclerView rvPoke; | |
Controller controller; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
rvPoke = findViewById(R.id.rvPoke); | |
controller = new Controller(this); | |
controller.initData(); | |
} | |
public void showRecyclerList() { | |
rvPoke.setLayoutManager(new LinearLayoutManager(this)); | |
ListAdapter listAdapter = new ListAdapter(this, controller.getMyPoke()); | |
rvPoke.setAdapter(listAdapter); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Model { | |
private String name; | |
private String image; | |
private String rarity; | |
private String set; | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public String getImage() { | |
return image; | |
} | |
public void setImage(String image) { | |
this.image = image; | |
} | |
public String getRarity() { | |
return rarity; | |
} | |
public void setRarity(String rarity) { | |
this.rarity = rarity; | |
} | |
public String getSet() { | |
return set; | |
} | |
public void setSet(String set) { | |
this.set = set; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout | |
xmlns:android="http://schemas.android.com/apk/res/android" | |
android:orientation="horizontal" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_margin="10dp" | |
> | |
<ImageView | |
android:id="@+id/ivPoke" | |
android:layout_width="90dp" | |
android:layout_height="120dp" | |
android:src="@mipmap/ic_launcher_round" | |
android:scaleType="fitXY" | |
/> | |
<LinearLayout | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:orientation="vertical" | |
android:layout_margin="5dp" | |
> | |
<TextView | |
android:layout_marginTop="5dp" | |
android:id="@+id/name" | |
android:text="Name" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" /> | |
<TextView | |
android:layout_marginTop="5dp" | |
android:id="@+id/rarity" | |
android:text="Rarity" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" /> | |
<TextView | |
android:layout_marginTop="5dp" | |
android:id="@+id/set" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_gravity="end" | |
android:text="Set" /> | |
</LinearLayout> | |
</LinearLayout> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment