Menampilkan Data Menggunakan RecyclerView (Retrofit) Part 1
Get link
Facebook
X
Pinterest
Email
Other Apps
Hello Sobat, Hari ini saya akan memberikan tutorial untuk menampilkan data menggunakan RecylerView dengan HTTP REQUEST menggunakan library Retrofit untuk lebih jelasnya mengenai library ini sobat bisa kunjungi situs resminya disini, Tanpa berpikir panjang karna saya sudah kehabisan kata-kata saya akan memberikan langkah-langkah untuk membuat aplikasinya perhatikan iya.
Membuat Databases
Sobat copy code databases dibawah ke mysql
/*
Navicat MySQL Data Transfer
Source Server : tanwir
Source Server Version : 50624
Source Host : localhost:3306
Source Database : db_blogspot
Target Server Type : MYSQL
Target Server Version : 50624
File Encoding : 65001
Date: 2017-07-03 09:16:54
*/
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for siswa
-- ----------------------------
DROP TABLE IF EXISTS `siswa`;
CREATE TABLE `siswa` (
`nis` int(11) NOT NULL,
`nama` varchar(255) DEFAULT NULL,
PRIMARY KEY (`nis`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- ----------------------------
-- Records of siswa
-- ----------------------------
INSERT INTO `siswa` VALUES ('1111', 'CodeTR');
INSERT INTO `siswa` VALUES ('2222', 'Randi');
INSERT INTO `siswa` VALUES ('3333', 'Tanwir');
INSERT INTO `siswa` VALUES ('5555', 'Hama');
INSERT INTO `siswa` VALUES ('6666', 'Cinta');
INSERT INTO `siswa` VALUES ('7777', 'Naruto');
INSERT INTO `siswa` VALUES ('8888', 'Budi');
INSERT INTO `siswa` VALUES ('9999', 'Tutu');
INSERT INTO `siswa` VALUES ('67676', 'hgfh');
Membuat item dengan nama item_siswa.xml dan masukan source code berikut
<?xml version="1.0" encoding="utf-8"?><!--
~ Copyright (c) 2016. Tanwir. All Rights Reserver.
~ <p>
~ Save to the extent permitted by law, you may not use,copy,modify,
~ distribute or create derivative works of this material or any part
~ of it without the prior written consent of Tanwir.
~ <p>
~ The above copyright notice and this permission notice shall be included in
~ all copies or substantial portions of the Software.
--><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="80dp"><ImageViewandroid:id="@+id/tvPhoto"android:layout_width="60dp"android:layout_height="60dp"android:layout_margin="8dp"android:scaleType="centerCrop"android:src="@mipmap/ic_launcher" /><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_alignParentBottom="true"android:layout_alignParentEnd="true"android:layout_alignParentRight="true"android:layout_alignParentTop="true"android:layout_marginLeft="80dp"android:gravity="center_vertical"android:orientation="horizontal"><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="match_parent"android:gravity="center_vertical"android:orientation="vertical"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:ellipsize="end"android:maxLines="1"android:paddingRight="16dp"android:text="Nis" /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="10dp"android:paddingRight="16dp"android:text="Nama " /></LinearLayout><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="match_parent"android:layout_weight="1"android:gravity="center_vertical"android:orientation="vertical"><TextViewandroid:id="@+id/tvNis"android:layout_width="match_parent"android:layout_height="wrap_content"android:ellipsize="end"android:maxLines="1"android:paddingRight="16dp" /><TextViewandroid:id="@+id/tvNama"android:layout_marginTop="10dp"android:layout_width="match_parent"android:layout_height="wrap_content"android:paddingRight="16dp" /></LinearLayout></LinearLayout></RelativeLayout><Viewandroid:layout_width="match_parent"android:background="@android:color/darker_gray"android:layout_height="1dp"/></LinearLayout>
Jika sudah selesai melakukan design pada tampilan layout selanjutnya melakukan penambahan uses permission pada Manifest
Tambahkan di folder adapter dengan nama SAdapter.java dan masukan source code
/* * Copyright (c) 2016. Tanwir. All Rights Reserver. * <p> * Save to the extent permitted by law, you may not use,copy,modify, * distribute or create derivative works of this material or any part * of it without the prior written consent of Tanwir. * <p> * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. */package com.codetr.tanwir.codetr.adapter;
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.codetr.tanwir.codetr.R;
import com.codetr.tanwir.codetr.model.Siswa;
import java.util.ArrayList;
import java.util.List;
/** * * Created by tanwir on 01/07/2017. */ public class SAdapter extends RecyclerView.Adapter<SAdapter.Holder> {
private final SiswaClickListener nListener;
private List<Siswa> lSiswa;
public SAdapter(SiswaClickListener listener) {
lSiswa = new ArrayList<>();
nListener = listener;
}
@Override
public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
View row = LayoutInflater.from(parent.getContext()).inflate(R.layout.items_siswa, parent, false);
return new Holder(row);
}
@Override
Tambahkan di folder model dengan nama Siswa.java dan masukan sourcode
/* * Copyright (c) 2016. Tanwir. All Rights Reserver. * <p> * Save to the extent permitted by law, you may not use,copy,modify, * distribute or create derivative works of this material or any part * of it without the prior written consent of Tanwir. * <p> * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. */package com.codetr.tanwir.codetr.model;
/** * * Created by tanwir on 01/07/2017. */public class Siswa {
public String nis;
public String nama;
public String photo;
public String getNis() {
return nis;
}
public void setNis(String nis) {
this.nis = nis;
}
public String getNama() {
return nama;
}
public void setNama(String nama) {
this.nama = nama;
}
public String getPhoto() {
return photo;
}
public void setPhoto(String photo) {
this.photo = photo;
}
}
Tambahkan di folder network tiga file java dengan nama Config.java, DataProvider.java dan DataService.java dan masukan sourecode
Config.java
/* * Copyright (c) 2016. Tanwir. All Rights Reserver. * <p> * Save to the extent permitted by law, you may not use,copy,modify, * distribute or create derivative works of this material or any part * of it without the prior written consent of Tanwir. * <p> * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. */package com.codetr.tanwir.codetr.network;
/** * * Created by tanwir on 01/07/2017. */public class Config {
public static final String BASE_URL = "https://blogspot-wtanwir.c9users.io/";
}
DataProvider.java
/* * Copyright (c) 2016. Tanwir. All Rights Reserver. * <p> * Save to the extent permitted by law, you may not use,copy,modify, * distribute or create derivative works of this material or any part * of it without the prior written consent of Tanwir. * <p> * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. */package com.codetr.tanwir.codetr.network;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
/** * * Created by tanwir on 01/07/2017. */public class DataProvider {
private DataService nService;
private Retrofit mRetrofitClient;
/** * config Retrofit in initialization */public DataProvider() {
OkHttpClient httpClient = new OkHttpClient();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Config.BASE_URL)
.client(httpClient)
.addConverterFactory(GsonConverterFactory.create())
.build();
nService = retrofit.create(DataService.class);
}
public DataService getTService() {
return nService;
}
public Retrofit getRetrofitClient() {
return mRetrofitClient;
}
}
DataService.java
/* * Copyright (c) 2016. Tanwir. All Rights Reserver. * <p> * Save to the extent permitted by law, you may not use,copy,modify, * distribute or create derivative works of this material or any part * of it without the prior written consent of Tanwir. * <p> * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. */package com.codetr.tanwir.codetr.network;
import com.codetr.tanwir.codetr.model.Siswa;
import java.util.List;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.http.Field;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.GET;
import retrofit2.http.POST;
import retrofit2.http.Path;
import retrofit2.http.Query;
/** * * Created by tanwir on 01/07/2017. */public interface DataService {
Privacy Policy Last updated: September 20, 2024 This Privacy Policy describes Our policies and procedures on the collection, use and disclosure of Your information when You use the Service and tells You about Your privacy rights and how the law protects You. We use Your Personal data to provide and improve the Service. By using the Service, You agree to the collection and use of information in accordance with this Privacy Policy. This Privacy Policy has been created with the help of the Privacy Policy Generator . Interpretation and Definitions Interpretation The words of which the initial letter is capitalized have meanings defined under the following conditions. The following definitions shall have the same meaning regardless of whether they appear in singular or in plural. Definitions For the purposes of this Privacy Policy: Account means a unique account created for You to access our Service or parts of our Service. Affiliate means an entity that controls, is control...
Privacy Policy Privacy Policy This privacy policy applies to the belmandiyah app (hereby referred to as "Application") for mobile devices that was created by tanwir (hereby referred to as "Service Provider") as a Free service. This service is intended for use "AS IS". Information Collection and Use The Application collects information when you download and use it. This information may include information such as Your device's Internet Protocol address (e.g. IP address) The pages of the Application that you visit, the time and date of your visit, the time spent on those pages The time spent on the Application The operating system you use on your mobile device The Application does not gather precise information about the location of your mobile device. The Application collects your device's location, which helps the Service Provider determine your approximate geographical location and make use of i...
Comments
Post a Comment