【问题标题】:How to use itemClickListener in Spinner on Android如何在 Android 上的 Spinner 中使用 itemClickListener
【发布时间】:2018-12-20 12:27:35
【问题描述】:

在我的应用程序中,我有一个Spinner,我应该将一些来自服务器的数据显示到这个Spinner
我的服务器数据有:

"sections": [{
            "id": 1,
            "name": "Item 1"
        }, {
            "id": 2,
            "name": "Item 2"
        }, {
            "id": 3,
            "name": "Item 3"
        }]

对于这个spinner,我应该设置默认文本,以便第一次显示以及当用户点击spinnersitem显示这个项目而不是默认文本。

为此,我编写了以下代码,但我不知道如何将 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;
    }

}

我的活动代码:

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。

怎么办?

【问题讨论】:

  • 使用 spinner.setOnItemSelectedListener
  • @Piyush,你能把我上面的代码发给我吗?拜托了
  • 检查this

标签: java android android-arrayadapter android-spinner


【解决方案1】:
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();
            }
        });

【讨论】:

    【解决方案2】:
        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);
    

    【讨论】:

    • 嘿,你在吗?我的代码有问题?你是来修的吗?
    • @Dr.Jake 当然。请让我知道问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多