Skip to content

Instantly share code, notes, and snippets.

@adammb86
Last active March 22, 2019 03:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adammb86/75323b165445af72b24a2e8cb95f6ef8 to your computer and use it in GitHub Desktop.
Save adammb86/75323b165445af72b24a2e8cb95f6ef8 to your computer and use it in GitHub Desktop.
Data Pokemon Online
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/detail_activity"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="@dimen/activity_vertical_margin"
android:orientation="vertical">
<ImageView
android:id="@+id/img_detail_gambar_pokemon"
android:layout_width="150dp"
android:layout_height="200dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="130dp"
android:layout_marginBottom="@dimen/medium_dimen"
android:contentDescription="@string/gambar_pokemon"
android:src="@mipmap/ic_launcher_round" />
<TextView
android:id="@+id/tv_detail_nama_pokemon"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/small_dimen"
android:text="@string/detail_nama_pokemon"
android:textAlignment="center"
android:textAllCaps="true"
android:textSize="40sp"
android:textStyle="bold" />
<TextView
android:id="@+id/tv_detail_rarity_pokemon"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/small_dimen"
android:text="@string/detail_rarity_pokemon"
android:textAlignment="center"
android:textAllCaps="true"
android:textSize="20sp" />
<TextView
android:id="@+id/tv_detail_deck_pokemon"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/small_dimen"
android:text="@string/detail_deck_pokemon"
android:textAlignment="center"
android:textAllCaps="true"
android:textSize="20sp" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="@+id/main_activity"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipe_refresh_pokemon"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_pokemon"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:listitem="@layout/item_pokemon">
</android.support.v7.widget.RecyclerView>
</android.support.v4.widget.SwipeRefreshLayout>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.datapokemononline">
<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=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.example.datapokemononline"
minSdkVersion 27
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(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
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.android.support:recyclerview-v7:28.0.0'
implementation 'com.github.bumptech.glide:glide:4.9.0'
implementation 'com.squareup.okhttp3:okhttp:3.12.0'
implementation 'com.google.code.gson:gson:2.8.5'
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/activity_horizontal_margin"
android:orientation="horizontal">
<ImageView
android:id="@+id/img_pokemon"
android:layout_width="150dp"
android:layout_height="200dp"
android:scaleType="fitXY"
android:src="@mipmap/ic_launcher_round" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:gravity="center_vertical"
android:orientation="vertical">
<TextView
android:id="@+id/tv_nama_pokemon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/small_dimen"
android:layout_marginBottom="@dimen/small_dimen"
android:text="@string/nama_pokemon"
android:textSize="24sp"
android:textStyle="bold" />
<TextView
android:id="@+id/tv_kelangkaan_pokemon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/small_dimen"
android:layout_marginBottom="@dimen/small_dimen"
android:text="@string/kelangkaan_pokemon"
android:textSize="20sp" />
<TextView
android:id="@+id/tv_pack_kartu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/small_dimen"
android:layout_marginBottom="@dimen/small_dimen"
android:text="@string/muncul_di_pack_mana"
android:textSize="20sp" />
</LinearLayout>
</LinearLayout>
package com.example.datapokemononline;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.widget.Toast;
import com.google.gson.Gson;
import java.io.IOException;
import java.util.ArrayList;
import okhttp3.Response;
public class MainActivity extends AppCompatActivity {
RecyclerView rvPokemon;
SwipeRefreshLayout swipePokemon;
Gson gson;
ArrayList<PokemonModel> pokemonModels;
PokemonAdapter pokemonAdapter;
PokemonController pokemonController = new PokemonController(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rvPokemon = findViewById(R.id.rv_pokemon);
rvPokemon.setLayoutManager(new LinearLayoutManager(this));
pokemonModels = new ArrayList<>();
pokemonAdapter = new PokemonAdapter(this, pokemonModels);
rvPokemon.setAdapter(pokemonAdapter);
lakukanSwipeRefresh();
}
public void onFailure() {
runOnUiThread(new Runnable() {
@Override
public void run() {
swipePokemon.setRefreshing(false);
Toast.makeText(MainActivity.this,"Request anda gagal",Toast.LENGTH_LONG).show();
}
});
}
public void onResponse(Response response) throws IOException {
if(response.isSuccessful()){
gson = new Gson();
final PokemonModel.PokemonResultModel resultModel = gson.fromJson(response.body().string(),
PokemonModel.PokemonResultModel.class);
runOnUiThread(new Runnable() {
@Override
public void run() {
swipePokemon.setRefreshing(false);
pokemonModels.addAll(resultModel.getCards());
pokemonAdapter.notifyDataSetChanged();
}
});
}else{
runOnUiThread(new Runnable() {
@Override
public void run() {
swipePokemon.setRefreshing(false);
Toast.makeText(MainActivity.this,"Request anda error",Toast.LENGTH_LONG).show();
}
});
}
}
public void lakukanSwipeRefresh(){
swipePokemon = findViewById(R.id.swipe_refresh_pokemon);
swipePokemon.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
pokemonController.populatePokemonData();
}
});
swipePokemon.setRefreshing(true);
pokemonController.populatePokemonData();
}
}
package com.example.datapokemononline;
import android.content.Context;
import android.support.annotation.NonNull;
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 com.bumptech.glide.Glide;
import java.util.ArrayList;
class PokemonAdapter extends RecyclerView.Adapter<PokemonAdapter.ViewHolder> {
private Context context;
private ArrayList<PokemonModel> myPokemons;
public PokemonAdapter(Context context, ArrayList<PokemonModel> myPokemons) {
this.context = context;
this.myPokemons = myPokemons;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.item_pokemon, viewGroup, false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull ViewHolder viewHolder, int position) {
PokemonModel pokemonModel = myPokemons.get(position);
viewHolder.tvNamaPokemon.setText(pokemonModel.getName());
viewHolder.tvSeriesPokemon.setText(pokemonModel.getSeries());
viewHolder.tvKelangkaanPokemon.setText(pokemonModel.getRarity());
Glide.with(context)
.load(pokemonModel.getImageUrl())
.into(viewHolder.ivPokemon);
}
@Override
public int getItemCount() {
return myPokemons.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
ImageView ivPokemon;
TextView tvNamaPokemon;
TextView tvKelangkaanPokemon;
TextView tvSeriesPokemon;
public ViewHolder(@NonNull View itemView) {
super(itemView);
ivPokemon = itemView.findViewById(R.id.img_pokemon);
tvNamaPokemon = itemView.findViewById(R.id.tv_nama_pokemon);
tvKelangkaanPokemon = itemView.findViewById(R.id.tv_kelangkaan_pokemon);
tvSeriesPokemon = itemView.findViewById(R.id.tv_pack_kartu);
}
}
}
package com.example.datapokemononline;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class PokemonController {
private MainActivity pokemonView;
SwipeRefreshLayout swipePokemon;
List<PokemonModel> pokemonModels;
OkHttpClient client;
public List<PokemonModel> getMyPokemon() {
return pokemonModels;
}
public PokemonController(MainActivity pokemonView) {
this.pokemonView = pokemonView;
}
public void populatePokemonData(){
pokemonModels = new ArrayList<>();
pokemonModels.clear();
//bawaan Okhttp3
Request request = new Request.Builder()
.url("https://api.pokemontcg.io/v1/cards")
.build();
client = new OkHttpClient();
Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
pokemonView.onFailure();
}
@Override
public void onResponse(Call call, Response response) throws IOException {
pokemonView.onResponse(response);
}
});
}
}
package com.example.datapokemononline;
import java.util.List;
public class PokemonModel {
private String id;
private String name;
private String imageUrl;
private String rarity;
private String series;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getImageUrl() {
return imageUrl;
}
public void setImageUrl(String imageUrl) {
this.imageUrl = imageUrl;
}
public String getRarity() {
return rarity;
}
public void setRarity(String rarity) {
this.rarity = rarity;
}
public String getSeries() {
return series;
}
public void setSeries(String series) {
this.series = series;
}
//inner class
public class PokemonResultModel{
private List<PokemonModel> cards;
public List<PokemonModel> getCards() {
return cards;
}
public void setCards(List<PokemonModel> cards) {
this.cards = cards;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment