NIM : 18650021
NAMA : FATMA INDIKA SARI
KELAS : PRAKTIKUM PEMROGRAMAN MOBILE (G)
1. Pertama buka aplikasi android studio untuk membuat projek.. klik start new project dan beri
nama projek pra_uts_nim_nama_kelas sesuai perintah di modul.
2. Kedua maka akan muncul tampilan seperti pada gambar berikut.. dan langkah selanjutnya
adalah menambah source kode implementation seperti gambar di bawah ini
3. Ketiga kita membuat kelas java pada projek kita dengan nama “Constants” fungsi URL dari kelas
ini untuk mengkoneksikan ke database yang sudah ada
4. Ke empat kita membuat kelas dengan nama “Detail data” .. dalam kelas ini kita menambahkan
source kode untuk : id, nama, phone, dan alamat yang nantinya akan muncul untuk
menambahkan data.
5. Ke lima kita menambahkan kelas java pada projek kita dengan nama “EditActivity” dalam kelas
ini terdapat source kode yang nantinya akan dapat mengedit data yang sudah kita inputkan.
6. Ke enam kita membuat kelas java pada projek kita dengan nama “ListActivity” yang nantinya
data-data yang sudah di masukkan akan muncul di halaman pertama sebelum kita add data dan
setelah add data.
7. Ke tujuh di kelas “MainActivity” kita menambah source kode yang seperti pada perintah modul
8. Ke delapan kita membuat kelas java pada projek dengan nama “Model”
9. Ke sembilan kita membuat kelas java dengan nama “MyAdapter” seperti pada gambar berikut
ini
10. Ke sepuluh ini membuat kelas terakhir java dengan nama “RequestHandler” seeprti pada
gambar berikut ini.
11. Ke sebeleas kita membuat layout dengan nama “activity_detail_data.xml” yang nantinya data-
data secara detail akan muncul.
12. Ke duabelas kita membuat layout dengan nama “Activity_Edit.xml” seperti pada gambar berikut
ini yang nantinya akan menampilkan tampilan sebuah layout untuk mengedit data.
13. Ke tigabelas kita membuat layout dengan nama “Activity_list.xml” yang nantinya pada kelas ini
akan muncul layout dengan tampilan list-list data yang telah di masukkan atau data yang telah
masuk ke database yang sudah terkoneksikan.
14. Ke empatbelas kita membuat layout dengan nama “list_item.xml” seperti pada gambar berikut
ini.
15. Ke limabelas kita menambahkan source kode pada AndroidManifest.xml seperti pada perintah
di modul.
16. Ke enambelas kita menambahkan folder “menu” dan menambahkan layout “add” untuk
tampilan add pada halaman yang akan di tambahkan data.
17. Ke tujuhbelas membuat folder dengan nama “xml” dan menambahkan layout “button” dan
“input” yang nantinya akan memasukkan data ke dalam database dan data tersebut akan ada di
list data .
18. Ke delapan belas disini saya menambahkan gambar icon untuk tampilan “add” .
Hasil :
Pada Saat di run akan muncul gambar seperti di bawah ini, list-list data yang telah masuk akan muncul
pada halaman layout, untuk emnambahkan data klik pojok kanan atas ada icon + untuk menambahkan
data kita.
android {
compileSdkVersion 30
buildToolsVersion "30.0.2"
defaultConfig {
applicationId "com.example.pra_uts_18650021_fatmaindikasari_g"
minSdkVersion 16
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'),
'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
//noinspection GradleCompatible
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:2.0.3'
implementation 'com.android.volley:volley:1.1.1'
//noinspection GradleCompatible
implementation 'com.android.support:cardview-v7:28.0.0'
//noinspection GradleCompatible
implementation 'com.android.support:recyclerview-v7:30.0.2'
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', {
exclude group: 'com.android.support', module: 'support-annotations'
}
)
}
--- Source Code Class Constants ----
package com.example.pra_uts_18650021_fatmaindikasari_g;
package com.example.pra_uts_18650021_fatmaindikasari_g;
import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail_data);
uid = (TextView) findViewById(R.id.uid);
name = (TextView) findViewById(R.id.name);
address = (TextView) findViewById(R.id.address);
phone = (TextView) findViewById(R.id.phone);
uid.setText(getIntent().getStringExtra("uid"));
name.setText(getIntent().getStringExtra("name"));
address.setText(getIntent().getStringExtra("address"));
phone.setText(getIntent().getStringExtra("phone"));
}
}
import java.util.HashMap;
import java.util.Map;
uid = findViewById(R.id.uid);
name = findViewById(R.id.name);
address = findViewById(R.id.address);
phone = findViewById(R.id.phone);
progressDialog = new ProgressDialog(this);
button = findViewById(R.id.button);
Id = getIntent().getStringExtra("id");
uid.setText(getIntent().getStringExtra("uid"));
name.setText(getIntent().getStringExtra("name"));
address.setText(getIntent().getStringExtra("address"));
phone.setText(getIntent().getStringExtra("phone"));
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Uid = uid.getText().toString();
Name = name.getText().toString();
Address = address.getText().toString();
Phone = phone.getText().toString();
if(TextUtils.isEmpty(Uid)){
uid.setError("UID cannot be empty");
valid = false;
}else {
valid = true;
if (TextUtils.isEmpty(Name)){
name.setError("Name cannot be empty");
valid = false;
}else{
valid = true;
if(TextUtils.isEmpty(Address)){
address.setError("Address cannot be empty");
valid = false;
}else{
valid = true;
if(TextUtils.isEmpty(Phone)){
phone.setError("Contact number cannot be empty");
valid = false;
}else{
valid = true;
}
}
}
}
if(valid){
progressDialog.setMessage("Loading");
progressDialog.show();
RequestHandler.getInstance(EditActivity.this).addToRequestQueue(stringRequest);
}
}
});
}
}
--- Source Code Class ListActivity ----
package com.example.pra_uts_18650021_fatmaindikasari_g;
import android.app.ProgressDialog;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ListActivity extends AppCompatActivity{
public static ListActivity ma;
protected Cursor cursor;
ArrayList<Model> thelist;
ListView listview;
List<Model> listItems;
private RecyclerView recyclerView;
private RecyclerView.Adapter adapter;
ProgressDialog progressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
getSupportActionBar().setTitle("Data");
recyclerView = (RecyclerView) findViewById(R.id.list);
recyclerView.setLayoutManager(new LinearLayoutManager( ListActivity.this));
progressDialog = new ProgressDialog( this);
listItems = new ArrayList<>();
ma = this;
refresh_list();
}
recyclerView.setItemAnimator(new DefaultItemAnimator());
progressDialog.setMessage("Loading");
progressDialog.show();
progressDialog.hide();
JSONObject jsonObject = new JSONObject(response);
JSONArray jsonArray = jsonObject.getJSONArray("data");
Toast.makeText(ListActivity.this,
jsonObject.getString("message"), Toast.LENGTH_SHORT).show();
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject o = jsonArray.getJSONObject(i);
Model item = new Model(
o.getString("id"),
o.getString("uid"),
o.getString("name"),
o.getString("phone"),
o.getString("address")
);
listItems.add(item);
adapter = new MyAdapter(listItems, getApplicationContext());
recyclerView.setAdapter(adapter);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
progressDialog.hide();
Toast.makeText(ListActivity.this, "Failed",
Toast.LENGTH_SHORT).show();
}
}) {
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("name", "kl");
return params;
}
};
RequestHandler.getInstance(ListActivity.this).addToRequestQueue(stringRequest);
}
}
package com.example.pra_uts_18650021_fatmaindikasari_g;
import androidx.appcompat.app.AppCompatActivity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;
import android.os.Bundle;
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Uid = uid.getText().toString();
Name = name.getText().toString();
Address = address.getText().toString();
Phone = phone.getText().toString();
if(TextUtils.isEmpty(Uid)){
uid.setError("UID Cannot be Empty");
valid = false;
}else {
valid = true;
if(TextUtils.isEmpty(Name)){
name.setError("Name Cannot be Empty");
valid = false;
}else {
valid = true;
if(TextUtils.isEmpty(Address)){
address.setError("Address Cannot be Empty");
valid = false;
}else {
valid = true;
if(TextUtils.isEmpty(Phone)){
phone.setError("Contact Number Cannot be Empty");
valid = false;
}else {
valid = true;
}
}
}
}
if(valid){
progressDialog.setMessage("Loading");
progressDialog.show();
RequestHandler.getInstance(MainActivity.this).addToRequestQueue(stringRequest);
}
}
});
}
--- Source Code Class Model ----
package com.example.pra_uts_18650021_fatmaindikasari_g;
public Model(String id, String uid, String name, String phone, String address){
this.id=id;
this.uid=uid;
this.name=name;
this.phone=phone;
this.address=address;
}
public String getId(){
return id;
}
public String getUid(){
return uid;
}
public String getName(){
return name;
}
public String getPhone(){
return phone;
}
public String getAddress(){
return address;
}
}
package com.example.pra_uts_18650021_fatmaindikasari_g;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder>{
private List<Model> listItems;
private Context context;
private ProgressDialog dialog;
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item,
parent, false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
final Model listItem = listItems.get(position);
holder.id.setText(listItem.getId());
holder.uid.setText(listItem.getUid());
holder.name.setText(listItem.getName());
holder.address.setText(listItem.getAddress());
holder.phone.setText(listItem.getPhone());
holder.card_view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View view) {
Intent intent;
AlertDialog.Builder builder = new
AlertDialog.Builder(view.getContext());
final ProgressDialog dialog = new ProgressDialog(view.getContext());
dialog.setMessage("Loading Delete Data");
final CharSequence[] dialogitem = {"View Data","Edit Data","Delete
Data"};
builder.setTitle(listItem.getName());
builder.setItems(dialogitem, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
switch (i){
case 0 :
Intent intent = new Intent(view.getContext(),
DetailData.class);
intent.putExtra("id", listItem.getId());
intent.putExtra("uid",listItem.getUid());
intent.putExtra("name",listItem.getName());
intent.putExtra("address",listItem.getAddress());
intent.putExtra("phone", listItem.getPhone());
view.getContext().startActivity(intent);
break;
case 1 :
@Override
public void onClick(DialogInterface
dialogInterface, int i) {
dialog.show();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError
error) {
dialog.hide();
dialog.dismiss();
}
}){
protected HashMap<String, String>
getParams() throws AuthFailureError {
Map<String, String> params= new
HashMap<>();
params.put("id",listItem.getId());
return (HashMap<String, String>)
params;
}
};
RequestHandler.getInstance(view.getContext()).addToRequestQueue(stringRequest);
dialogInterface.dismiss();
}
});
builderDel.setNegativeButton("Cancel", new
DialogInterface.OnClickListener() {
@Override
builderDel.create().show();
break;
}
}
});
builder.create().show();
}
});
}
@Override
public int getItemCount() {
return listItems.size();
}
}
package com.example.pra_uts_18650021_fatmaindikasari_g;
import android.content.Context;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
public class RequestHandler {
private static RequestHandler mInstance;
private RequestQueue mRequestQueue;
private static Context mCtx;
mRequestQueue = Volley.newRequestQueue(mCtx.getApplicationContext());
}
return mRequestQueue;
}
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="14dp"
android:orientation="vertical"
android:padding="20dp">
<TextView
android:id="@+id/textView12"
android:textSize="22sp"
android:layout_marginBottom="30dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="View Data"
android:textColor="#546e7a"/>
<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="UID"
android:textColor="#222"
android:textStyle="bold"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/uid"
android:textSize="19sp"
android:layout_marginBottom="20dp"/>
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Name"
android:textColor="#222"
android:textStyle="bold"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/name"
android:textSize="19sp"
android:layout_marginBottom="20dp"/>
<TextView
android:id="@+id/textView4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Contact Number"
android:textColor="#222"
android:textStyle="bold"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/phone"
android:textSize="19sp"
android:layout_marginBottom="20dp"/>
<TextView
android:id="@+id/textView5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Address"
android:textColor="#222"
android:textStyle="bold"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/address"
android:textSize="19sp"
android:layout_marginBottom="20dp"/>
</LinearLayout>
</ScrollView>
</RelativeLayout>
<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="UID"
android:textColor="#546e7a"/>
<EditText
android:id="@+id/uid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:padding="10dp"
android:background="@xml/input"
android:layout_marginBottom="10dp"
android:inputType="number"/>
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Name"
android:textColor="#546e7a"/>
<EditText
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:padding="10dp"
android:background="@xml/input"
android:layout_marginBottom="10dp"
android:inputType="text"/>
<TextView
android:id="@+id/textView4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Contact Number"
android:textColor="#546e7a"/>
<EditText
android:id="@+id/phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:padding="10dp"
android:background="@xml/input"
android:layout_marginBottom="10dp"
android:inputType="phone"/>
<TextView
android:id="@+id/textView5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Address"
android:textColor="#546e7a"/>
<EditText
android:id="@+id/address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:padding="10dp"
android:background="@xml/input"
android:layout_marginBottom="10dp"
android:inputType="text"/>
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Update Data"
android:background="@xml/button"
android:textColor="#fff"/>
</LinearLayout>
</ScrollView>
</RelativeLayout>
</LinearLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="14dp"
android:orientation="vertical"
android:padding="20dp">
<TextView
android:id="@+id/textView12"
android:textSize="22sp"
android:layout_marginBottom="30dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Add Data"
android:textColor="#546e7a"/>
<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="UID"
android:textColor="#546e7a"/>
<EditText
android:id="@+id/uid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:padding="10dp"
android:background="@xml/input"
android:layout_marginBottom="10dp"
android:inputType="number"/>
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Name"
android:textColor="#546e7a"/>
<EditText
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:padding="10dp"
android:background="@xml/input"
android:layout_marginBottom="10dp"
android:inputType="text"/>
<TextView
android:id="@+id/textView4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Contact Number"
android:textColor="#546e7a"/>
<EditText
android:id="@+id/phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:padding="10dp"
android:background="@xml/input"
android:layout_marginBottom="10dp"
android:inputType="phone"/>
<TextView
android:id="@+id/textView5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Address"
android:textColor="#546e7a"/>
<EditText
android:id="@+id/address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:padding="10dp"
android:layout_marginBottom="10dp"
android:inputType="text"/>
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Insert Data"
android:background="@xml/button"
android:textColor="#fff"/>
</LinearLayout>
</ScrollView>
</RelativeLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="3dp"
android:paddingBottom="3dp">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="4dp"
android:elevation="14dp"
android:id="@+id/card_view"
android:layout_marginBottom="2dp">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="8dp">
<TextView
android:id="@+id/uid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="3dp"
android:textSize="16dp"
android:textColor="#2196f3"/>
<TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="12dp"
android:textColor="#878586"/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#2196f3"
android:visibility="invisible"/>
<TextView
android:id="@+id/phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10dp" android:textColor="#aeaeae"/>
<TextView
android:visibility="invisible"
android:layout_alignParentRight="true"
android:id="@+id/address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10dp"
android:textColor="#aeaeae"/>
</RelativeLayout>
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
<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="com.example.pra_uts_18650021_fatmaindikasari_g.MainActivity"
android:parentActivityName="com.example.pra_uts_18650021_fatmaindikasari_g.ListActivi
ty" />
<activity
android:name="com.example.pra_uts_18650021_fatmaindikasari_g.ListActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.pra_uts_18650021_fatmaindikasari_g.DetailData"
android:parentActivityName="com.example.pra_uts_18650021_fatmaindikasari_g.ListActivi
ty" />
<activity
android:name="com.example.pra_uts_18650021_fatmaindikasari_g.EditActivity"
android:parentActivityName="com.example.pra_uts_18650021_fatmaindikasari_g.ListActivi
ty"
></activity>
</application>
</manifest>