setSelection on Spinner based on rowId

前端 未结 7 706
离开以前
离开以前 2020-12-14 22:19

I have a Spinner View that\'s populated through a SimpleCursorAdapter.

Based on the selection I need to save the rowid in the entry database (posit

7条回答
  •  执笔经年
    2020-12-14 22:35

    I agree with Erich Douglass's above answer but i found fastest loop syntax which will be useful while spinner.getCount() is greater than 50k to 100k.

    /* 1 (fastest) */
    for (int i = initializer; i >= 0; i--) { ... }
    
    /* 2 */
    int limit = calculateLoopLimit();
    for (int i = 0; i < limit; i++) { ... }
    
    /* 3 */
    Type[] array = getMyArray();
    for (Type obj : array) { ... }
    
    /* 4 */
    for (int i = 0; i < array.length; i++) { ... }
    
    /* 5 */
    for (int i = 0; i < this.var; i++) { ... }
    
    /* 6 */
    for (int i = 0; i < obj.size(); i++) { ... }
    
    /* 7 (slowest) */
    Iterable list = getMyList();
    for (Type obj : list) { ... }
    

    So i think we can use here second for better performance:

    int spinnerCount = spinner.getCount();
    for (int i = 0; i < spinnerCount; i++) {
        Cursor value = (Cursor) spinner.getItemAtPosition(i);
        long id = value.getLong(value.getColumnIndex("_id"));
        if (id == rowid) {
            spinner.setSelection(i);
        }
    }
    

提交回复
热议问题