填充微调器[定制适配器]改造

时间:2019-01-29 10:22:47

标签: java android retrofit2

我有一个自定义适配器,我想用它来填充微调器。我正在使用改造,我想从JSon文件中获取响应并将其传递给微调器。 我曾尝试将列表项称为微调器,但我被卡住了。 Iam是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;
}

}

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

}

旋转数组对象

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

}

1 个答案:

答案 0 :(得分:0)

好,因此要实现此目的,首先需要创建xml视图,例如:

<body>
  <div id="page-wrapper">
    <header>
      <h3>1 Header</h3>
    </header>
    
    <div class="side">
      <div class="sidebar">
        2 <ul>
          <li>Menu item </li>
          <li>Menu item </li>
          <li>Menu item </li>
          <li>Menu item </li>
          <li>Menu item </li>
          <li>Menu item </li>
          <li>Menu item </li>
          <li>Menu item </li>
          <li>Menu item </li>
          <li>Menu item </li>
        </ul>
      </div>
      <div class="sidebar1">4</div>
      <div class="sidebar2">6</div>
    </div>
    
    <div class="content">
      <article>
        3 <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
      </article>

      <div class="bottom">5</div>
      <div class="base">7</div>
    </div>
		
    <footer><h3>8 Footer</h3></footer>
  </div>
</body>

现在在函数<?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