Aplikasi Sistem Sekolah Android (2)

Kita Lanjutkan membuat bagian clientnya :
Kita buat sebuah activity dengan class javanya, bisa dengan perintah file, new, other, android activity. Kemudia beri nama Member.java dan activity_member.xml, kemudian isi file :
  1. Member.java
package jhohannes.purba;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.view.ViewPager.LayoutParams;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TableLayout;
import android.widget.Tabl eRow;
import android.widget.TextView;

public class Member extends Activity implements OnClickListener {

User user = new User();
TableLayout tabelUser;

JSONArray arrayUser;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_member);

tabelUser = (TableLayout) findViewById(R.id.tableUser);

TableRow barisTabel = new TableRow(this);
barisTabel.setBackgroundColor(Color.CYAN);

TextView viewHeaderNama = new TextView(this);
TextView viewHeaderAlamat = new TextView(this);
TextView viewHeaderTelpon = new TextView(this);

viewHeaderNama.setText("Nama");
viewHeaderAlamat.setText("Alamat");
viewHeaderTelpon.setText("Telpon");

viewHeaderNama.setPadding(5, 1, 5, 1);
viewHeaderAlamat.setPadding(5, 1, 5, 1);
viewHeaderTelpon.setPadding(5, 1, 5, 1);

barisTabel.addView(viewHeaderNama);
barisTabel.addView(viewHeaderAlamat);
barisTabel.addView(viewHeaderTelpon);

tabelUser.addView(barisTabel, new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));

try {

arrayUser = new JSONArray(user.tampilUser());

for (int i = 0; i < arrayUser.length(); i++) {
JSONObject jsonChildNode = arrayUser.getJSONObject(i);
String nama = jsonChildNode.optString("nama");
String alamat = jsonChildNode.optString("alamat");
String telpon = jsonChildNode.optString("telpon");

System.out.println("Nama :" + nama);
System.out.println("Alamat :" + alamat);
System.out.println("Telpon :" + telpon);

barisTabel = new TableRow(this);

if (i % 2 == 0) {
barisTabel.setBackgroundColor(Color.LTGRAY);
}

TextView viewNama = new TextView(this);
viewNama.setText(nama);
viewNama.setPadding(5, 1, 5, 1);
barisTabel.addView(viewNama);

TextView viewAlamat = new TextView(this);
viewAlamat.setText(alamat);
viewAlamat.setPadding(5, 1, 5, 1);
barisTabel.addView(viewAlamat);
TextView viewTelpon = new TextView(this);
viewTelpon.setText(telpon);
viewTelpon.setPadding(5, 1, 5, 1);
barisTabel.addView(viewTelpon);

tabelUser.addView(barisTabel, new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
}
} catch (JSONException e) {
e.printStackTrace();
}
}


@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
}

}

  1. File activity_member.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:id="@+id/LinearLayout1"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:background="@drawable/back2"
   android:layout_gravity="center"
   android:orientation="vertical"
   tools:context="Member" >

   <TextView
       android:id="@+id/textView1"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:text="Data Member/Pelanggan"
       android:textColor="#ffff"
       android:gravity="center_horizontal"
       android:textAppearance="?android:attr/textAppearanceMedium" />
   
   <HorizontalScrollView
       android:id="@+id/horizontalScrollView"
       android:layout_width="match_parent"
       android:layout_height="wrap_content" >

       <ScrollView
           android:id="@+id/verticalScrollView"
           android:layout_width="match_parent"
           android:layout_height="wrap_content" >

           <TableLayout
               android:id="@+id/tableUser"
               android:layout_width="match_parent"
               android:layout_height="wrap_content"
               android:layout_marginTop="100dp" >
           </TableLayout>
           
       </ScrollView>
   </HorizontalScrollView>

</LinearLayout>
Selanjutnya buat sebuh class dengan nama User.java
package jhohannes.purba;
public class User extends Koneksi {
String URL = "http://10.0.2.2/seapotik/smember.php";
String url = "";
String response = "";

public String tampilUser() {
try {
url = URL + "?operasi=view";
System.out.println("URL Tampil User: " + url);
response = call(url);
} catch (Exception e) {
}
return response;
}

public String getUserById(int id) {
try {
url = URL + "?operasi=get_user_by_id&id=" + id;
System.out.println("URL Insert User : " + url);
response = call(url);
} catch (Exception e) {
}
return response;
}
}

Kemudian Buat lagi Android Activity dengan nama Seapotik.java dan activity_seapotik.xml. dan isi file :
1. sekolah.java
package jhohannes.purba;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.view.ViewPager.LayoutParams;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class Seapotik extends Activity implements OnClickListener {

Apotik apotik = new Apotik();
TableLayout tabelApotik;

JSONArray arrayApotik;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_seapotik);

tabelApotik = (TableLayout) findViewById(R.id.tableApotik);

TableRow barisTabel = new TableRow(this);
barisTabel.setBackgroundColor(Color.CYAN);

TextView viewHeaderNama = new TextView(this);
TextView viewHeaderAlamat = new TextView(this);
TextView viewHeaderPemilik = new TextView(this);
TextView viewHeaderTelpon = new TextView(this);

viewHeaderNama.setText("Nama");
viewHeaderAlamat.setText("Alamat");
viewHeaderPemilik.setText("Pemilik");
viewHeaderTelpon.setText("Telpon");

viewHeaderNama.setPadding(5, 1, 5, 1);
viewHeaderAlamat.setPadding(5, 1, 5, 1);
viewHeaderPemilik.setPadding(5, 1, 5, 1);
viewHeaderTelpon.setPadding(5, 1, 5, 1);

barisTabel.addView(viewHeaderNama);
barisTabel.addView(viewHeaderAlamat);
barisTabel.addView(viewHeaderPemilik);
barisTabel.addView(viewHeaderTelpon);

tabelApotik.addView(barisTabel, new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));

try {

arrayApotik = new JSONArray(apotik.tampilApotik());

for (int i = 0; i < arrayApotik.length(); i++) {
JSONObject jsonChildNode = arrayApotik.getJSONObject(i);
String nama = jsonChildNode.optString("nama");
String alamat = jsonChildNode.optString("alamat");
String pemilik = jsonChildNode.optString("pemilik");
String telpon = jsonChildNode.optString("telpon");

System.out.println("Nama :" + nama);
System.out.println("Alamat :" + alamat);
System.out.println("Pemilik :" + pemilik);
System.out.println("Telpon :" + telpon);

barisTabel = new TableRow(this);

if (i % 2 == 0) {
barisTabel.setBackgroundColor(Color.LTGRAY);
}

TextView viewNama = new TextView(this);
viewNama.setText(nama);
viewNama.setPadding(5, 1, 5, 1);
barisTabel.addView(viewNama);

TextView viewAlamat = new TextView(this);
viewAlamat.setText(alamat);
viewAlamat.setPadding(5, 1, 5, 1);
barisTabel.addView(viewAlamat);

TextView viewPemilik = new TextView(this);
viewPemilik.setText(pemilik);
viewPemilik.setPadding(5, 1, 5, 1);
barisTabel.addView(viewPemilik);
TextView viewTelpon = new TextView(this);
viewTelpon.setText(telpon);
viewTelpon.setPadding(5, 1, 5, 1);
barisTabel.addView(viewTelpon);

tabelApotik.addView(barisTabel, new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
}
} catch (JSONException e) {
e.printStackTrace();
}
}


@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
}

}
2. activity_sekolah.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:id="@+id/LinearLayout1"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:layout_gravity="center"
   android:background="@drawable/back2"
   android:orientation="vertical"
   tools:context="Seapotik" >

   <TextView
       android:id="@+id/textView1"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:text="Data Apotik yang Terdaftar"
       android:gravity="center_horizontal"
       android:textColor="#ffff"
       android:textAppearance="?android:attr/textAppearanceMedium" />
   
   <HorizontalScrollView
       android:id="@+id/horizontalScrollView"
       android:layout_width="match_parent"
       android:layout_height="wrap_content" >

       <ScrollView
           android:id="@+id/verticalScrollView"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content" >

           <TableLayout
               android:id="@+id/tableApotik"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:layout_marginTop="80dp" >
           </TableLayout>
           
       </ScrollView>
   </HorizontalScrollView>
</LinearLayout>
Selanjutnya buat sebuah class biasa dengan nama Apotik.java :

package jhohannes.purba;

public class Apotik extends Koneksi {
String URL = "http://10.0.2.2/seapotik/sapotik.php";
String url = "";
String response = "";

public String tampilApotik() {
try {
url = URL + "?operasi=view";
System.out.println("URL Tampil Apotik: " + url);
response = call(url);
} catch (Exception e) {
}
return response;
}

public String getApotikById(int id) {
try {
url = URL + "?operasi=get_apotik_by_id&id=" + id;
System.out.println("URL Insert Apotik : " + url);
response = call(url);
} catch (Exception e) {
}
return response;
}
}

Kemudian Buat android activity dengan nama MainObat.java dan activity_main_obat.xml, kemudian isi :
  1. MainSiswa.java
package jhohannes.purba;

import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.view.ViewPager.LayoutParams;
import android.text.Html;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

public class MainObat extends Activity implements OnClickListener {

Obat obat = new Obat();
TableLayout tabelObat;

JSONArray arrayObat;
Button beli;
SessionManager session;
TextView status;
JSONArray contacts = null;
String email, nama, id;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_obat);
session = new SessionManager(getApplicationContext());
Toast.makeText(getApplicationContext(),
"User Login Status: " + session.isLoggedIn(), Toast.LENGTH_LONG)
.show();

session.checkLogin();

HashMap<String, String> user = session.getUserDetails();

nama = user.get(SessionManager.KEY_NAME);
// id = user.get(SessionManager.KEY_ID);

TextView status = (TextView) findViewById(R.id.status);
status.setText(Html.fromHtml("welcome<b> " + nama + "</b> "));
beli = (Button)findViewById(R.id.beli);
beli.setOnClickListener(new View.OnClickListener() {
       
        @Override
           public void onClick(View v) {
               Intent intent = new Intent(MainObat.this, MainTransaksi.class);
               startActivity(intent);
           }
       });

tabelObat = (TableLayout) findViewById(R.id.tableObat);

TableRow barisTabel = new TableRow(this);
barisTabel.setBackgroundColor(Color.CYAN);

TextView viewHeaderJenis = new TextView(this);
TextView viewHeaderNama = new TextView(this);
TextView viewHeaderStok = new TextView(this);
TextView viewHeaderApotik = new TextView(this);
TextView viewHeaderHarga = new TextView(this);
viewHeaderJenis.setText("Jenis");
viewHeaderNama.setText("Nama");
viewHeaderStok.setText("Stok");
viewHeaderApotik.setText("Apotik");
viewHeaderHarga.setText("Harga");

viewHeaderJenis.setPadding(5, 1, 5, 1);
viewHeaderNama.setPadding(5, 1, 5, 1);
viewHeaderStok.setPadding(5, 1, 5, 1);
viewHeaderApotik.setPadding(5, 1, 5, 1);
viewHeaderHarga.setPadding(5, 1, 5, 1);

barisTabel.addView(viewHeaderJenis);
barisTabel.addView(viewHeaderNama);
barisTabel.addView(viewHeaderStok);
barisTabel.addView(viewHeaderApotik);
barisTabel.addView(viewHeaderHarga);

tabelObat.addView(barisTabel, new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));

try {

arrayObat = new JSONArray(obat.tampilObat());

for (int i = 0; i < arrayObat.length(); i++) {
JSONObject jsonChildNode = arrayObat.getJSONObject(i);
String jenis = jsonChildNode.optString("jenis");
String nama = jsonChildNode.optString("nama");
String stok = jsonChildNode.optString("stok");
String apotik = jsonChildNode.optString("apotik");
String harga = jsonChildNode.optString("harga");

System.out.println("Jenis :" + jenis);
System.out.println("Nama :" + nama);
System.out.println("Stok :" + stok);
System.out.println("Apotik :" + apotik);
System.out.println("Harga :" + harga);

barisTabel = new TableRow(this);

if (i % 2 == 0) {
barisTabel.setBackgroundColor(Color.LTGRAY);
}

TextView viewJenis = new TextView(this);
viewJenis.setText(jenis);
viewJenis.setPadding(5, 1, 5, 1);
barisTabel.addView(viewJenis);
TextView viewNama = new TextView(this);
viewNama.setText(nama);
viewNama.setPadding(5, 1, 5, 1);
barisTabel.addView(viewNama);

TextView viewStok = new TextView(this);
viewStok.setText(stok);
viewStok.setPadding(5, 1, 5, 1);
barisTabel.addView(viewStok);
TextView viewApotik = new TextView(this);
viewApotik.setText(apotik);
viewApotik.setPadding(5, 1, 5, 1);
barisTabel.addView(viewApotik);
TextView viewHarga = new TextView(this);
viewHarga.setText(harga);
viewHarga.setPadding(5, 1, 5, 1);
barisTabel.addView(viewHarga);

tabelObat.addView(barisTabel, new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
}
} catch (JSONException e) {
e.printStackTrace();
}
}


@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
}

}

  1. activity_main_siswa.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:id="@+id/LinearLayout1"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:layout_gravity="center"
   android:background="@drawable/back2"
   android:orientation="vertical"
   tools:context=".Member" >

   <TextView
       android:id="@+id/status"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_marginTop="10dp"
       android:gravity="center_horizontal"
       android:textColor="#ffff"
       android:paddingLeft="10dp"
       android:text="Hallo"
       android:textAppearance="?android:attr/textAppearanceSmall" />

   <TextView
       android:id="@+id/textView1"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:textColor="#ffff"
       android:gravity="center_horizontal"
       android:text="Daftar Obat Semua Apotik"
       android:textAppearance="?android:attr/textAppearanceMedium" />
   
   <HorizontalScrollView
       android:id="@+id/horizontalScrollView"
       android:layout_width="match_parent"
       android:layout_height="wrap_content" >

       <ScrollView
           android:id="@+id/verticalScrollView"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content" >

           <TableLayout
               android:id="@+id/tableObat"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:layout_marginTop="80dp" >
           </TableLayout>
           
       </ScrollView>
   </HorizontalScrollView>

   <Button
       android:id="@+id/beli"
       style="?android:attr/buttonStyleSmall"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:text="Beli" />

</LinearLayout>

Kemudian Buat class baru dengan nama Obat.java dan isi :

package jhohannes.purba;

public class Obat extends Koneksi {
String URL = "http://10.0.2.2/seapotik/sobat.php";
String url = "";
String response = "";

public String tampilObat() {
try {
url = URL + "?operasi=view";
System.out.println("URL Tampil Obat: " + url);
response = call(url);
} catch (Exception e) {
}
return response;
}

public String getObatById(int id) {
try {
url = URL + "?operasi=get_obat_by_id&id=" + id;
System.out.println("URL Insert Obat : " + url);
response = call(url);
} catch (Exception e) {
}
return response;
}
}

Komentar

Postingan populer dari blog ini

Aplikasi kamus bahasa isyarat indonesia 1

Aplikasi kamus bahasa isyarat indonesia 2

Aplikasi kamus bahasa isyarat indonesia 3