Advertisement
Guest User

Untitled

a guest
Jan 24th, 2020
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.71 KB | None | 0 0
  1. package com.joshua.r0th.crud2.ui.gallery;
  2.  
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.database.Cursor;
  6. import android.os.Bundle;
  7. import android.view.LayoutInflater;
  8. import android.view.View;
  9. import android.view.ViewGroup;
  10. import android.widget.Button;
  11. import android.widget.EditText;
  12. import android.widget.Toast;
  13.  
  14. import androidx.appcompat.app.AlertDialog;
  15. import androidx.fragment.app.Fragment;
  16. import androidx.fragment.app.FragmentActivity;
  17.  
  18. import com.joshua.r0th.crud2.LoginActivity;
  19. import com.joshua.r0th.crud2.R;
  20. import com.joshua.r0th.crud2.database1;
  21. import com.joshua.r0th.crud2.pantauan;
  22.  
  23.  
  24. public class GalleryFragment extends FragmentActivity {
  25.  
  26.     database1 myDb;
  27.  
  28.     EditText editNomorRumah,editJentikDalam,editJentikLuar;
  29.  
  30.     Button btnAddData;
  31.  
  32.     Button btnViewAll;
  33.  
  34.  
  35.  
  36.  
  37.  
  38.     @Override
  39.  
  40.     protected void onCreate(Bundle savedInstanceState) {
  41.  
  42.         super.onCreate(savedInstanceState);
  43.  
  44.         setContentView(R.layout.fragment_pantauan);
  45.  
  46.         myDb = new database1(this);
  47.  
  48.         editNomorRumah = (EditText)findViewById(R.id.nomorrmh);
  49.  
  50.         editJentikDalam = (EditText)findViewById(R.id.jentikdirumah);
  51.  
  52.         editJentikLuar = (EditText)findViewById(R.id.jentikdiluarrumah);
  53.  
  54.  
  55.         btnAddData = (Button)findViewById(R.id.tambahdata);
  56.  
  57.         btnViewAll = (Button)findViewById(R.id.lihatdata);
  58.  
  59.  
  60.  
  61.         AddData();
  62.  
  63.         viewAll();
  64.  
  65.  
  66.  
  67.     }
  68.  
  69.  
  70.  
  71.     //fungsi tambah
  72.  
  73.     public void AddData() {
  74.  
  75.         btnAddData.setOnClickListener(
  76.  
  77.                 new View.OnClickListener() {
  78.  
  79.                     @Override
  80.  
  81.                     public void onClick(View v) {
  82.  
  83.                         boolean isInserted = myDb.insertData(editNomorRumah.getText().toString(),
  84.  
  85.                                 editJentikDalam.getText().toString(),
  86.  
  87.                                 editJentikLuar.getText().toString() );
  88.  
  89.                         if(isInserted == true)
  90.  
  91.                             Toast.makeText(GalleryFragment.this,"Data Iserted",Toast.LENGTH_LONG).show();
  92.  
  93.                         else
  94.  
  95.                             Toast.makeText(GalleryFragment.this,"Data Not Iserted",Toast.LENGTH_LONG).show();
  96.  
  97.                     }
  98.  
  99.                 }
  100.  
  101.         );
  102.  
  103.     }
  104.  
  105.  
  106.  
  107.     //fungsi menampilkan data
  108.  
  109.     public void viewAll() {
  110.  
  111.         btnViewAll.setOnClickListener(
  112.  
  113.                 new View.OnClickListener(){
  114.  
  115.                     @Override
  116.  
  117.                     public void onClick(View v) {
  118.  
  119.                         Cursor res = myDb.getAllData();
  120.  
  121.                         if(res.getCount() == 0) {
  122.  
  123.                             // show message
  124.  
  125.                             showMessage("Error","Noting Found");
  126.  
  127.                             return;
  128.  
  129.                         }
  130.  
  131.  
  132.  
  133.                         StringBuffer buffer = new StringBuffer();
  134.  
  135.                         while (res.moveToNext() ) {
  136.  
  137.                             buffer.append("NomorRumah :"+ res.getString(0)+"\n");
  138.  
  139.                             buffer.append("JentikDalam :"+ res.getString(1)+"\n");
  140.  
  141.                             buffer.append("JentikLuar :"+ res.getString(2)+"\n");
  142.  
  143.  
  144.  
  145.                         }
  146.  
  147.  
  148.  
  149.                         // show all data
  150.  
  151.                         showMessage("Data",buffer.toString());
  152.  
  153.                     }
  154.  
  155.                 }
  156.  
  157.         );
  158.  
  159.     }
  160.  
  161.  
  162.  
  163.     //membuat alert dialog
  164.  
  165.     public void showMessage(String title, String Message){
  166.  
  167.         AlertDialog.Builder builder = new AlertDialog.Builder(this);
  168.  
  169.         builder.setCancelable(true);
  170.  
  171.         builder.setTitle(title);
  172.  
  173.         builder.setMessage(Message);
  174.  
  175.         builder.show();
  176.  
  177.     }
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement