简体   繁体   中英

Populate spinner [custom adapter] retrofit

I have a custom adapter that I want to use to populate my spinner. I am using retrofit and I want to get the response from the JSon file and pass it to the spinner. I have tried to call the list items to the spinner but I am stuck. Iam new to android and Java. Thanks in advance

Call<RegistrationDropdownResponse> call = apiService.dropDownresponse(commonRequest);
    call.enqueue(new Callback<RegistrationDropdownResponse>() {
        @Override
        public void onResponse(Call<RegistrationDropdownResponse> call, Response<RegistrationDropdownResponse> response) {

            if (response.isSuccessful()) {
                Log.e("RESPONSE", new Gson().toJson(response.body()));
                Constants.registrationDropdownResponse = response.body();

                CustomAdapter adapter = new CustomAdapter(this,);
                spCompany.setAdapter(adapter);
            }

        }

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

        }
    });    

Here is my response class

public class RegistrationDropdownResponse {
private List<MaritalStatus> marital;
private List<IncomeBand> income;
private List<EducationLevel> education;
private List<Rental> rental;
private List<EmploymentLevel> employment;

public List<MaritalStatus> getMarital() {

    return marital;
}

public List<IncomeBand> getIncome() {
    return income;
}

public List<EducationLevel> getEducation() {
    return education;
}

public List<Rental> getRental() {
    return rental;
}

public List<EmploymentLevel> getEmployment() {
    return employment;
}

}

CustomAdapter

public class CustomAdapter extends BaseAdapter implements SpinnerAdapter {

private ArrayList<SpinnerArrayObject> spinnerArrayObjects;
private Context context;

public CustomAdapter(Context context,  ArrayList<SpinnerArrayObject> spinnerArrayObjects) {
    this.spinnerArrayObjects = spinnerArrayObjects;
    this.context = context;
}

@Override
public int getCount() {
    return spinnerArrayObjects.size();
}

@Override
public SpinnerArrayObject getItem(int position) {
    return spinnerArrayObjects.get(position);
}

@Override
public long getItemId(int position) {
    return
            spinnerArrayObjects.get(position).getId();
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view = View.inflate(context, R.layout.company_main, null);
    TextView textView = (TextView) view.findViewById(R.id.main);
    textView.setText((CharSequence) spinnerArrayObjects.get(position));
    return textView;
}

public View getDropDownView(int position, View convertView, ViewGroup parent) {

    View view;
    view = View.inflate(context, R.layout.company_dropdown, null);
    final TextView textView = (TextView) view.findViewById(R.id.dropdown);
    textView.setText(spinnerArrayObjects.size());
    return view;
}

}

spinner arrayObject

public class SpinnerArrayObject {
private int id;
private String name;

public SpinnerArrayObject() {
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

}

Ok, so to implement this you need first of all to create you xml view, for example:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/name_text_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

Now in adapter in function getView() you need to inflate it and bind the TextView :

convertView = LayoutInflater.from(context).
                        inflate(R.layout.layout_list_view_row_items, parent, false);
TextView nameTextView = (TextView) convertView.findViewById(R.id.name_text_view);


//and now get the SpinnerArrayObject by position


 SpinnerArrayObject currentObject = spinnerArrayObjects.get(position);
    nameTextView.setText(currentObject.getName());

    return convertView;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM