Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android : How to get the Id of selected item from Spinner

In my case I want to get the Id of selected item from Spinner. I have two fields in my modal class which are id and name. I'am listing all the data and am setting to this list to the adapter. I have tried to get the selectedItem Id by using the getSelectedItem() method. But i can only get the first id of the list of item.

This is my code.

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

This is CustomEventType adapter code.

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

Can someone help me to solve this problem?

like image 333
user3764346 Avatar asked Nov 08 '14 05:11

user3764346


2 Answers

Get selected item data from adapter list holder using position :

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

     }
});
like image 96
Haresh Chhelana Avatar answered Sep 27 '22 17:09

Haresh Chhelana


Sharing the sample code from my class to get the selected item id -

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

Cheers :)

like image 41
Ashish Tamrakar Avatar answered Sep 27 '22 17:09

Ashish Tamrakar