kuyy

mail@pastecode.io avatar
unknown
java
3 years ago
5.8 kB
2
Indexable
Never
package com.example.lks_jabar_testing_java;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

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

import java.util.HashMap;
public class TampilDataDetail extends AppCompatActivity implements View.OnClickListener{

    private EditText editTextId;
    private EditText editTextMenuName;
    private EditText editTextDescription;
    private EditText editTextPrice;

    private Button buttonUpdate;


    private String id;

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


        Intent intent = getIntent();

//
        id = intent.getStringExtra(konfigurasi.DETAIL_ID);
        System.out.println("kesini id" + id);

        editTextMenuName = (EditText) findViewById(R.id.editTextMenuName);
        editTextDescription = (EditText) findViewById(R.id.editTextDescription);
        editTextPrice = (EditText) findViewById(R.id.editTextPrice);

        buttonUpdate = (Button) findViewById(R.id.buttonAddUpdate);
//
        buttonUpdate.setOnClickListener(this);

        if(id != null) {
            System.out.println("not nulll id" + id);
            getEmployee();
        } else {
            System.out.println("nulll id");
        }


    }

    private void getEmployee(){
        class GetEmployee extends AsyncTask<Void,Void,String>{
            ProgressDialog loading;
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                loading = ProgressDialog.show(TampilDataDetail.this,"Fetching...","Wait...",false,false);
            }

            @Override
            protected void onPostExecute(String s) {
                super.onPostExecute(s);
                loading.dismiss();
                showEmployee(s);
            }

            @Override
            protected String doInBackground(Void... params) {
                RequestHandler rh = new RequestHandler();
                String s = rh.sendGetRequestParam(konfigurasi.URL_MENU,id, "GET");
                return s;
            }
        }
        GetEmployee ge = new GetEmployee();
        ge.execute();
    }

    private void showEmployee(String json){
        try {

            JSONObject jsonObject = new JSONObject(json);
            String name = jsonObject.getString(konfigurasi.TAG_NAME);
            String desg = jsonObject.getString(konfigurasi.TAG_DESCRIPTION);
            String price = jsonObject.getString(konfigurasi.TAG_PRICE);

            editTextMenuName.setText(name);
            editTextDescription.setText(desg);
            editTextPrice.setText(price);

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }


    private void updateEmployee(){
        final String name = editTextMenuName.getText().toString().trim();
        final String desc = editTextDescription.getText().toString().trim();
        final String price = editTextPrice.getText().toString().trim();

        class UpdateEmployee extends AsyncTask<Void,Void,String>{
            ProgressDialog loading;
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                loading = ProgressDialog.show(TampilDataDetail.this,"Updating...","Wait...",false,false);
            }

            @Override
            protected void onPostExecute(String s) {
                super.onPostExecute(s);
                loading.dismiss();
                Toast.makeText(TampilDataDetail.this,s,Toast.LENGTH_LONG).show();
                System.out.println("kesini gak sih" + s);

                //check if successfully login
                JSONObject json = null; // Convert text to object
                String type = null;
                try {
                    json = new JSONObject(s);
                    type = json.get("ID").toString();

                    if (type != null) {
                        System.out.println(type);
                        Intent homepage = new Intent(TampilDataDetail.this, MenuActivity.class);
                        startActivity(homepage);
                    } else {
                        System.out.println("null cuy");
                    }


                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

            @Override
            protected String doInBackground(Void... params) {
                HashMap<String,String> hashMap = new HashMap<>();
                hashMap.put(konfigurasi.KEY_EMP_ID,id);
                hashMap.put(konfigurasi.KEY_EMP_NAME,name);
                hashMap.put(konfigurasi.KEY_EMP_DESCRIPTION,desc);
                hashMap.put(konfigurasi.KEY_EMP_PRICE,price);

                RequestHandler rh = new RequestHandler();

                String method = "";
                if(id == null) {
                    // update data
                    id = "forAdd";
                    method = "POST";
                } else {
                    method = "PUT";
                }
                String s = rh.sendGetRequestWithParam(konfigurasi.URL_MENU,hashMap, id, method);
                return s;
            }
        }

        UpdateEmployee ue = new UpdateEmployee();
        ue.execute();
    }


    @Override
    public void onClick(View v) {
         updateEmployee();

    }
}