【问题标题】:How to set Spinner Default by its Value instead of Position?如何通过其值而不是位置设置微调器默认值?
【发布时间】:2012-05-24 22:32:06
【问题描述】:

我在数据库中有 1-50 条记录。我正在使用游标获取这些数据,并使用简单游标适配器将这些值设置为 Spinner。现在我需要的是我想设置一个值,比如第 39 个值作为默认值。但不是通过它的位置,我想通过它的值来设置。

我知道如何通过位置设置微调器默认值

   spinner.setSelection(39) 

将微调器设置为该值。

但我不知道通过数据库中的值(文本)设置微调器默认值。 我知道数据库中的值。例如,“书籍”是微调器中的价值之一。我需要将微调器默认设置为书籍。

有没有办法做到这一点?

【问题讨论】:

  • 您似乎只需要从数据库中获取一个值。还是我误会了什么?
  • @keyser No.. 我从数据库中获取值并使用简单的游标适配器将其设置为微调器。现在我需要将微调器的一个值设置为默认值而不是它的位置..我解释清楚了吗
  • 不,看来您仍然只需要从 db 本身获取值并使用 pinner.setSelection(somepositionvalue) 进行设置。

标签: android spinner simplecursoradapter android-spinner


【解决方案1】:

如果您通过arraylistarray 设置微调器值,则可以使用值的索引设置微调器的选择。

String myString = "some value"; //the value you want the position for

ArrayAdapter myAdap = (ArrayAdapter) mySpinner.getAdapter(); //cast to an ArrayAdapter

int spinnerPosition = myAdap.getPosition(myString);

//set the default according to value
spinner.setSelection(spinnerPosition);

查看链接How to set selected item of Spinner by value, not by position?

【讨论】:

  • 谢谢你的回答..我知道它可以使用 ArrayAdapter.. 简单光标适配器也可以吗??
  • @Vino 是的,请查看以下答案:stackoverflow.com/a/11667045/293280
  • 这才是真正的答案。
【解决方案2】:

最后,我通过以下方式解决了这个问题,其中微调器的位置可以通过它的字符串得到

private int getPostiton(String locationid,Cursor cursor)
{
    int i;
    cursor.moveToFirst(); 
    for(i=0;i< cursor.getCount()-1;i++)
    {  

        String locationVal = cursor.getString(cursor.getColumnIndex(RoadMoveDataBase.LT_LOCATION));  
        if(locationVal.equals(locationid))
        { 
            position = i+1;  
            break;
        }
        else
        {
            position = 0;
        }
        cursor.moveToNext();  
    } 

调用方法

    Spinner location2 = (Spinner)findViewById(R.id.spinner1);
    int location2id = getPostiton(cursor.getString(3),cursor);
    location2.setSelection(location2id);

希望对大家有所帮助。

【讨论】:

  • 也许更精简和更优雅的版本可能看起来像这样: public class SpinnerHelper { public static int getPosition(String displayText, String fieldName, Cursor cursor) { int position = -1; cursor.moveToFirst(); while(cursor.moveToNext()) { 字符串文本 = cursor.getString(cursor.getColumnIndex(fieldName)); if (text.equals(displayText)) { position = cursor.getPosition();休息; } } 返回位置; } }
【解决方案3】:

比较字符串和索引中的值

private void selectSpinnerValue(Spinner spinner, String myString)
     {
         int index = 0;
         for(int i = 0; i < spinner.getCount(); i++){
             if(spinner.getItemAtPosition(i).toString().equals(myString)){
                 spinner.setSelection(i);
                 break;
             }
         }
     }

【讨论】:

    【解决方案4】:

    我就是这样做的:

    String[] listAges = getResources().getStringArray(R.array.ages);
    
            // Creating adapter for spinner
            ArrayAdapter<String> dataAdapter =
                    new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, listAges);
    
            // Drop down layout style - list view with radio button
            dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    
            // attaching data adapter to spinner
            spinner_age.getBackground().setColorFilter(ContextCompat.getColor(this, R.color.spinner_icon), PorterDuff.Mode.SRC_ATOP);
            spinner_age.setAdapter(dataAdapter);
            spinner_age.setSelection(0);
            spinner_age.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                    String item = parent.getItemAtPosition(position).toString();
    
                    if(position > 0){
                        // get spinner value
                        Toast.makeText(parent.getContext(), "Age..." + item, Toast.LENGTH_SHORT).show();
                    }else{
                        // show toast select gender
                        Toast.makeText(parent.getContext(), "none" + item, Toast.LENGTH_SHORT).show();
                    }
                }
                @Override
                public void onNothingSelected(AdapterView<?> parent) {
                }
            });
    

    【讨论】:

      【解决方案5】:

      你可以像这样轻松地做到这一点。

      String cls=student.getStudentClass();
      class_spinner.setSelection(classArray.indexOf(cls),true);
      

      【讨论】:

        【解决方案6】:

        如果您用于微调器的列表是一个对象,那么您可以像这样找到它的位置

        private int selectSpinnerValue( List<Object> ListSpinner,String myString) 
        {
            int index = 0;
            for(int i = 0; i < ListSpinner.size(); i++){
              if(ListSpinner.get(i).getValueEquals().equals(myString)){
                  index=i;
                  break;
              }
            }
            return index;
        }
        

        使用:

         int index=selectSpinnerValue(ListOfSpinner,StringEquals);
            spinner.setSelection(index,true);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-01-22
          • 2015-12-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多