setSelection on Spinner based on rowId

前端 未结 7 703
离开以前
离开以前 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:22

    First step, create view for your data set, with joins etc.:

    CREATE VIEW my_view AS
      SELECT _id, field FROM my_table
    

    Second step:

    CREATE VIEW my_view2 AS
      SELECT count(*) AS row_id, q1.*
      FROM my_view AS q1
      LEFT JOIN my_view AS q2
      WHERE q1._id >= q2._id
      GROUP BY q1._id
    

    Then simply:

    SELECT * FROM my_view2
    

    Results:

    row_id | _id | field
    
    1   4   XbMCmUBFwb
    2   6   Te JMejSaK
    3   8   dDGMMiuRuh
    4   10  phALAbnq c
    5   11  EQQwPKksIj
    6   12  PAt tbDnf
    7   13  f zUSuhvM
    8   14  TIMBgAGhkT
    9   15  OOcnjKLLER
    

    To get position by id:

    SELECT * FROM my_view2 WHERE _id=11
    

    Results:

    row_id | _id | field
    
    5   11  EQQwPKksIj
    

    Hope that help

    0 讨论(0)
  • 2020-12-14 22:30

    If you want to set the selection of a Spinner thats backed by a CursorAdapter, you can loop through all the items in the Cursor and look for the one you want (assuming that the primary key in your table is named "_id"):

    Spinner spinner = (Spinner) findViewById(R.id.spinner);
    spinner.setAdapter(new SimpleCursorAdapter(...));
    
    for (int i = 0; i < spinner.getCount(); i++) {
        Cursor value = (Cursor) spinner.getItemAtPosition(i);
        long id = value.getLong(value.getColumnIndex("_id"));
        if (id == rowid) {
            spinner.setSelection(i);
        }
    }
    

    If you want to get the rowid of a selected item, you can do something similar:

    Cursor cursor = (Cursor) spinner.getSelectedItem();
    long rowid = cursor.getLong(cursor.getColumnIndex("_id"));
    

    There might be a quicker way to do it, but that's always worked for me.

    0 讨论(0)
  • 2020-12-14 22:33

    I think that instead of a for loop is better a while, because when you find your item, can break the loop.

    int spinnerCount = spinner.getCount();
    int i = 0;
    while(i++ < spinnerCount) {
        Cursor value = (Cursor) spinner.getItemAtPosition(i);
        long id = value.getLong(value.getColumnIndex("_id"));
        if (id == rowid) {
            spinner.setSelection(i);
            break;
        }
    }
    
    0 讨论(0)
  • 2020-12-14 22:33

    https://stackoverflow.com/a/5040748/1206052

    dziobas

    has provided an awesome answer.. But I am not surprised why nobody has recommended it.. just want to make some correction in his answer..

    CREATE VIEW my_view2 AS
      SELECT count(*) AS row_id, q1.*
      FROM my_view AS q1
      LEFT JOIN my_view AS q2
      ON (q1._id >= q2._id)
      GROUP BY q1._id
    

    I just replaced "where" with "on" that is required by join..

    now u have all the items with their Positions associated with there ID's..

    Just assign the ROW_id to the setselection() of spinner

    0 讨论(0)
  • 2020-12-14 22:34

    Why do it the hard way when you can do it the right way?

    I refer to the manual:

    http://d.android.com/reference/android/widget/AdapterView.OnItemSelectedListener.html#onItemSelected%28android.widget.AdapterView%3C?%3E,%20android.view.View,%20int,%20long%29

    example code:

    spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
            {
                int index = spinner.getSelectedItemPosition();
                Toast.makeText(getBaseContext(), 
                    "You have selected item : " + index + " which is row " + id, 
                    Toast.LENGTH_SHORT).show();                
            }
            public void onNothingSelected(AdapterView<?> arg0) {}
        });
    

    Thanks to evancharlton on #android-dev for this enlightment. :)

    0 讨论(0)
  • 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<Type> 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);
        }
    }
    
    0 讨论(0)
提交回复
热议问题