Home > other >  How do I write java code, to display the name automatically when after inputting id or number?
How do I write java code, to display the name automatically when after inputting id or number?

Time:12-27

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"] = "*&*F68%*%^$%#*&*(()%$!@#%";
}
echo json_encode($response);
oci_close($conn);
?>

CodePudding user response:

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) {
         
    }
});

CodePudding user response:

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
}
  • Related