如何在Android上的Spinner中使用itemClickListener

时间:2018-12-20 12:27:36

标签: java android android-arrayadapter android-spinner

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

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

为此,spinner我应该设置默认文本,以便在用户第一次{strong}点击 spinners 项目显示此项目,而不显示默认文本。

为此,我编写了以下代码,但我不知道如何为该项目设置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。

我怎么办?

3 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

dashboardCreateSupport_sectionSpinner.setOnItemClickListener(new AdapterView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                }

                @Override
                public void onNothingSelected(AdapterView<?> parent) {

                }
            });

答案 2 :(得分:0)

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