How to set and get the id for the items in the spinner in Android
Posted
by
Haresh Chaudhary
on Stack Overflow
See other posts from Stack Overflow
or by Haresh Chaudhary
Published on 2012-03-31T07:51:09Z
Indexed on
2012/09/10
9:38 UTC
Read the original article
Hit count: 286
I have a problem in my project that i am displaying a activity which would contain the details of the project which is previously added in Project Management System..
Now the fields in it are like:
Fields of the Activity
Name Of Project: ABC(EditText)
Name Of Developer : ________(Spinner)
Deadline : ________(Date Picker)
Created On : _______(TextView)
.
.
Now, the Spinner contains the Names of all developers working in the Company..I have used the ArrayAdapter
with a array having the names of all the developers which is fetched from the database..
The problem is that when i update the Name Of Developer field, i get Only the Name of the Developer which is not enough to update the data as their can be multiple developers with the same name in the Company..So now I require the id of the developers to update it..
How to store that id of the developers with the Spinner so that i can achieve this..
Please help me to sort this out.
Actually what i want to do like is as that we do in html::
<select>
<option id="1">Developer1</option>
<option id="2">Developer2</option>
<option id="3">Developer2</option>
<option id="4">Developer2</option>
</select>
where the id attached would be the database id....I want to imitate this in our android....
This the code that i have used to store the names in the array::
String alldevelopers = null;
try {
ArrayList<NameValuePair> postP = new ArrayList<NameValuePair>();
alldevelopers = CustomHttpClient.executeHttpPost(
"/fetchdevelopers.php", postP);
String respcl = alldevelopers.toString();
alldevelopersParser dev = new alldevelopersParser();
ow = dev.parseByDOM(respcl);
} catch (Exception e) {
e.printStackTrace();
}
String developers[] = new String[ow.length]; //dev is a class object
for (int n = 0; n < ow.length; n++)
{
developers[n] = ow.developer[n][2];
}
This is the Spinner that would spin the array..
final Spinner devl = (Spinner) findViewById(R.id.towner);
devl.setOnItemSelectedListener(managetasks.this);
ArrayAdapter<String> b =
new ArrayAdapter<String>getApplicationContext(),
android.R.layout.simple_spinner_item,developers);
b.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
devl.setAdapter(b);
© Stack Overflow or respective owner