Apa itu Aplikasi Kode POS?
Aplikasi Kode POS merupakan yang digunakan untuk mencari kode POS di Indonesia. Aplikasi sederhana ini menggunakan database yang sebelumnya disimpan di Firebase.
Daftar propinsi
https://kodepos-2d475.firebaseio.com/list_propinsi.json?print=pretty
Daftar kota/kabupaten
https://kodepos-2d475.firebaseio.com/list_kotakab/{key propinsi}.json?print=pretty
Daftar kecamatan, kelurahan & kodepos
https://kodepos-2d475.firebaseio.com/kota_kab/{key kota}.json?print=pretty
Buka Android Studio, Create New Project buat file2. class2 seperti gambar dibawah :
Aplikasi Kode POS merupakan yang digunakan untuk mencari kode POS di Indonesia. Aplikasi sederhana ini menggunakan database yang sebelumnya disimpan
dimens.xml
<?xml version=“1.0” encoding=“utf-8”?>
<resources>
<dimen name=“margin_16dp”>16dp</dimen>
</resources>
<resources>
<dimen name=“margin_16dp”>16dp</dimen>
</resources>
activity_main.xml
<?xml version=“1.0” encoding=“utf-8”?>
<android.support.constraint.ConstraintLayout 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”
tools:context=“.MainActivity”> <ListView
android:id=“@+id/list_pos”
android:layout_width=“match_parent”
android:layout_height=“wrap_content” /> </android.support.constraint.ConstraintLayout>
<android.support.constraint.ConstraintLayout 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”
tools:context=“.MainActivity”> <ListView
android:id=“@+id/list_pos”
android:layout_width=“match_parent”
android:layout_height=“wrap_content” /> </android.support.constraint.ConstraintLayout>
list_daerah.xml //untuk menampilkan daftar propinsi dan kota.
<?xml version=“1.0” encoding=“utf-8”?>
<LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”
android:orientation=“vertical”
android:padding=“@dimen/margin_16dp”
android:layout_width=“match_parent”
android:layout_height=“match_parent”> <TextView
android:id=“@+id/name”
android:freezesText=“true”
android:layout_width=“fill_parent”
android:layout_height=“wrap_content”
style=“@style/TextAppearance.AppCompat.Subhead“ /> </LinearLayout>
<LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”
android:orientation=“vertical”
android:padding=“@dimen/margin_16dp”
android:layout_width=“match_parent”
android:layout_height=“match_parent”> <TextView
android:id=“@+id/name”
android:freezesText=“true”
android:layout_width=“fill_parent”
android:layout_height=“wrap_content”
style=“@style/TextAppearance.AppCompat.Subhead“ /> </LinearLayout>
activity_kota.xml //EditText untuk filter.
<?xml version=“1.0” encoding=“utf-8”?>
<android.support.constraint.ConstraintLayout 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”
tools:context=“com.kodepos.KotaActivity”> <LinearLayout
android:orientation=“vertical”
android:layout_width=“fill_parent”
android:layout_height=“wrap_content”> <EditText android:id=“@+id/filter”
android:layout_width=“fill_parent”
android:layout_height=“wrap_content”
android:padding=“@dimen/margin_16dp”
android:hint=“Filter” /> <ListView
android:id=“@+id/list_pos”
android:layout_width=“fill_parent”
android:layout_height=“wrap_content” /> </LinearLayout> </android.support.constraint.ConstraintLayout>
<android.support.constraint.ConstraintLayout 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”
tools:context=“com.kodepos.KotaActivity”> <LinearLayout
android:orientation=“vertical”
android:layout_width=“fill_parent”
android:layout_height=“wrap_content”> <EditText android:id=“@+id/filter”
android:layout_width=“fill_parent”
android:layout_height=“wrap_content”
android:padding=“@dimen/margin_16dp”
android:hint=“Filter” /> <ListView
android:id=“@+id/list_pos”
android:layout_width=“fill_parent”
android:layout_height=“wrap_content” /> </LinearLayout> </android.support.constraint.ConstraintLayout>
list_pos.xml //untuk menampilkan data kecamatan, kelurahan dan kodepos
<?xml version=“1.0” encoding=“utf-8”?>
<LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”
android:orientation=“vertical”
android:padding=“@dimen/margin_16dp”
android:layout_width=“match_parent”
android:layout_height=“match_parent”> <LinearLayout
android:layout_width=“fill_parent”
android:layout_height=“wrap_content”
android:orientation=“horizontal” > <TextView
android:id=“@+id/kecamatan”
android:freezesText=“true”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
style=“@style/TextAppearance.AppCompat.Subhead“ /> <TextView
android:id=“@+id/kodepos”
android:freezesText=“true”
style=“@style/TextAppearance.AppCompat.Subhead“
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:textColor=“@color/colorPrimary”
android:gravity=“end” /> </LinearLayout> <TextView
android:id=“@+id/kelurahan”
android:freezesText=“true”
android:layout_width=“fill_parent”
android:layout_height=“wrap_content” /> </LinearLayout>
<LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”
android:orientation=“vertical”
android:padding=“@dimen/margin_16dp”
android:layout_width=“match_parent”
android:layout_height=“match_parent”> <LinearLayout
android:layout_width=“fill_parent”
android:layout_height=“wrap_content”
android:orientation=“horizontal” > <TextView
android:id=“@+id/kecamatan”
android:freezesText=“true”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
style=“@style/TextAppearance.AppCompat.Subhead“ /> <TextView
android:id=“@+id/kodepos”
android:freezesText=“true”
style=“@style/TextAppearance.AppCompat.Subhead“
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:textColor=“@color/colorPrimary”
android:gravity=“end” /> </LinearLayout> <TextView
android:id=“@+id/kelurahan”
android:freezesText=“true”
android:layout_width=“fill_parent”
android:layout_height=“wrap_content” /> </LinearLayout>
MainActivity.java //mengambil data json daftar propinsi lalu menampilkannya di listview list_daerah.xml menggunakan SimpleAdapter.
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {
private String TAG = MainActivity.class.getSimpleName();
private ProgressDialog pDialog;
private ListView lv; // URL to get propinsi list JSON
final static String url = “https://kodepos-2d475.firebaseio.com/list_propinsi.json”; ArrayList<HashMap<String, String>> posList; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); // menampung data propinsi
posList = new ArrayList<>(); // listview untuk menampilkan daftar propinsi
lv = (ListView) findViewById(R.id.list_pos);
lv.setOnItemClickListener(this); // Get JSON data
new GetPost().execute();
} // Async task class to get json by making HTTP call
private class GetPost extends AsyncTask<Void, Void, Void> { @Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage(“Please wait…”);
pDialog.setCancelable(false);
pDialog.show(); } @Override
protected Void doInBackground(Void… arg0) {
HttpHandler sh = new HttpHandler(); // Making a request to url and getting response
String jsonStr = sh.makeServiceCall(url); Log.e(TAG, “Response from url: “ + jsonStr); if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr); // Parser flat object
Iterator<String> iter = jsonObj.keys();
while (iter.hasNext()) {
String key = iter.next();
try {
Object value = jsonObj.get(key);
String nama = value.toString(); // tmp hash map for single GetPost
HashMap<String, String> pos = new HashMap<>(); // adding each child node to HashMap key => value
pos.put(“name”, nama);
pos.put(“idp”, key); // adding pos to pos list
posList.add(pos); } catch (JSONException e) {
// Something went wrong!
}
}
} catch (final JSONException e) {
Log.e(TAG, “Json parsing error: “ + e.getMessage());
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
“Data tidak ada.”,
Toast.LENGTH_LONG)
.show();
}
}); }
} else {
Log.e(TAG, “Couldn’t get json from server.”);
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
“Terjadi kesalahan coba lain waktu.”,
Toast.LENGTH_LONG)
.show();
}
}); } return null;
} @Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss(); // Sorting a-z
Collections.sort(posList, new Comparator<HashMap< String,String >>() { @Override
public int compare(HashMap<String, String> lhs,
HashMap<String, String> rhs) {
return lhs.get(“name”).compareTo(rhs.get(“name”));
}
}); // Updating parsed JSON data into ListView
ListAdapter adapter = new SimpleAdapter(
MainActivity.this, posList,
R.layout.list_daerah, new String[]{“name”}, new int[]{R.id.name}); lv.setAdapter(adapter);
} } @Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// mengambil idp & name dari list yang kita click
String idp = posList.get(position).get(“idp”);
String name = posList.get(position).get(“name”); // mengirim parameter idp & name ke PropinsiActivity
Intent intent = new Intent(MainActivity.this, PropinsiActivity.class);
intent.putExtra(“idp”, posList.get(position).get(“idp”));
intent.putExtra(“name”, posList.get(position).get(“name”));
startActivity(intent);
} }
private ListView lv; // URL to get propinsi list JSON
final static String url = “https://kodepos-2d475.firebaseio.com/list_propinsi.json”; ArrayList<HashMap<String, String>> posList; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); // menampung data propinsi
posList = new ArrayList<>(); // listview untuk menampilkan daftar propinsi
lv = (ListView) findViewById(R.id.list_pos);
lv.setOnItemClickListener(this); // Get JSON data
new GetPost().execute();
} // Async task class to get json by making HTTP call
private class GetPost extends AsyncTask<Void, Void, Void> { @Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage(“Please wait…”);
pDialog.setCancelable(false);
pDialog.show(); } @Override
protected Void doInBackground(Void… arg0) {
HttpHandler sh = new HttpHandler(); // Making a request to url and getting response
String jsonStr = sh.makeServiceCall(url); Log.e(TAG, “Response from url: “ + jsonStr); if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr); // Parser flat object
Iterator<String> iter = jsonObj.keys();
while (iter.hasNext()) {
String key = iter.next();
try {
Object value = jsonObj.get(key);
String nama = value.toString(); // tmp hash map for single GetPost
HashMap<String, String> pos = new HashMap<>(); // adding each child node to HashMap key => value
pos.put(“name”, nama);
pos.put(“idp”, key); // adding pos to pos list
posList.add(pos); } catch (JSONException e) {
// Something went wrong!
}
}
} catch (final JSONException e) {
Log.e(TAG, “Json parsing error: “ + e.getMessage());
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
“Data tidak ada.”,
Toast.LENGTH_LONG)
.show();
}
}); }
} else {
Log.e(TAG, “Couldn’t get json from server.”);
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
“Terjadi kesalahan coba lain waktu.”,
Toast.LENGTH_LONG)
.show();
}
}); } return null;
} @Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss(); // Sorting a-z
Collections.sort(posList, new Comparator<HashMap< String,String >>() { @Override
public int compare(HashMap<String, String> lhs,
HashMap<String, String> rhs) {
return lhs.get(“name”).compareTo(rhs.get(“name”));
}
}); // Updating parsed JSON data into ListView
ListAdapter adapter = new SimpleAdapter(
MainActivity.this, posList,
R.layout.list_daerah, new String[]{“name”}, new int[]{R.id.name}); lv.setAdapter(adapter);
} } @Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// mengambil idp & name dari list yang kita click
String idp = posList.get(position).get(“idp”);
String name = posList.get(position).get(“name”); // mengirim parameter idp & name ke PropinsiActivity
Intent intent = new Intent(MainActivity.this, PropinsiActivity.class);
intent.putExtra(“idp”, posList.get(position).get(“idp”));
intent.putExtra(“name”, posList.get(position).get(“name”));
startActivity(intent);
} }
Dan yang terakhir adalah AndroidManifest.xml yg berisi informasi tentang aplikasi yang dibuat ;
AndroidManifest.xml
<?xml version=“1.0” encoding=“utf-8”?>
<manifest xmlns:android=“http://schemas.android.com/apk/res/android”
package=“com.kodepos”> <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”
android:configChanges=“orientation|keyboardHidden|screenSize”>
<intent-filter>
<action android:name=“android.intent.action.MAIN” /> <category android:name=“android.intent.category.LAUNCHER” />
</intent-filter>
</activity>
<activity
android:name=“.PropinsiActivity”
android:configChanges=“orientation|keyboardHidden|screenSize” />
<activity
android:name=“.KotaActivity”
android:configChanges=“orientation|keyboardHidden|screenSize” />
</application> </manifest>
<manifest xmlns:android=“http://schemas.android.com/apk/res/android”
package=“com.kodepos”> <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”
android:configChanges=“orientation|keyboardHidden|screenSize”>
<intent-filter>
<action android:name=“android.intent.action.MAIN” /> <category android:name=“android.intent.category.LAUNCHER” />
</intent-filter>
</activity>
<activity
android:name=“.PropinsiActivity”
android:configChanges=“orientation|keyboardHidden|screenSize” />
<activity
android:name=“.KotaActivity”
android:configChanges=“orientation|keyboardHidden|screenSize” />
</application> </manifest>
Berikut Adalah hasil ketika di Running.
![]() |
Hasil Running – Kode POS |