Android id fetching problem
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View arg1,
int pos, long arg3)
{
String name;
name=parent.getItemAtPosition(pos).toString();
String[] projection1 = new String[]{
People._ID
};
Cursor pCur=getContentResolver().query(People.CONTENT_URI, projection1,People._ID+"="+name,null,null);
pCur.moveToFirst();
if(pCur.moveToFirst())
{
String id;
int x=0;
do
{
id=pCur.getString(pCur.getColumnIn开发者_运维技巧dex(People._ID));
Toast.makeText(parent.getContext(),"resule::"+id,Toast.LENGTH_LONG).show();
x++;
}while(pCur.moveToNext());
}
}
public void onNothingSelected(AdapterView<?> arg0)
{
// DO Nothing
}
});
I am novice in android development.spinner has all contact name i want get id from particular name.please help me.
Thanks in advance
Regards Arpit Trivedi
If you mean to get particular id from the list than you should populate the spinner outside the setOnItemSelectedListener()
because you do it when the item is clicked. All you do to get the item data is name=parent.getItemAtPosition(pos);
like you did at the beginning. Just populate the spinner outside this method, let say at point after you fetch the contacts. I hope this helps.
Call this method with a name.You will get the id
public long idForName(String name){
int count=spinner.getCount();
for(int i=0;i<count;i++)
if(spinner.getItemAtPosition(i).equals(name)) return spinner.getItemIdAtPosition(i);
}
精彩评论