1. Masuk ke file FormActivity.java , kemudian isi dengan source code di bawah

    [sourcecode lang=”java”]

    public class FormActivity extends AppCompatActivity {

    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.et_contact_number)
    EditText etContactNumber;
    @BindView(R.id.btn_simpan)
    Button btnSimpan;

    int personId;

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

    ButterKnife.bind(this);
    personId=getIntent().getIntExtra("id",0);

    if (personId > 0) {
    getPerson(generateToken(), personId);

    }

    }

    @OnClick(R.id.btn_simpan)
    public void onClick(View view) {
    int getId = view.getId();
    switch (getId) {
    case R.id.btn_simpan:
    if (personId > 0) {
    updatePerson(personId, etName.getText().toString(), etContactNumber.getText().toString());
    } else {
    setPerson(etName.getText().toString(), etContactNumber.getText().toString());

    }
    break;

    }
    }

    public void setPerson(String name, String contactNumber) {

    ApiEndPoint apiEndPoint = ApiClient.getClient().create(ApiEndPoint.class);
    Call<StatusResponse> call = apiEndPoint.createRequest(name, contactNumber);

    call.enqueue(new Callback<StatusResponse>() {
    @Override
    public void onResponse(Call<StatusResponse> call, Response<StatusResponse> response) {

    final StatusResponse statusResponse = response.body();

    if (statusResponse != null) {
    Log.d("Response Data ", "Total Data" + statusResponse.getStatus());
    if (statusResponse.getStatus()) {
    finish();
    Toast.makeText(getApplicationContext(), "Data Berhasil Di Tambah", Toast.LENGTH_SHORT).show();
    } else {
    Toast.makeText(getApplicationContext(), "Data Kosong", Toast.LENGTH_SHORT).show();
    }

    } else {
    Log.d("Login : ", "Data Null");
    }
    }

    @Override
    public void onFailure(Call<StatusResponse> call, Throwable t) {
    Toast.makeText(getApplicationContext(), "Koneksi Bermasalah", Toast.LENGTH_SHORT).show();

    }
    });
    }

    private String generateToken() {

    SecureRandom random = new SecureRandom();
    byte bytes[] = new byte[20];
    random.nextBytes(bytes);
    return bytes.toString();
    }

    public void getPerson(final String token, int id) {

    ApiEndPoint apiEndPoint = ApiClient.getClient().create(ApiEndPoint.class);
    Call<ReadResponse> call = apiEndPoint.readPersonRequest(token, id);

    call.enqueue(new Callback<ReadResponse>() {
    @Override
    public void onResponse(Call<ReadResponse> call, Response<ReadResponse> response) {

    final ReadResponse readResponse = response.body();

    if (readResponse != null) {
    Log.d("Response Data ", "Total Data" + readResponse.getStatus());
    if (readResponse.getStatus()) {

    runOnUiThread(new Runnable() {
    @Override
    public void run() {

    List<Person> person = readResponse.getPersons();

    etName.setText(person.get(0).getPersonName());
    etContactNumber.setText(person.get(0).getContactNumber());
    }
    });

    } else {
    Toast.makeText(getApplicationContext(), "Data Kosong", Toast.LENGTH_SHORT).show();
    }

    } else {
    Log.d("Login : ", "Data Null");
    }
    }

    @Override
    public void onFailure(Call<ReadResponse> call, Throwable t) {
    Toast.makeText(getApplicationContext(), "Koneksi Bermasalah", Toast.LENGTH_SHORT).show();

    }
    });
    }

    public void updatePerson(int id, String name, String contactNumber) {

    ApiEndPoint apiEndPoint = ApiClient.getClient().create(ApiEndPoint.class);
    Call<StatusResponse> call = apiEndPoint.updateRequest(id, name, contactNumber);

    call.enqueue(new Callback<StatusResponse>() {
    @Override
    public void onResponse(Call<StatusResponse> call, Response<StatusResponse> response) {

    final StatusResponse statusResponse = response.body();

    if (statusResponse != null) {
    Log.d("Response Data ", "Total Data" + statusResponse.getStatus());
    if (statusResponse.getStatus()) {
    finish();
    Toast.makeText(getApplicationContext(), "Data Berhasil Di Ubah", Toast.LENGTH_SHORT).show();
    } else {
    Toast.makeText(getApplicationContext(), "Data Kosong", Toast.LENGTH_SHORT).show();
    }

    } else {
    Log.d("Login : ", "Data Null");
    }
    }

    @Override
    public void onFailure(Call<StatusResponse> call, Throwable t) {
    Toast.makeText(getApplicationContext(), "Koneksi Bermasalah", Toast.LENGTH_SHORT).show();

    }
    });
    }

    }
    [/sourcecode]

  2. Sekarang kita buat menu untuk menampilkan Form Input Data. Buat sebuah folder dengan nama menu di dalam res.
  3. Klik kanan pada folder menu , lalu buat file tambah_data.xml .
  4. Lalu masuk ke app > res > menu > tambah_data.xml . Masukkan source code di bawah

    [sourcecode lang=”xml”]

    <?xml version="1.0" encoding="utf-8"?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <item
    android:id="@+id/menu_create"
    android:icon="@mipmap/ic_launcher"
    android:title="Create"
    app:showAsAction="always" />

    </menu>
    [/sourcecode]

  5. Lalu masuk ke file MainActivity.java . Tambahkan 2 fungsi di bawah.
  6. Sekarang kita coba running ulang aplikasi, lalu sentuh Kotak Hijau. Nanti akan tampil form input data. Coba input satu data, lalu sentuh Tombol Simpan.                                                                                                               
  7. Buat menu resource seperti cara no 3, namakan dengan context_menu. Kemudian tambahkan 2 item menu seperti gambar di bawah.
  8. Sekarang kita aktifkan context menu untuk memunculkan menu Edit dan Delete. Masuk ke file MainActivity.java . Tambahkan 2 fungsi di bawah.
  9. Daftarkan context menu ke ListView dengan memanggil fungsi registerForContextMenu().
  10. Sekarang coba running kembali , lalu sentuh dan tahan pada data. Nanti akan muncul menu 
  11. Tambahkan fungsi hapus pada MainActivity Class, masukkan source code berikut.

    [sourcecode lang=”java”]

    public void deletePerson(final int id) {

    ApiEndPoint apiEndPoint = ApiClient.getClient().create(ApiEndPoint.class);
    Call<StatusResponse> call = apiEndPoint.deleteRequest(id);

    call.enqueue(new Callback<StatusResponse>() {
    @Override
    public void onResponse(Call<StatusResponse> call, Response<StatusResponse> response) {

    final StatusResponse StatusResponse = response.body();

    if (StatusResponse != null) {
    Log.d("Response Data ", "Total Data" + StatusResponse.getStatus());
    if (StatusResponse.getStatus()) {

    runOnUiThread(new Runnable() {
    @Override
    public void run() {
    Toast.makeText(getApplicationContext(), "Data Berhasil Dihapus", Toast.LENGTH_SHORT).show();
    getAllPerson(generateToken());
    }
    });

    } else {
    Toast.makeText(getApplicationContext(), "Data Kosong", Toast.LENGTH_SHORT).show();
    }

    } else {
    Log.d("Login : ", "Data Null");
    }
    }

    @Override
    public void onFailure(Call<StatusResponse> call, Throwable t) {
    Toast.makeText(getApplicationContext(), "Koneksi Bermasalah", Toast.LENGTH_SHORT).show();

    }
    });
    }
    [/sourcecode]

  12. Sekarang kita tambahkan code untuk mengambil data Person Id. Lalu panggil fungsi deletePerson .
  13. Selanjutnya, kita tambahkan code untuk pindah activity, serta mengirim nilai Person Id.
  14. Sekarang coba anda hapus dan ubah data melalui menu. Kalau berhasil, it’s done yea ……

Categories: Uncategorized

0 Comments

Leave a Reply

Avatar placeholder