【问题标题】:How to use query for specific number?如何使用查询特定号码?
【发布时间】:2011-07-27 14:26:21
【问题描述】:

我是安卓开发新手。我想根据存储在微调器中的名称进行编号,我该如何使用查询在 1.6 中进行操作,请帮助我。

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

【问题讨论】:

    标签: android widget


    【解决方案1】:

    在您开始查询内容提供程序时继续,获取所有数字,然后在同一时间循环游标数据,检查名称是否与您要查找的名称相同。

    类似

    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 我不能那样使用 Cursor cur=getContentResolver().query(People.CONTENT_URI, projection1,People.NAME+"="+name,null,null);然后使用 people.NUMBER
    【解决方案2】:

    对于大数据,循环很简单但效率低下。可以在数据库端完成:

    在这里回答:How can I get the position of a row in a dataset in order to set the selected item in an Android Spinner?

    【讨论】:

      猜你喜欢
      • 2018-02-10
      • 2021-10-31
      • 1970-01-01
      • 2022-12-11
      • 2018-02-16
      • 2015-08-29
      • 2014-03-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多