setSelection on Spinner based on rowId
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 (position won't work 开发者_如何学Cbecause things can be added and deleted from the Spinner Database).
This I can do by using spinner.getAdapter().getItemId(pos);
. But When I edit an entry I need to make the Spinner position selected that is associated with this rowid (currently).
spinner.setSelection(position);
won't work because I have the rowid, I need a way to find the current position of the item in the current spinner based on the rowid in the database.
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.
Had an idea when writing this, made a hashtable with rowid->pos when populating the spinner and then used that. Might help someone if they're searching.
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);
}
}
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;
}
}
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
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
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. :)
精彩评论