以编程方式设置选定的微调控制项

时间:2023-01-19 08:58:13

I am working on an android project and I am using a spinner which uses an array adapter which is populated from the database.

我正在开发一个android项目,我正在使用一个spinner,它使用一个从数据库中填充的数组适配器。

I can't find out how I can set the selected item programmatically from the list. For example if, in the spinner I have the following items:

我不知道如何从列表中以编程方式设置所选项。例如,如果,在旋转器中我有以下项目:

  • Category 1
  • 类别1
  • Category 2
  • 第二类
  • Category 3
  • 3级

How would I programmatically make Category 2 the selected item when the screen is created. I was thinking it might be similar to c# I.E Spinner.SelectedText = "Category 2" but there doesn't seem to be any method similar to this for Android.

在创建屏幕时,如何以编程方式将第2类设置为所选项。我想它可能和c# I类似。E转轮。SelectedText = "第2类",但Android似乎没有类似的方法。

16 个解决方案

#1


571  

Use the following: spinnerObject.setSelection(INDEX_OF_CATEGORY2).

使用以下:spinnerObject.setSelection(INDEX_OF_CATEGORY2)。

#2


61  

No one of these answers gave me the solution, only worked with this:

这些答案中没有一个给了我答案,只有这个:

mySpinner.post(new Runnable() {
    @Override
    public void run() {
        mySpinner.setSelection(position);
    }
});

#3


27  

public static void selectSpinnerItemByValue(Spinner spnr, long value) {
    SimpleCursorAdapter adapter = (SimpleCursorAdapter) spnr.getAdapter();
    for (int position = 0; position < adapter.getCount(); position++) {
        if(adapter.getItemId(position) == value) {
            spnr.setSelection(position);
            return;
        }
    }
}

You can use the above like:

你可以使用上面的例子:

selectSpinnerItemByValue(spinnerObject, desiredValue);

& of course you can also select by index directly like

当然你也可以直接按索引来选择

spinnerObject.setSelection(index);

#4


24  

Some explanation (at least for Fragments - never tried with pure Activity). Hope it helps someone to understand Android better.

一些解释(至少是对片段的解释——从未尝试过纯活动)。希望它能帮助人们更好地理解Android。

Most popular answer by Arun George is correct but don't work in some cases.
The answer by Marco HC uses Runnable wich is a last resort due to additional CPU load.

Arun George最常用的答案是正确的,但在某些情况下却行不通。Marco HC的答案是Runnable wich由于额外的CPU负载是最后的选择。

The answer is - you should simply choose correct place to call to setSelection(), for example it works for me:

答案是-您应该简单地选择要调用setSelection()的正确位置,例如它对我来说是有效的:

@Override
public void onResume() {
    super.onResume();

    yourSpinner.setSelection(pos);
 }

But it won't work in onCreateView(). I suspect that is the reason for the interest to this topic.

但是它不能在onCreateView()中工作。我想这就是人们对这个话题感兴趣的原因。

The secret is that with Android you can't do anything you want in any method - oops:( - components may just not be ready. As another example - you can't scroll ScrollView neither in onCreateView() nor in onResume() (see the answer here)

秘密在于,使用Android,你无法在任何方法中做任何你想做的事情——哎呦:(组件可能还没有准备好。作为另一个例子,您不能在onCreateView()或onResume()中滚动ScrollView(参见此处的答案)

#5


10  

Why don't you use your values from the DB and store them on an ArrayList and then just use:

为什么不使用DB中的值并将它们存储在ArrayList中,然后使用:

yourSpinner.setSelection(yourArrayList.indexOf("Category 1"));

#6


9  

To find a value and select it:

找到一个值并选择它:

private void selectValue(Spinner spinner, Object value) {
    for (int i = 0; i < spinner.getCount(); i++) {
        if (spinner.getItemAtPosition(i).equals(value)) {
            spinner.setSelection(i);
            break;
        }
    }
}

#7


6  

You can make a generic method for this kind of work as I do in my UtilityClass which is

你可以为这类工作创建一个通用方法就像我在我的实用程序类中做的那样

public void SetSpinnerSelection(Spinner spinner,String[] array,String text) {
    for(int i=0;i<array.length;i++) {
        if(array[i].equals(text)) {
            spinner.setSelection(i);
        }
    }
}

#8


3  

I have a SimpleCursorAdapter so I have to duplicate the data for use the respose in this post. So, I recommend you try this way:

我有一个SimpleCursorAdapter,因此我必须复制数据,以使用在这篇文章的重生。所以,我建议你这样尝试:

for (int i = 0; i < spinnerRegion.getAdapter().getCount(); i++) {
    if (spinnerRegion.getItemIdAtPosition(i) == Integer
        .valueOf(signal.getInt(signal
            .getColumnIndexOrThrow("id_region")))) {
        spinnerRegion.setSelection(i);
        break;
    }
}

I think that is a real way

我认为这是真的

#9


2  

This is work for me.

这是我的工作。

 spinner.setSelection(spinner_adapter.getPosition(selected_value)+1);

#10


2  

You can easily set like this: spinner.setSelection(1), instead of 1, you can set any position of list you would like to show.

您可以轻松设置为:spinner.setSelection(1),而不是1,您可以设置想要显示的列表的任何位置。

#11


2  

the optimal solution is:

最优的解决方案是:

    public String[] items= new String[]{"item1","item2","item3"};
    // here you can use array or list 
    ArrayAdapter adapter= new ArrayAdapter(Your_Context, R.layout.support_simple_spinner_dropdown_item, items);
    final Spinner itemsSpinner= (Spinner) findViewById(R.id.itemSpinner);
itemsSpinner.setAdapter(adapter);

to get the position of the item automatically add the following statement

要获取项的位置,将自动添加以下语句

itemsSpinner.setSelection(itemsSpinner.getPosition("item2"));

#12


1  

If you have a list of contacts the you can go for this:

如果你有一个联系人列表,你可以这样做:

((Spinner) view.findViewById(R.id.mobile)).setSelection(spinnerContactPersonDesignationAdapter.getPosition(schoolContact.get(i).getCONT_DESIGNATION()));

#13


1  

I know that is already answered, but simple code to select one item, very simple:

我知道那已经回答了,但简单的代码选择一项,非常简单:

spGenre.setSelection( ( (ArrayAdapter) spGenre.getAdapter()).getPosition(client.getGenre()) );

#14


1  

  for (int x = 0; x < spRaca.getAdapter().getCount(); x++) {
            if (spRaca.getItemIdAtPosition(x) == reprodutor.getId()) {
                spRaca.setSelection(x);
                break;
            }
        }

#15


1  

In my case, this code saved my day:

在我的案例中,这段代码挽救了我的一天:

public static void selectSpinnerItemByValue(Spinner spnr, long value) {
    SpinnerAdapter adapter = spnr.getAdapter();
    for (int position = 0; position < adapter.getCount(); position++) {
        if(adapter.getItemId(position) == value) {
            spnr.setSelection(position);
            return;
        }
    }
}

#16


-1  

This worked for me:

这工作对我来说:

@Override
protected void onStart() {
    super.onStart();
    mySpinner.setSelection(position);
}

It's similar to @sberezin's solution but calling setSelection() in onStart().

它类似于@sberezin的解决方案,但在onStart()中调用setSelection()。

#1


571  

Use the following: spinnerObject.setSelection(INDEX_OF_CATEGORY2).

使用以下:spinnerObject.setSelection(INDEX_OF_CATEGORY2)。

#2


61  

No one of these answers gave me the solution, only worked with this:

这些答案中没有一个给了我答案,只有这个:

mySpinner.post(new Runnable() {
    @Override
    public void run() {
        mySpinner.setSelection(position);
    }
});

#3


27  

public static void selectSpinnerItemByValue(Spinner spnr, long value) {
    SimpleCursorAdapter adapter = (SimpleCursorAdapter) spnr.getAdapter();
    for (int position = 0; position < adapter.getCount(); position++) {
        if(adapter.getItemId(position) == value) {
            spnr.setSelection(position);
            return;
        }
    }
}

You can use the above like:

你可以使用上面的例子:

selectSpinnerItemByValue(spinnerObject, desiredValue);

& of course you can also select by index directly like

当然你也可以直接按索引来选择

spinnerObject.setSelection(index);

#4


24  

Some explanation (at least for Fragments - never tried with pure Activity). Hope it helps someone to understand Android better.

一些解释(至少是对片段的解释——从未尝试过纯活动)。希望它能帮助人们更好地理解Android。

Most popular answer by Arun George is correct but don't work in some cases.
The answer by Marco HC uses Runnable wich is a last resort due to additional CPU load.

Arun George最常用的答案是正确的,但在某些情况下却行不通。Marco HC的答案是Runnable wich由于额外的CPU负载是最后的选择。

The answer is - you should simply choose correct place to call to setSelection(), for example it works for me:

答案是-您应该简单地选择要调用setSelection()的正确位置,例如它对我来说是有效的:

@Override
public void onResume() {
    super.onResume();

    yourSpinner.setSelection(pos);
 }

But it won't work in onCreateView(). I suspect that is the reason for the interest to this topic.

但是它不能在onCreateView()中工作。我想这就是人们对这个话题感兴趣的原因。

The secret is that with Android you can't do anything you want in any method - oops:( - components may just not be ready. As another example - you can't scroll ScrollView neither in onCreateView() nor in onResume() (see the answer here)

秘密在于,使用Android,你无法在任何方法中做任何你想做的事情——哎呦:(组件可能还没有准备好。作为另一个例子,您不能在onCreateView()或onResume()中滚动ScrollView(参见此处的答案)

#5


10  

Why don't you use your values from the DB and store them on an ArrayList and then just use:

为什么不使用DB中的值并将它们存储在ArrayList中,然后使用:

yourSpinner.setSelection(yourArrayList.indexOf("Category 1"));

#6


9  

To find a value and select it:

找到一个值并选择它:

private void selectValue(Spinner spinner, Object value) {
    for (int i = 0; i < spinner.getCount(); i++) {
        if (spinner.getItemAtPosition(i).equals(value)) {
            spinner.setSelection(i);
            break;
        }
    }
}

#7


6  

You can make a generic method for this kind of work as I do in my UtilityClass which is

你可以为这类工作创建一个通用方法就像我在我的实用程序类中做的那样

public void SetSpinnerSelection(Spinner spinner,String[] array,String text) {
    for(int i=0;i<array.length;i++) {
        if(array[i].equals(text)) {
            spinner.setSelection(i);
        }
    }
}

#8


3  

I have a SimpleCursorAdapter so I have to duplicate the data for use the respose in this post. So, I recommend you try this way:

我有一个SimpleCursorAdapter,因此我必须复制数据,以使用在这篇文章的重生。所以,我建议你这样尝试:

for (int i = 0; i < spinnerRegion.getAdapter().getCount(); i++) {
    if (spinnerRegion.getItemIdAtPosition(i) == Integer
        .valueOf(signal.getInt(signal
            .getColumnIndexOrThrow("id_region")))) {
        spinnerRegion.setSelection(i);
        break;
    }
}

I think that is a real way

我认为这是真的

#9


2  

This is work for me.

这是我的工作。

 spinner.setSelection(spinner_adapter.getPosition(selected_value)+1);

#10


2  

You can easily set like this: spinner.setSelection(1), instead of 1, you can set any position of list you would like to show.

您可以轻松设置为:spinner.setSelection(1),而不是1,您可以设置想要显示的列表的任何位置。

#11


2  

the optimal solution is:

最优的解决方案是:

    public String[] items= new String[]{"item1","item2","item3"};
    // here you can use array or list 
    ArrayAdapter adapter= new ArrayAdapter(Your_Context, R.layout.support_simple_spinner_dropdown_item, items);
    final Spinner itemsSpinner= (Spinner) findViewById(R.id.itemSpinner);
itemsSpinner.setAdapter(adapter);

to get the position of the item automatically add the following statement

要获取项的位置,将自动添加以下语句

itemsSpinner.setSelection(itemsSpinner.getPosition("item2"));

#12


1  

If you have a list of contacts the you can go for this:

如果你有一个联系人列表,你可以这样做:

((Spinner) view.findViewById(R.id.mobile)).setSelection(spinnerContactPersonDesignationAdapter.getPosition(schoolContact.get(i).getCONT_DESIGNATION()));

#13


1  

I know that is already answered, but simple code to select one item, very simple:

我知道那已经回答了,但简单的代码选择一项,非常简单:

spGenre.setSelection( ( (ArrayAdapter) spGenre.getAdapter()).getPosition(client.getGenre()) );

#14


1  

  for (int x = 0; x < spRaca.getAdapter().getCount(); x++) {
            if (spRaca.getItemIdAtPosition(x) == reprodutor.getId()) {
                spRaca.setSelection(x);
                break;
            }
        }

#15


1  

In my case, this code saved my day:

在我的案例中,这段代码挽救了我的一天:

public static void selectSpinnerItemByValue(Spinner spnr, long value) {
    SpinnerAdapter adapter = spnr.getAdapter();
    for (int position = 0; position < adapter.getCount(); position++) {
        if(adapter.getItemId(position) == value) {
            spnr.setSelection(position);
            return;
        }
    }
}

#16


-1  

This worked for me:

这工作对我来说:

@Override
protected void onStart() {
    super.onStart();
    mySpinner.setSelection(position);
}

It's similar to @sberezin's solution but calling setSelection() in onStart().

它类似于@sberezin的解决方案,但在onStart()中调用setSelection()。