Android 如何在 Spinner 中使用 itemClickListener
How to use itemClickListener in Spinner on Android
在我的应用程序中,我有一个 Spinner
,我应该将一些来自服务器的数据显示到这个 Spinner
。
我的服务器数据有:
"sections": [{
"id": 1,
"name": "Item 1"
}, {
"id": 2,
"name": "Item 2"
}, {
"id": 3,
"name": "Item 3"
}]
为此 spinner
我应该设置 默认文本 以便首次显示以及当用户 在 spinners
上单击 ] item 显示此项目而不是默认文本。
为此我写了下面的代码,但我不知道如何为这个项目设置 clickListener
以获得每个项目的 ID!
我的适配器代码:
public class DashboardSupportSectionAdapter extends ArrayAdapter<Section> {
Context context;
List<Section> model;
String firstElement;
boolean isFirstTime;
public DashboardSupportSectionAdapter(Context context, int textViewResourceId, List<Section> model, String defaultText) {
super(context, textViewResourceId, model);
this.context = context;
this.model = model;
this.isFirstTime = true;
setDefaultText(defaultText);
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
if (isFirstTime) {
model.get(0).setName(firstElement);
isFirstTime = false;
}
return getCustomView(position, convertView, parent);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
notifyDataSetChanged();
return getCustomView(position, convertView, parent);
}
public void setDefaultText(String defaultText) {
this.firstElement = model.get(0).getName();
model.get(0).setName(defaultText);
}
public View getCustomView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.row_dashboard_support_section, parent, false);
TextView label = row.findViewById(R.id.spinner_text);
label.setText(model.get(position).getName());
return row;
}
}
我的Activity代码:
public class DashboardCreateSupportActivity extends BaseActivity {
@BindView(R.id.dashboardCreateSupport_sectionSpinner)
Spinner dashboardCreateSupport_sectionSpinner;
String defaultTextForSpinner = "Select section";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dashboard_create_support);
ButterKnife.bind(this);
dashboardCreateSupport_sectionSpinner.setAdapter(new DashboardSupportSectionAdapter(this, R.layout.row_dashboard_support_section,
Constants.supportListResponse.getRes().getSections(), defaultTextForSpinner));
}
}
我想要在每个项目上 单击 时,在 toast
消息中显示项目的 ID。
怎么办?
dashboardCreateSupport_sectionSpinner.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Constants.supportListResponse.getRes().getSections().get(position).getId();
}
});
dashboardCreateSupport_sectionSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Section item = Constants.supportListResponse.getRes().getSections().getItem(position);
//use item object
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
使用 setOnItemSelectedListener。
我什至建议修改如下代码:
final DashboardSupportSectionAdapter dashboardSupportSectionAdapter =new DashboardSupportSectionAdapter(this, android.R.layout.simple_list_item_1,
sections, "default");
dashboardCreateSupport_sectionSpinner.setAdapter(dashboardSupportSectionAdapter);
和 onItemSelected 内部
Section item = dashboardSupportSectionAdapter.getItem(position);
在我的应用程序中,我有一个 Spinner
,我应该将一些来自服务器的数据显示到这个 Spinner
。
我的服务器数据有:
"sections": [{
"id": 1,
"name": "Item 1"
}, {
"id": 2,
"name": "Item 2"
}, {
"id": 3,
"name": "Item 3"
}]
为此 spinner
我应该设置 默认文本 以便首次显示以及当用户 在 spinners
上单击 ] item 显示此项目而不是默认文本。
为此我写了下面的代码,但我不知道如何为这个项目设置 clickListener
以获得每个项目的 ID!
我的适配器代码:
public class DashboardSupportSectionAdapter extends ArrayAdapter<Section> {
Context context;
List<Section> model;
String firstElement;
boolean isFirstTime;
public DashboardSupportSectionAdapter(Context context, int textViewResourceId, List<Section> model, String defaultText) {
super(context, textViewResourceId, model);
this.context = context;
this.model = model;
this.isFirstTime = true;
setDefaultText(defaultText);
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
if (isFirstTime) {
model.get(0).setName(firstElement);
isFirstTime = false;
}
return getCustomView(position, convertView, parent);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
notifyDataSetChanged();
return getCustomView(position, convertView, parent);
}
public void setDefaultText(String defaultText) {
this.firstElement = model.get(0).getName();
model.get(0).setName(defaultText);
}
public View getCustomView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.row_dashboard_support_section, parent, false);
TextView label = row.findViewById(R.id.spinner_text);
label.setText(model.get(position).getName());
return row;
}
}
我的Activity代码:
public class DashboardCreateSupportActivity extends BaseActivity {
@BindView(R.id.dashboardCreateSupport_sectionSpinner)
Spinner dashboardCreateSupport_sectionSpinner;
String defaultTextForSpinner = "Select section";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dashboard_create_support);
ButterKnife.bind(this);
dashboardCreateSupport_sectionSpinner.setAdapter(new DashboardSupportSectionAdapter(this, R.layout.row_dashboard_support_section,
Constants.supportListResponse.getRes().getSections(), defaultTextForSpinner));
}
}
我想要在每个项目上 单击 时,在 toast
消息中显示项目的 ID。
怎么办?
dashboardCreateSupport_sectionSpinner.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Constants.supportListResponse.getRes().getSections().get(position).getId();
}
});
dashboardCreateSupport_sectionSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Section item = Constants.supportListResponse.getRes().getSections().getItem(position);
//use item object
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
使用 setOnItemSelectedListener。
我什至建议修改如下代码:
final DashboardSupportSectionAdapter dashboardSupportSectionAdapter =new DashboardSupportSectionAdapter(this, android.R.layout.simple_list_item_1,
sections, "default");
dashboardCreateSupport_sectionSpinner.setAdapter(dashboardSupportSectionAdapter);
和 onItemSelected 内部
Section item = dashboardSupportSectionAdapter.getItem(position);