开发者

How to set spinner selection by id not position

开发者 https://www.devze.com 2023-01-30 07:04 出处:网络
I have created a simple Spinner binding it to a SimpleCursorAdapter.I\'m populating the SimpleCursorAdapter with a list of towns from a content provider.

I have created a simple Spinner binding it to a SimpleCursorAdapter. I'm populating the SimpleCursorAdapter with a list of towns from a content provider.

When I go to save the users selection I'm planning on saving the row id that is being populated into my SimpleCursorAdapter.

I'm using the following code to get the ID.

townSpinner.getSelectedItemId();

What I can not figure out is how best to set the 开发者_StackOverflow社区selection when I pull back up the saved item.

The following code works but it only sets selection by position number.

townSpinner.setSelection(2);

Should I just create a loop to determine the correct position value based on Id?

long cityId = Long.parseLong(cursor.getString(CityQuery.CITY_ID));

for (int i = 0; i < citySpinner.getCount(); i++) {              
    long itemIdAtPosition2 = citySpinner.getItemIdAtPosition(i);
    if (itemIdAtPosition2 == cityId) {
        citySpinner.setSelection(i);
        break;
    }
}


I think you've answered your own question there! Just write your own setSelectionByItemId method using the code you posted


Example:

DB:
    public Cursor getData() {
        SQLiteDatabase db = getReadableDatabase();
        String sql = "select ID _id, Name from MyTable order by Name ";
        Cursor c = db.rawQuery(sql, null);
        c.moveToFirst();
        return c;
    }   

Activity:

        Cursor myCursor = db.getData();

        SimpleCursorAdapter adapter1 = new SimpleCursorAdapter(
          this, 
          android.R.layout.simple_spinner_item, 
          myCursor,
          new String[] { "Name" }, new int[] { android.R.id.text1 }, 0);

        adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        mySpinner.setAdapter(adapter1);         

        for(int i = 0; i < adapter1.getCount(); i++)
        {
            if (adapter1.getItemId(i) == myID )
            {
                mySpinner.setSelection(i, false); //(false is optional)
                break;
            }
        }   


Android Spinner set Selected Item by Value

The spinner provides a way to set the selected valued based on the position using the setSelection(int position) method. Now to get the position based on a value you have to loop thru the spinner and get the position. Here is an example

  mySpinner.setSelection(getIndex(mySpinner, myValue));
  private int getIndex(Spinner spinner, String myString){

 int index = 0;

 for (int i=0;i<spinner.getCount();i++){
  if (spinner.getItemAtPosition(i).equals(myString)){
   index = i;
   }
 }
return index;
}

If you are using an ArrayList for your Spinner Adapter then you can use that to loop thru and get the index. Another way is is to loop thru the adapter entries

    Spinner s = (Spinner) findViewById(R.id.spinner_id);
    for(i=0; i < adapter.getCount(); i++) {
     if(myString.trim().equals(adapter.getItem(i).toString())){
       s.setSelection(i);
       break;
    }
  }

0

精彩评论

暂无评论...
验证码 换一张
取 消