Advertisement
Guest User

Untitled

a guest
Dec 13th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.10 KB | None | 0 0
  1. package com.example.mysql_136;
  2.  
  3. import android.content.DialogInterface;
  4. import android.content.Intent;
  5. import android.os.AsyncTask;
  6. import android.os.Bundle;
  7. import android.view.Menu;
  8. import android.view.MenuItem;
  9. import android.widget.EditText;
  10. import android.widget.Toast;
  11.  
  12. import androidx.appcompat.app.ActionBar;
  13. import androidx.appcompat.app.AlertDialog;
  14. import androidx.appcompat.app.AppCompatActivity;
  15.  
  16. public class detail_buku extends AppCompatActivity {
  17. EditText edittextid, edittextjudul, edittextpengarang;
  18. Buku buku;
  19. ServerRequest server;
  20.  
  21.  
  22.  
  23. @Override
  24. protected void onCreate(Bundle savedInstanceState) {
  25. super.onCreate(savedInstanceState);
  26. setContentView(R.layout.activity_detail_buku);
  27.  
  28. buku = new Buku();
  29. server = new ServerRequest();
  30. initView();
  31. ActionBar actionBar = getSupportActionBar();
  32. getSupportActionBar().setDisplayHomeAsUpEnabled(true);
  33. }
  34.  
  35. private void initView() {
  36. edittextid = (EditText) findViewById(R.id.editTextId);
  37. edittextjudul = (EditText) findViewById(R.id.editTextJudul);
  38. edittextpengarang = (EditText) findViewById(R.id.editTextpengarang);
  39.  
  40. String id = getIntent().getStringExtra("id");
  41. String judul = getIntent().getStringExtra("judul");
  42. String pengarang = getIntent().getStringExtra("pengarang");
  43.  
  44. edittextid.setText(id);
  45. edittextjudul.setText(judul);
  46. edittextpengarang.setText(pengarang);
  47.  
  48. buku.setId(Integer.valueOf(id));
  49. buku.setJudul(judul);
  50. buku.setPengarang(pengarang);
  51. }
  52.  
  53. @Override
  54. public boolean onCreateOptionsMenu (Menu menu) {
  55. getMenuInflater().inflate(R.menu.main_action,menu);
  56. return true;
  57. }
  58.  
  59. @Override
  60. public boolean onOptionsItemSelected(MenuItem item) {
  61. switch (item.getItemId()) {
  62. case android.R.id.home:
  63. goToMainActivity();
  64. break;
  65.  
  66. case R.id.action_menu_edit:
  67. Intent in = new Intent(getApplicationContext(), form_ubah.class);
  68. in.putExtra("id", buku.getId().toString());
  69. in.putExtra("judul", buku.getJudul());
  70. in.putExtra("pengarang", buku.getPengarang());
  71. startActivity(in);
  72. break;
  73.  
  74. case R.id.action_menu_delete:
  75. delete();
  76. break;
  77. }
  78. return super.onOptionsItemSelected(item);
  79. }
  80.  
  81. private void goToMainActivity() {
  82. Intent in = new Intent(getApplicationContext(), MainActivity.class);
  83. in.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  84. startActivity(in);
  85. }
  86.  
  87. private void delete() {
  88. AlertDialog.Builder builder = new AlertDialog.Builder(this);
  89. builder.setMessage("Delete"+buku.getJudul()+" ?");
  90. builder.setTitle("Delete");
  91. builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
  92. public void onClick(DialogInterface dialog, int which) {
  93. new DetailBukuAsync().execute();
  94. Toast.makeText(getApplicationContext(), "deleted", Toast.LENGTH_SHORT).show();
  95. }
  96. });
  97. builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
  98. public void onClick(DialogInterface dialog, int which) {
  99. dialog.cancel();
  100. }
  101. });
  102. AlertDialog alert = builder.create();
  103. alert.setIcon(android.R.drawable.ic_menu_delete);
  104. alert.show();
  105. }
  106.  
  107. private class DetailBukuAsync extends AsyncTask<String, String, String> {
  108.  
  109. @Override
  110. protected String doInBackground(String... params) {
  111. server.sendGetRequest(ServerRequest.urlDelete+"?id="+buku.getId().toString());
  112. return null;
  113. }
  114.  
  115. @Override
  116. protected void onPostExecute(String result) {
  117. Intent in = new Intent(getApplicationContext(), MainActivity.class);
  118. in.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  119. startActivity(in);
  120. }
  121. }
  122.  
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement