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 propinsihttps://kodepos-2d475.firebaseio.com/list_propinsi.json?print=prettyDaftar kota/kabupatenhttps://kodepos-2d475.firebaseio.com/list_kotakab/{key propinsi}.json?print=prettyDaftar kecamatan, kelurahan & kodeposhttps://kodepos-2d475.firebaseio.com/kota_kab/{key kota}.json?print=pretty

Buka Android Studio, Create New Project buat file2. class2 seperti gambar dibawah :

Create New Project Kode POS

Aplikasi Kode POS merupakan yang digunakan untuk mencari kode POS di Indonesia. Aplikasi sederhana ini menggunakan database yang sebelumnya disimpan

   dimens.xml


    <dimen name=“margin_16dp”>16dp

     
activity_main.xml

<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.

<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" />

   activity_kota.xml //EditText untuk filter.

<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” />

   

</android.support.constraint.ConstraintLayout>

   list_pos.xml //untuk menampilkan data kecamatan, kelurahan dan kodepos

<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” />

   

    <TextView
        android:id="@+id/kelurahan"
        android:freezesText=“true”
        android:layout_width=“fill_parent”
        android:layout_height=“wrap_content” />

  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);
    }

}

          Dan yang terakhir adalah AndroidManifest.xml yg berisi informasi tentang aplikasi yang dibuat ;

     AndroidManifest.xml

<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”>
           
                <action android:name=“android.intent.action.MAIN” />

                <category android:name=“android.intent.category.LAUNCHER” />
           
       
        <activity
            android:name=".PropinsiActivity"
            android:configChanges=“orientation|keyboardHidden|screenSize” />
        <activity
            android:name=".KotaActivity"
            android:configChanges=“orientation|keyboardHidden|screenSize” />
   

Berikut Adalah hasil ketika di Running.