Masuk ke file FormActivity.java , kemudian isi dengan source code di bawah
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();
}
});
}
}
Tambahkan fungsi hapus pada MainActivity Class, masukkan source code berikut.
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();
}
});
}