我有一个自定义适配器,我想用它来填充我的微调器。我正在使用改造,我想从 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;
}
最佳答案
好的,所以要实现这一点,您首先需要创建 xml View ,例如:
<?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()
中的适配器中,您需要对其进行充气并绑定(bind) 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;
关于java - 填充微调器 [自定义适配器] 改造,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54418805/