skip to Main Content

based on the image below, I have NIK & NAMA fields. the concept is when I fill in the NIK field then I press the GET button it will display the name(NAMA FIELD)

Picture : Menu add

Database structure example

NIK NAMA
96296 Farrasta
94878 Alfian

Java class

btnGet.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        NikKry = etNik.getText().toString();

        getNama(); // THE FUNCTION 

    }
});

private void getNama(){

    APIRequestData armNama = RetroMaster.konekRetrofit().create(APIRequestData.class);
    Call<ResponseMaster> tampilNama = armNama.ardGetNama(NikKry);

    tampilNama.enqueue(new Callback<ResponseMaster>() {
        @Override
        public void onResponse(Call<ResponseMaster> call, Response<ResponseMaster> response) {

            // HOW TO CODE PROPERLY ?

        }

        @Override
        public void onFailure(Call<ResponseMaster> call, Throwable t) {

        }
    });
}

PHP file

<?php
include ("koneksi.php");
$response = array();

if($_SERVER['REQUEST_METHOD'] == 'POST'){
    $NIK = $_POST["NIK"];
    $query = "SELECT NAMA FROM MASTER_KRY WHERE NIK = '$NIK'";
    $eksekusi = oci_parse($conn, $query);
    $cek = oci_execute($eksekusi);

    if($cek > 0){
        $response["data"] = array();

        while($ambil = oci_fetch_object($eksekusi)){
            $F["NAMA"] = $ambil->NAMA;
            array_push($response["data"], $F);
        }
    }else{
        $response["pesan"] = "Data tidak tersedia";
    }
}
else{
    $response["pesan"] = "*&*%4668%*%^$%#*&*(()%$!@#%";
}
echo json_encode($response);
oci_close($conn);
?>

2

Answers


  1. If you used EditText then this method will be help you to notify when text change in EditText.

    editTextObject.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                    
            }
    
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }
    
            @Override
            public void afterTextChanged(Editable s) {
                // here you get text via "s.toString();"
                NikKry = s.toString();
                getNama();
            }
        });
    

    If you used AutoCompleteTextView then this method will be help you like:

    autoCompleteTextView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            NikKry = yourArrayList.get(position).getText().toString();
            getNama();
        }
    
        @Override
        public void onNothingSelected(AdapterView<?> parent) {
             
        }
    });
    
    Login or Signup to reply.
  2. This depends on your entity of ResponseMaster. If the entity has field Nama and getter setter exists, you can use below:

    if(response.isSuccessful()){
        edNama.setText(response.body().getNama());
    }else{
        // show toast or log error
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search