【问题标题】:Populate spinner [custom adapter] retrofit填充微调器 [自定义适配器] 改造
【发布时间】:2019-01-29 21:37:24
【问题描述】:

我有一个自定义适配器,我想用它来填充我的微调器。我正在使用改造,我想从 JSon 文件中获取响应并将其传递给微调器。 我试图将列表项调用到微调器,但我被卡住了。我是 android 和 Java 的新手。提前致谢

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

        }
    });    

这是我的响应类

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

}

自定义适配器

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

}

微调器数组对象

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

}

【问题讨论】:

  • CustomAdapter 的基类是什么?
  • 我已经添加了我的 CustomAdapter 类,
  • 请发布SpinnerArrayObject的代码

标签: java android retrofit2


【解决方案1】:

好的,所以要实现这个你首先需要创建你的xml视图,例如:

<?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>

现在在getView()函数中的适配器中,你需要给它充气并绑定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;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多