Professional Documents
Culture Documents
select.php
<?php
include "koneksi.php";
$json = array();
while($row = mysql_fetch_assoc($query)){
$json[] = $row;
}
echo json_encode($json);
mysql_close($connect);
?>
delete.php
<?php
include "koneksi.php";
class emp{}
if (empty($id)) {
$response = new emp();
$response->success = 0;
$response->message = "Error hapus Data";
die(json_encode($response));
} else {
$query = mysql_query("DELETE FROM kontak WHERE id='".$id."'");
if ($query) {
$response = new emp();
$response->success = 1;
$response->message = "Data berhasil di hapus";
die(json_encode($response));
} else{
$response = new emp();
$response->success = 0;
$response->message = "Error hapus Data";
die(json_encode($response));
}
}
?>
edit.php
<?php
include "koneksi.php";
class emp{}
if (empty($id)) {
$response = new emp();
$response->success = 0;
$response->message = "Error Mengambil Data";
die(json_encode($response));
} else {
$query = mysql_query("SELECT * FROM kontak WHERE id='".$id."'");
$row = mysql_fetch_array($query);
if (!empty($row)) {
$response = new emp();
$response->success = 1;
$response->id = $row["id"];
$response->nama = $row["nama"];
$response->tlp = $row["tlp"];
$response->email = $row["email"];
die(json_encode($response));
} else{
$response = new emp();
$response->success = 0;
$response->message = "Error Mengambil Data";
die(json_encode($response));
}
}
?>
update.php
<?php
include "koneksi.php";
class emp{}
if ($query) {
$response = new emp();
$response->success = 1;
$response->message = "Data berhasil di update";
die(json_encode($response));
} else{
$response = new emp();
$response->success = 0;
$response->message = "Error update Data";
die(json_encode($response));
}
}
?>
insert.php
<?php
include "koneksi.php";
class emp{}
if ($query) {
$response = new emp();
$response->success = 1;
$response->message = "Data berhasil di simpan";
die(json_encode($response));
} else{
$response = new emp();
$response->success = 0;
$response->message = "Error simpan Data";
die(json_encode($response));
}
}
?>
<application
android:name=".app.AppController"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</manifest>
Buat MainActivity.java
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.StringRequest;
import com.azhar.crud.adapter.KontakAdapter;
import com.azhar.crud.app.AppController;
import com.azhar.crud.data.Data;
import com.azhar.crud.util.Server;
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;
/**
* Created by Azhar Rivaldi on 27/06/2019.
*/
Toolbar toolbar;
FloatingActionButton fab;
ListView list;
SwipeRefreshLayout swipe;
List itemList = new ArrayList();
KontakAdapter adapter;
int success;
AlertDialog.Builder dialog;
LayoutInflater inflater;
View dialogView;
EditText txt_id, txt_nama, txt_tlp, txt_email;
String id, nama, tlp, email;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
swipe.post(new Runnable() {
@Override
public void run() {
swipe.setRefreshing(true);
itemList.clear();
adapter.notifyDataSetChanged();
callVolley();
}
}
);
// listview ditekan lama akan menampilkan dua pilihan edit atau delete data
list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(final AdapterView parent, View view,
final int position, long id) {
// TODO Auto-generated method stub
final String idx = itemList.get(position).getId();
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
switch (which) {
case 0:
edit(idx);
break;
case 1:
delete(idx);
break;
}
}
}).show();
return false;
}
});
@Override
public void onRefresh() {
itemList.clear();
adapter.notifyDataSetChanged();
callVolley();
}
if (!idx.isEmpty()) {
txt_id.setText(idx);
txt_nama.setText(namax);
txt_tlp.setText(tlpx);
txt_email.setText(emailx);
} else {
kosong();
}
@Override
public void onClick(DialogInterface dialog, int which) {
id = txt_id.getText().toString();
nama = txt_nama.getText().toString();
tlp = txt_tlp.getText().toString();
email = txt_email.getText().toString();
simpan_update();
dialog.dismiss();
}
});
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
kosong();
}
});
dialog.show();
}
// Parsing json
for (int i = 0; i < response.length(); i++) {
try {
JSONObject obj = response.getJSONObject(i);
item.setId(obj.getString(TAG_ID));
item.setNama(obj.getString(TAG_NAMA));
item.setTlp(obj.getString(TAG_TLP));
item.setEmail(obj.getString(TAG_EMAIL));
swipe.setRefreshing(false);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
swipe.setRefreshing(false);
}
});
if (id.isEmpty()) {
url = url_insert;
} else {
url = url_update;
}
@Override
public void onResponse(String response) {
Log.d(TAG, "Response: " + response.toString());
try {
JSONObject jObj = new JSONObject(response);
success = jObj.getInt(TAG_SUCCESS);
callVolley();
kosong();
Toast.makeText(MainActivity.this,
jObj.getString(TAG_MESSAGE), Toast.LENGTH_LONG).show();
adapter.notifyDataSetChanged();
} else {
Toast.makeText(MainActivity.this,
jObj.getString(TAG_MESSAGE), Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
// JSON error
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "Error: " + error.getMessage());
Toast.makeText(MainActivity.this, error.getMessage(),
Toast.LENGTH_LONG).show();
}
})
@Override
protected Map getParams() {
// Posting parameters ke post url
Map params = new HashMap();
if (id.isEmpty()) {
params.put("nama", nama);
params.put("tlp", tlp);
params.put("email", email);
} else {
params.put("id", id);
params.put("nama", nama);
params.put("tlp", tlp);
params.put("email", email);
}
return params;
}
};
AppController.getInstance().addToRequestQueue(strReq, tag_json_obj);
}
@Override
public void onResponse(String response) {
Log.d(TAG, "Response: " + response.toString());
try {
JSONObject jObj = new JSONObject(response);
success = jObj.getInt(TAG_SUCCESS);
adapter.notifyDataSetChanged();
} else {
Toast.makeText(MainActivity.this,
jObj.getString(TAG_MESSAGE), Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
// JSON error
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "Error: " + error.getMessage());
Toast.makeText(MainActivity.this, error.getMessage(),
Toast.LENGTH_LONG).show();
}
}) {
@Override
protected Map getParams() {
// Posting parameters ke post url
Map params = new HashMap();
params.put("id", idx);
return params;
}
};
AppController.getInstance().addToRequestQueue(strReq, tag_json_obj);
}
@Override
public void onResponse(String response) {
Log.d(TAG, "Response: " + response.toString());
try {
JSONObject jObj = new JSONObject(response);
success = jObj.getInt(TAG_SUCCESS);
callVolley();
Toast.makeText(MainActivity.this,
jObj.getString(TAG_MESSAGE), Toast.LENGTH_LONG).show();
adapter.notifyDataSetChanged();
} else {
Toast.makeText(MainActivity.this,
jObj.getString(TAG_MESSAGE), Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
// JSON error
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "Error: " + error.getMessage());
Toast.makeText(MainActivity.this, error.getMessage(),
Toast.LENGTH_LONG).show();
}
}) {
@Override
protected Map getParams() {
// Posting parameters ke post url
Map params = new HashMap();
params.put("id", idx);
return params;
}
};
AppController.getInstance().addToRequestQueue(strReq, tag_json_obj);
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.azhar.crud.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
android:src="@drawable/ic_fab_add" />
</android.support.design.widget.CoordinatorLayout>
Buat content_main.xml
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipe_refresh_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ListView
android:id="@+id/list"
android:divider="@color/list_divider"
android:dividerHeight="2dp"
android:listSelector="@drawable/list_row_selector"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</android.support.v4.widget.SwipeRefreshLayout>
</RelativeLayout>
Buat form_kontak.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin" >
<EditText
android:id="@+id/txt_id"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:drawable/editbox_background_normal"/>
<EditText
android:id="@+id/txt_nama"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:hint="Nama"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:drawable/editbox_background_normal"/>
<EditText
android:id="@+id/txt_tlp"
android:layout_marginBottom="10dp"
android:hint="Telp"
android:inputType="textMultiLine"
android:minLines="3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:drawable/editbox_background_normal"/>
<EditText
android:id="@+id/txt_email"
android:layout_marginBottom="10dp"
android:hint="Email"
android:inputType="textMultiLine"
android:minLines="3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:drawable/editbox_background_normal"/>
</LinearLayout>
Buat list_row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:padding="16dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"/>
<TextView
android:id="@+id/nama"
android:layout_marginBottom="5dp"
android:textStyle="bold"
android:text="Nama"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/tlp"
android:text="Telp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/email"
android:text="Email"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Buat KontakAdapter.java
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.azhar.crud.R;
import com.azhar.crud.data.Data;
import java.util.List;
/**
* Created by Azhar Rivaldi on 27/06/2019.
*/
@Override
public int getCount() {
return items.size();
}
@Override
public Object getItem(int location) {
return items.get(location);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (inflater == null)
inflater = (LayoutInflater)
activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
id.setText(data.getId());
nama.setText(data.getNama());
tlp.setText(data.getTlp());
email.setText(data.getEmail());
return convertView;
}
Buat AppController.java
import android.app.Application;
import android.text.TextUtils;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
/**
* Created by Azhar Rivaldi on 27/06/2019.
*/
@Override
public void onCreate() {
super.onCreate();
mInstance = this;
}
return mRequestQueue;
}
}
Data.java
public class Data {
private String id, nama, tlp, email;
public Data() {
}
Server.java
public class Server {
/* Jika IP 10.0.2.2, itu adalah IP Address localhost EMULATOR ANDROID STUDIO,
Ganti IP Address tersebut dengan IP Laptop Anda. Apabila di RUN di HP. HP dan
Laptop harus 1 jaringan */
public static final String URL = "http://192.168.115.114/android/biodata/";
}