如何通过其值而不是位置来设置微调默认值? [英] How to set Spinner Default by its Value instead of Position?
问题描述
我在数据库中有1-50条记录.我正在使用游标获取这些数据,并使用简单游标适配器将这些值设置为Spinner.现在我需要的是设置一个值,比如说第39个值作为默认值.但是我不希望通过其位置来设置它的值.
I have 1-50 records in the database. I am fetching those data using cursor and set those values to Spinner using Simple Cursor Adapter. Now what i need is i want to set one value say 39th value as default. But not by its position i want to set by its value.
我知道如何根据其位置设置微调器默认值
I know how to set the spinner default by its position
spinner.setSelection(39)
会将微调器设置为该值.
will set the spinner to that value.
但是我对通过数据库中的值(文本)设置微调器默认值一无所知. 我知道数据库中的值.例如,书"是微调器中的值之一.我需要将微调框默认设置为书籍.
But i didn't have any idea about setting the spinner default by its value(text) in the database. I know the values in the database. For eg "books" is one of the value in the spinner. I need to set the spinner default as books.
有没有办法做到这一点?
Is there any possible way to do this?
推荐答案
最后,我通过使用以下方法解决了该问题,其中旋转器的位置可以通过其字符串获取
Finally, i solved the problem by using following way, in which the position of the spinner can be get by its string
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);
我希望它会对某些人有所帮助.
I hope it will help for some one..
这篇关于如何通过其值而不是位置来设置微调默认值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!