Android:如何从Spinner获取所选项目的Id

Android:如何从Spinner获取所选项目的Id,android,spinner,android-spinner,Android,Spinner,Android Spinner,在本例中,我希望从微调器获取所选项目的Id。在模态类中有两个字段,分别是id和name。我正在列出所有数据,并将此列表设置为适配器。我尝试使用getSelectedItem()方法获取selectedItem Id。但我只能获取项目列表的第一个id 这是我的密码 public class ModifyEventFragment extends DialogFragment{ Context context; CalEvent eve; Project proj; Spinner eventTyp

在本例中,我希望从微调器获取所选项目的Id。在模态类中有两个字段,分别是id和name。我正在列出所有数据,并将此列表设置为适配器。我尝试使用getSelectedItem()方法获取selectedItem Id。但我只能获取项目列表的第一个id

这是我的密码

public class ModifyEventFragment extends DialogFragment{
Context context;
CalEvent eve;
Project proj;
Spinner eventType,stage;
public static String eid,pid,type;
public static List<EventType> event_type;
public static List<ProjectStatus> cust_stage;
EditText where,when,who,notes;
String eve_type,stage_val,when_val,who_val,notes_val;
String modified_where,modified_who,modified_when,modified_notes;
public ModifyEventFragment(Project proj)
{
    this.proj=proj;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.modify_project_event, container,
            false);
    context = rootView.getContext();
    eventType = (Spinner) rootView.findViewById(R.id.modifyEventType);
    stage =(Spinner) rootView.findViewById(R.id.modifyStage);
    where = (EditText)rootView.findViewById(R.id.modifyWhere);
    who = (EditText)rootView.findViewById(R.id.modifyWho);
    when = (EditText)rootView.findViewById(R.id.modifyWhen);
    notes =(EditText) rootView.findViewById(R.id.modifyNotes);
    eve = CalEvent.getCalEvent(ProjectEventFragment.calevent.eve_id);
    event_type = EventType.listAll();
    CustomEventTypeAdapter adapter = new CustomEventTypeAdapter(context, event_type);
    eventType.setAdapter(adapter);
    type=((EventType)eventType.getSelectedItem()).et_id;
    cust_stage = ProjectStatus.listAll();
    CustomStatusAdapter adapter1 = new CustomStatusAdapter(context, cust_stage);
    stage.setAdapter(adapter1);
    where.setText(eve.followup_location.toString());
    where.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // TODO Auto-generated method stub

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub
            modified_where = s.toString();
        }
    });
    who.setText(eve.person_met.toString());
    who.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // TODO Auto-generated method stub

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub
            modified_who = s.toString();
        }
    });
    when.setText(eve.event_start.toString());
    when.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // TODO Auto-generated method stub

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub
            modified_when = s.toString();
        }
    });
    notes.setText(eve.notes.toString());
    notes.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // TODO Auto-generated method stub

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub
            modified_notes = s.toString();
        }
    });
    Button save = (Button) rootView.findViewById(R.id.modifyeventsave);

    save.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            eve.followup_location = modified_where;
            eve.event_start = modified_when;
            eve.person_met = modified_who;
            eve.notes = modified_notes;
            System.out.println("print type"+type);
            eve.save();
            ProjectEventFragment.adapter.notifyDataSetChanged();
        }
    });

    return rootView;
}
公共类ModifyEventFragment扩展了DialogFragment{
语境;
夏娃;
项目项目;
旋转器事件类型、阶段;
公共静态字符串eid,pid,类型;
公共静态列表事件类型;
公共静态列表客户阶段;
编辑文本何处、何时、何人、注释;
字符串eve\u类型、stage\u val、when\u val、who\u val、notes\u val;
字符串修改的\u where、修改的\u who、修改的\u when、修改的\u notes;
公共ModifyEventFragment(项目项目)
{
这个.proj=proj;
}
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
视图根视图=充气机。充气(R.layout.modify\u项目\u事件,容器,
假);
context=rootView.getContext();
eventType=(微调器)rootView.findViewById(R.id.modifyEventType);
stage=(微调器)rootView.findviewbyd(R.id.modifyStage);
其中=(EditText)rootView.findViewById(R.id.modifyWhere);
who=(EditText)rootView.findViewById(R.id.modifyWho);
when=(EditText)rootView.findViewById(R.id.modifyWhen);
notes=(EditText)rootView.findviewbyd(R.id.modifyNotes);
eve=CalEvent.getCalEvent(ProjectEventFragment.CalEvent.eve\u id);
event_type=EventType.listAll();
CustomEventTypeAdapter=新的CustomEventTypeAdapter(上下文,事件类型);
setAdapter(适配器);
type=((EventType)EventType.getSelectedItem()).et\u id;
cust_stage=ProjectStatus.listAll();
CustomStatusAdapter adapter1=新的CustomStatusAdapter(上下文,客户阶段);
阶段。设置适配器(适配器1);
where.setText(eve.followup_location.toString());
where.addTextChangedListener(新的TextWatcher(){
@凌驾
public void onTextChanged(字符序列、int start、int before、int count){
//TODO自动生成的方法存根
}
@凌驾
更改前的公共无效(字符序列、整数开始、整数计数、,
整数后){
//TODO自动生成的方法存根
}
@凌驾
公共无效后文本已更改(可编辑){
//TODO自动生成的方法存根
修改的_,其中=s.toString();
}
});
who.setText(eve.person_meet.toString());
who.addTextChangedListener(新的TextWatcher(){
@凌驾
public void onTextChanged(字符序列、int start、int before、int count){
//TODO自动生成的方法存根
}
@凌驾
更改前的公共无效(字符序列、整数开始、整数计数、,
整数后){
//TODO自动生成的方法存根
}
@凌驾
公共无效后文本已更改(可编辑){
//TODO自动生成的方法存根
修改的_who=s.toString();
}
});
when.setText(eve.event_start.toString());
when.addTextChangedListener(新的TextWatcher(){
@凌驾
public void onTextChanged(字符序列、int start、int before、int count){
//TODO自动生成的方法存根
}
@凌驾
更改前的公共无效(字符序列、整数开始、整数计数、,
整数后){
//TODO自动生成的方法存根
}
@凌驾
公共无效后文本已更改(可编辑){
//TODO自动生成的方法存根
当=s.toString()时修改的_;
}
});
notes.setText(eve.notes.toString());
notes.addTextChangedListener(新的TextWatcher(){
@凌驾
public void onTextChanged(字符序列、int start、int before、int count){
//TODO自动生成的方法存根
}
@凌驾
更改前的公共无效(字符序列、整数开始、整数计数、,
整数后){
//TODO自动生成的方法存根
}
@凌驾
公共无效后文本已更改(可编辑){
//TODO自动生成的方法存根
修改的注释=s.toString();
}
});
按钮保存=(按钮)rootView.findViewById(R.id.modifyeventsave);
save.setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图v){
//TODO自动生成的方法存根
eve.followup\u位置=修改的\u位置;
eve.event\u start=修改的\u时间;
eve.person\u met=修改的\u who;
eve.notes=修改的注释;
System.out.println(“打印类型”+类型);
eve.save();
ProjectEventFragment.adapter.notifyDataSetChanged();
}
});
返回rootView;
}
这是CustomEventType适配器代码

public class CustomEventTypeAdapter extends BaseAdapter{
Context ctx;
List<EventType> ps;
LayoutInflater inflater;
public CustomEventTypeAdapter(Context ctx,List<EventType> ps)
{
    this.ctx=ctx;
    this.ps=ps;
    inflater = (LayoutInflater) ctx
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
}
@Override
public int getCount() {
    // TODO Auto-generated method stub
    return ps.size();
}

@Override
public EventType getItem(int position) {
    // TODO Auto-generated method stub
    return ps.get(position);
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return ps.get(position).getId();
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View rowView = convertView;
    if (convertView == null)
        rowView = inflater.inflate(R.layout.spinner_item_local, parent,
                false);
    TextView textView = (TextView) rowView.findViewById(R.id.spinner_item_text);

    EventType proj = getItem(position);
    try {
        textView.setText(proj.name);

    } catch (Exception e) {

    }

    return rowView;
}
公共类CustomEventTypeAdapter扩展了BaseAdapter{
上下文ctx;
列表P;
充气机;
公共CustomEventTypeAdapter(上下文ctx,列表ps)
{
这个.ctx=ctx;
这个.ps=ps;
充气机=(充气机)ctx
.getSystemService(上下文布局\充气机\服务);
}
@凌驾
public int getCount(){
//TODO自动生成的方法存根
返回ps.size();
}
@凌驾
公共事件类型getItem(int位置){
//TODO自动生成的方法存根
返回ps.get(位置);
}
@凌驾
公共长getItemId(int位置){
//TODO自动生成的方法存根
返回ps.get(position.getId();
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
视图行视图=转换视图;
if(convertView==null)
行视图=充气器。充气(R.layout.spinner\u项目\u本地,父级,
假);
TextView TextView=(TextView)rowView.findViewById(R.id.spinner\u item\u text);
spinner1 .setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                // TODO Auto-generated method stub
                spinner1 = parent.getItemAtPosition(position).toString();
                count = position; //this would give you the id of the selected item
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub
            }
        });
eventType.setOnItemSelectedListener(new OnItemSelectedListener() {
     @Override
     public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
         String id = event_type.get(position).getId();
     }

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

     }
});
public class ProjectStatus {

    private Integer id; // Long, String any type

    private String name;

    // more properties if you need

    @Override  // Mandatory
    public String toString() {
        return name; // name + " ("+id+")"; //  can also be returned
    }

    // Getter & Setter
}
//Task 1.
List<ProjectStatus> cust_stages = ProjectStatus.listAll(); // Or using any JSON Data OR API call;

//Task 2.
ArrayAdapter<ProjectStatus> statusAdapter = new ArrayAdapter<ProjectStatus>(
        this, android.R.layout.simple_spinner_item, cust_stages
    );
statusAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

//Task 3.
Spinner stage =(Spinner) rootView.findViewById(R.id.modifyStage);
stage.setAdapter(statusAdapter);
// In any OnClick / OnSelect event
ProjectStatus selectedStatus = (ProjectStatus) stage.getSelectedItem();
Integer statusId = selectedStatus.getId();
String statusName = selectedStatus.getName();
// and if you have more properties