2 Spinners: Populate second spinner from first spinner id selected using Retrofit

General Tech Bugs & Fixes 2 years ago

1 2 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 29 Aug 2022, this text provides information on Bugs & Fixes related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago

 

Hi I working with my android, I have two spinners and using retrofit to populate my spinners. I manage to populate first spinner. But I don't know how to get my second spinner populated under id selected from first spinner. I'm using codeigniter rest controller to pass the id and want to retrieve it using retrofit for second spinner, but I don't know how to do this? Please help me.

I already created Rest Controller by passing the id. Already created ApiInterface. And beans to get details data. First spinner working good.

Declaration

 classSpinner = (Spinner) findViewById(R.id.classSpinner);
    divSpinner = (Spinner) findViewById(R.id.divSpinner);

    classSpinner.setAdapter(mAdapter);

    mApiInterface = ApiClient.getClient().create(ApiInterface.class);

    getAllProvinsi();

ApiInterface

public interface ApiInterface {

//first spinner
    @GET("Testpage/provinsi")
    Call<GetProvinsi> getProvinsi();

//second spinner that I needed how to pass the id? and get data by id selected from first spinner
    @GET("Testpage/kotabyprovid/provinsi_id/")
    Call<GetKota> getKota();
}

GetProvinsi ( First Spinner )

public class GetProvinsi {

@SerializedName("result")
List<Provinsi> listDataProvinsi;


public List<Provinsi> getListDataProvinsi() {
    return listDataProvinsi;
}
public void setListDataProvinsi(List<Provinsi> listDataProvinsi) {
    this.listDataProvinsi = listDataProvinsi;
}

}

GetKota ( Second Spinner )

public class GetKota {

@SerializedName("result")
List<Kota> listDataKota;


public List<Kota> getListDataKota() {
    return listDataKota;
}
public void setListDataKota(List<Kota> listDataKota) {
    this.listDataKota = listDataKota;
}

}

First Spinner to get all data. I'm trying to select the id to populate second spinner from the adapter.

private void getAllProvinsi() {

    Call<GetProvinsi> kontakCall = mApiInterface.getProvinsi();
    kontakCall.enqueue(new Callback<GetProvinsi>() {
        @Override
        public void onResponse(Call<GetProvinsi> call, Response<GetProvinsi>
                response) {
            List
                                                
                                                
1 views
0 shares
profilepic.png
manpreet 2 years ago

Replace

@GET("Testpage/kotabyprovid/provinsi_id/")  
Call<GetKota> getKota(); 

with

@GET("Testpage/kotabyprovid/provinsi_id/{prov_id}")  
Call<GetKota> getKota(@Path("prov_id") int provId);

Now you can pass id to the method, the retrofit will take care of passing provId to the URL path.


1 views   0 shares

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.