0

I am new to android development. I want number according to name that is stored in spinner how can I do in1.6 using query please help me.

spinner.setOnItemSelectedListener(new OnItemSelectedListener() 
  {
    @Override
    public void onNothingSelected(AdapterView<?> arg0)
    {
    }
    @Override
    public void onItemSelected(AdapterView<?> parent, View arg1,
            int pos, long arg3) 
    {
        String name,s;
        int i;
        name=parent.getItemAtPosition(pos).toString();

              //String projection1[]={People._ID,People.NAME,People.NUMBER};    
              //Cursor cur=getContentResolver().query(People.CONTENT_URI, 
              //               projection1,People.NAME+"="+name,null,null); 
              // s=cur.getString(cur.getColumnIndex(People._ID));
        Toast.makeText(parent.getContext (),name,Toast.LENGTH_LONG).show();
    }
});
phihag
  • 278,196
  • 72
  • 453
  • 469
Arpit
  • 21
  • 6

2 Answers2

0

Proceed as you started query the contnent provider, get all the numbers, then loop through the cursors data in the same time checking if the name equals with the name you are looking for.

Something like

cur.moveToFirst();
boolean found=false;
while(cur.isAfterLast()==false || found==false)
{
     if(name.equalsIgnoreCase(cur.getString(cur.getColumnIndex(People._ID)))
     {
       found==true;
      // other things to do when the name is found
     }
cur.moveToNext();
}
Nikola Despotoski
  • 49,966
  • 15
  • 119
  • 148
  • thanks nikola i can not use like that Cursor cur=getContentResolver().query(People.CONTENT_URI, projection1,People.NAME+"="+name,null,null); and after that use people.NUMBER – Arpit Jul 27 '11 at 14:44
0

Loop is simple but inefficient for large data. It can be done on database side:

Answered here: How can I get the position of a row in a dataset in order to set the selected item in an Android Spinner?

Community
  • 1
  • 1
pawelzieba
  • 16,082
  • 3
  • 46
  • 72