Click here to Skip to main content
15,880,608 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want a loop when posting..
example :
today is 21 October 2020, then i add 2 days later..
so the output in database is 21 October 2020, 22 October 2020, and 23 October 2020..

example of output
date | data
-----------------------
21 October 2020|A---->data
22 October 2020|A---->data
23 October 2020|A---->data


so here is the code :

Java
<pre>package com.example.eis2;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import android.Manifest;
import android.app.DatePickerDialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.util.Base64;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.example.eis2.Item.getNo_pengajuan_full;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import static com.example.eis2.Item.LoginItem.KEY_NIK;
import static com.example.eis2.cuti.txt_nomor_jabatan;

public class cutikhusus extends AppCompatActivity {
    Button list_data, upload, pengajuan;
    EditText jeniscuti, hari, nopengajuan, edittext, keterangan;
    Spinner spinner;
    ImageView gambar1;
    private List<getNo_pengajuan_full> no_pengajuan;
    ProgressDialog pDialog;
    final int CODE_GALLERY_REQUEST = 999;
    Bitmap bitmap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cutikhusus);
        final String nik_baru = getIntent().getStringExtra(KEY_NIK);
        jeniscuti = (EditText) findViewById(R.id.jeniscuti);
        gambar1 = (ImageView) findViewById(R.id.gambar1);
        upload = (Button) findViewById(R.id.upload);
        pengajuan = (Button) findViewById(R.id.pengajuan);

        keterangan = (EditText) findViewById(R.id.keterangan);
        edittext = (EditText) findViewById(R.id.tanggalizincutikhusus);

        hari = (EditText) findViewById(R.id.hari);
        nopengajuan = (EditText) findViewById(R.id.nopengajuan);
        no_pengajuan = new ArrayList<>();

        upload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ActivityCompat.requestPermissions(cutikhusus.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, CODE_GALLERY_REQUEST);
            }
        });

        pengajuan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                khusus();
            }
        });

        list_data = (Button) findViewById(R.id.list_data_cuti_khusus);
        hari.setFocusable(false);
        jeniscuti.setFocusable(false);
        getNo();

        list_data.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(cutikhusus.this, tabel_cuti_khusus.class);
                i.putExtra(KEY_NIK, nik_baru);
                startActivity(i);
            }
        });
        spinner = findViewById(R.id.jenis);
        String[] jenisizin = {
                "Istri / Suami / Orang tua / Mertua / Menantu / Anak meninggal dunia",
                "Saudara kandung meninggal dunia",
                "Anggota keluarga dalam satu rumah meninggal dunia",
                "Istri melahirkan / keguguran",
                "Karyawati keguguran",
                "Karyawan menikah (hanya satu kali dalam bekerja)",
                "Karyawati melahirkan",
                "Anak khitan / baptis",
                "Menikahkan anak",
        };
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, jenisizin);
        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            public void onItemSelected(AdapterView<?> arg0, View arg1,
                                       int arg2, long arg3) {
                if (spinner.getSelectedItem().toString().equals("Istri / Suami / Orang tua / Mertua / Menantu / Anak meninggal dunia")) {
                    jeniscuti.setText("Tidak Direncanakan");
                    hari.setText("2");
                } else if (spinner.getSelectedItem().toString().equals("Saudara kandung meninggal dunia")) {
                    jeniscuti.setText("Tidak Direncanakan");
                    hari.setText("1");
                } else if (spinner.getSelectedItem().toString().equals("Anggota keluarga dalam satu rumah meninggal dunia")) {
                    jeniscuti.setText("Tidak Direncanakan");
                    hari.setText("1");
                } else if (spinner.getSelectedItem().toString().equals("Istri melahirkan / keguguran")) {
                    jeniscuti.setText("Tidak Direncanakan");
                    hari.setText("2");
                } else if (spinner.getSelectedItem().toString().equals("Karyawati keguguran")) {
                    hari.setText("45");
                    jeniscuti.setText("Tidak Direncanakan");
                } else if (spinner.getSelectedItem().toString().equals("Karyawan menikah (hanya satu kali dalam bekerja)")) {
                    hari.setText("3");
                    jeniscuti.setText("Direncanakan");
                } else if (spinner.getSelectedItem().toString().equals("Karyawati melahirkan")) {
                    hari.setText("90");
                    jeniscuti.setText("Direncanakan");
                } else if (spinner.getSelectedItem().toString().equals("Anak khitan / baptis")) {
                    hari.setText("2");
                    jeniscuti.setText("Direncanakan");
                } else if (spinner.getSelectedItem().toString().equals("Menikahkan anak")) {
                    hari.setText("2");
                    jeniscuti.setText("Direncanakan");
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });

            final Calendar myCalendar = Calendar.getInstance();
        final DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                myCalendar.set(Calendar.YEAR, year);
                myCalendar.set(Calendar.MONTH, monthOfYear);
                myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                updateLabel();
            }
            private void updateLabel(){
                String myFormat = "yyyy/MM/dd"; //In which you need put here
                SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
                edittext.setText(sdf.format(myCalendar.getTime()));
            }
        };
        edittext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                new DatePickerDialog(cutikhusus.this, date, myCalendar
                        .get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
                        myCalendar.get(Calendar.DAY_OF_MONTH)).show();
            }
        });
    }
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {

        if (requestCode == CODE_GALLERY_REQUEST){
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
                Intent intent = new Intent(Intent.ACTION_PICK);
                intent.setType("image/*");
                startActivityForResult(Intent.createChooser(intent, "Select Image"), CODE_GALLERY_REQUEST);
            }else{
                Toast.makeText(getApplicationContext(), "You don't have permission to access gallery!", Toast.LENGTH_LONG).show();
            }
            return;
        }
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == CODE_GALLERY_REQUEST && resultCode == RESULT_OK && data != null) {
            Uri path = data.getData();
            try {
                InputStream inputStream = getContentResolver().openInputStream(path);
                bitmap = BitmapFactory.decodeStream(inputStream);
                gambar1.setImageBitmap(bitmap);
                gambar1.setVisibility(View.VISIBLE);
            } catch (IOException e) {
                e.printStackTrace();
            }
            Toast.makeText(cutikhusus.this, "Gambar sudah diupload", Toast.LENGTH_SHORT).show();
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    private void getNo() {
        pDialog = new ProgressDialog(cutikhusus.this);
        showDialog();
        pDialog.setContentView(R.layout.progress_dialog);
        pDialog.getWindow().setBackgroundDrawableResource(
                android.R.color.transparent
        );

        StringRequest stringRequest1 = new StringRequest(Request.Method.GET, "http://10.12.0.220/db_eis/cuti_khusus.php",
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        try {
                            JSONObject obj = new JSONObject(response);
                            JSONArray movieArray = obj.getJSONArray("data");

                            ///get last data only///
                            for (int i = 0; i < movieArray.length(); i++) {
                                JSONObject movieObject = movieArray.getJSONObject(i);
                                getNo_pengajuan_full nomor = new getNo_pengajuan_full(
                                        movieObject.getInt("no_pengajuan_khusus"));
                                no_pengajuan.add(nomor);
                            }
                            getNo_pengajuan_full item = no_pengajuan.get(no_pengajuan.size() - 1);

                            nopengajuan.setText(String.valueOf(item.getNo_pengajuan_full() + 1));
                            hideDialog();

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Toast.makeText(getApplicationContext(), "Maaf, ada kesalahan", Toast.LENGTH_SHORT).show();
                    }
                });
        RequestQueue requestQueue1 = Volley.newRequestQueue(this);
        requestQueue1.add(stringRequest1);
    }
    private void khusus() {
        pDialog = new ProgressDialog(this);
        showDialog();
        pDialog.setContentView(R.layout.progress_dialog);
        pDialog.getWindow().setBackgroundDrawableResource(
                android.R.color.transparent
        );
        StringRequest stringRequest2 = new StringRequest(Request.Method.POST, "http://10.12.0.220/db_eis/cuti_khusus_insert.php",
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        hideDialog();
                        Toast.makeText(getApplicationContext(), "data sudah dimasukkan", Toast.LENGTH_LONG).show();
                        cutikhusus.this.finish();
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        hideDialog();
                        Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();

                    }
                }) {

            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> params = new HashMap<String, String>();
                String pengajuan = nopengajuan.getText().toString();
                String nik_baru = getIntent().getStringExtra(KEY_NIK);
                String jabatan = txt_nomor_jabatan.getText().toString();
                String jenis = jeniscuti.getText().toString();
                String kondisi = spinner.getSelectedItem().toString();
                String tanggal1 = edittext.getText().toString();
                String keterangan_khusus = keterangan.getText().toString();
                String gambar = imagetoString(bitmap);

                    params.put("no_pengajuan_khusus", pengajuan);
                    params.put("nik_cuti_khusus", nik_baru);
                    params.put("jabatan_cuti_khusus", jabatan);
                    params.put("jenis_cuti_khusus", jenis);

                    params.put("kondisi", kondisi);

                    params.put("start_cuti_khusus", tanggal1);
                    params.put("ket_tambahan_khusus", keterangan_khusus);
                    params.put("status_cuti_khusus", "0");
                    params.put("status_cuti_khusus_2", "0");

                    params.put("dokumen_cuti_khusus", gambar);


                    return params;
                }
        };
        RequestQueue requestQueue2 = Volley.newRequestQueue(this);
        requestQueue2.add(stringRequest2);
    }

    private void hideDialog() {
        if (pDialog.isShowing())
            pDialog.dismiss();
    }

    private void showDialog() {
        if (!pDialog.isShowing())
            pDialog.show();
    }
    private String imagetoString(Bitmap bitmap)
    {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 20, byteArrayOutputStream);
        byte[] imageType = byteArrayOutputStream.toByteArray();
        return Base64.encodeToString(imageType, Base64.DEFAULT);
    }
}


What I have tried:

- Hari (EditText) = showing 2 days later (example)
- edittext (EditText) = Choosing a date
Posted
Updated 20-Oct-20 21:28pm

1 solution

Start with a Calendar  |  Android Developers[^] object. You can then use the add method to increment the day. An added benefit is that it will automatically handle any dates going beyond the end of the month or year.
 
Share this answer
 
Comments
Komang Putra 21-Oct-20 4:04am    
Thanks a lot sir.
but... i still don't know how to use loop when post...
Richard MacCutchan 21-Oct-20 4:07am    
Sorry, but I do not understand what you mean by "use loop when post".
Komang Putra 21-Oct-20 4:23am    
uh.. just like this...
i post 'a' on my data on today (21 October 2020), but i choose 3 days after today..
so the output is :

data date
a 21 October 2020
a 22 October 2020
a 23 October 2020

just like that...
in data keep same but in date is different..
Richard MacCutchan 21-Oct-20 4:53am    
So create a Calendar object with today's date. Start a loop for the number of days you wish to post, e.g. from 0 to 3, and do something like:
Calendar myCal = Calendar.getInstance(); // new calendar with today's date
for (int day = 0; day < 3; ++day) {
    myCal.add(Calendar.DAY_OF_MONTH, day);
    // post the details
}
Komang Putra 21-Oct-20 8:55am    
Thank you sir, I appreciate that

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900