Android Spinner:在初始化期间避免 onItemSelected 调用 [英] Android Spinner : Avoid onItemSelected calls during initialization

查看:38
本文介绍了Android Spinner:在初始化期间避免 onItemSelected 调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个带有 SpinnerTextView 的 Android 应用程序.我想在 TextView 的 Spinner 下拉列表中显示所选项目.我在 onCreate 方法中实现了 Spinner,所以当我运行程序时,它会在 TextView 中显示一个值(在从下拉列表中选择一个项目之前).

我只想在从下拉列表中选择一个项目后才在 TextView 中显示值.我该怎么做?

这是我的代码:

import android.app.Activity;导入 android.os.Bundle;导入 android.view.View;导入 android.widget.AdapterView;导入 android.widget.AdapterView.OnItemSelectedListener;导入 android.widget.ArrayAdapter;导入 android.widget.Spinner;导入 android.widget.TextView;公共类 GPACal01Activity 扩展 Activity 实现 OnItemSelectedListener {/** 在第一次创建活动时调用.*/@覆盖public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);Spinner spinner = (Spinner) findViewById(R.id.noOfSubjects);//使用字符串数组和默认微调器布局创建一个 ArrayAdapterArrayAdapter适配器 = ArrayAdapter.createFromResource(this,R.array.noofsubjects_array, android.R.layout.simple_spinner_item);//指定出现选项列表时要使用的布局adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);//将适配器应用到微调器spinner.setAdapter(适配器);spinner.setOnItemSelectedListener(this);}public void onItemSelected(AdapterView parent, View arg1, int pos,long id) {TextView textView = (TextView) findViewById(R.id.textView1);String str = (String) parent.getItemAtPosition(pos);textView.setText(str);}public void onNothingSelected(AdapterViewarg0) {//TODO 自动生成的方法存根}}

解决方案

spinner.setOnItemSelectedListener(this);//将调用 onItemSelected() 监听器.

所以第一次用任何整数值处理这个

示例:最初取 int check = 0;

public void onItemSelected(AdapterView parent, View arg1, int pos,long id) {如果(++检查> 1){TextView textView = (TextView) findViewById(R.id.textView1);String str = (String) parent.getItemAtPosition(pos);textView.setText(str);}}

您可以使用布尔值以及检查当前和以前的位置来实现.见这里>

I created an Android application with a Spinner and a TextView. I want to display the selected item from the Spinner's drop down list in the TextView. I implemented the Spinner in the onCreate method so when I'm running the program, it shows a value in the TextView (before selecting an item from the drop down list).

I want to show the value in the TextView only after selecting an item from the drop down list. How do I do this?

Here is my code:

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

public class GPACal01Activity extends Activity implements OnItemSelectedListener {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Spinner spinner = (Spinner) findViewById(R.id.noOfSubjects);

        // Create an ArrayAdapter using the string array and a default spinner layout
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,R.array.noofsubjects_array, android.R.layout.simple_spinner_item);
        // Specify the layout to use when the list of choices appears
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        // Apply the adapter to the spinner
        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(this);
    }

    public void onItemSelected(AdapterView<?> parent, View arg1, int pos,long id) {
        TextView textView = (TextView) findViewById(R.id.textView1);
        String str = (String) parent.getItemAtPosition(pos);
        textView.setText(str);
    }

    public void onNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub

    }
}

解决方案

spinner.setOnItemSelectedListener(this); // Will call onItemSelected() Listener.

So first time handle this with any Integer value

Example: Initially Take int check = 0;

public void onItemSelected(AdapterView<?> parent, View arg1, int pos,long id) {
   if(++check > 1) {
      TextView textView = (TextView) findViewById(R.id.textView1);
      String str = (String) parent.getItemAtPosition(pos);
      textView.setText(str);
   }
}

You can do it with boolean value and also by checking current and previous positions. See here

这篇关于Android Spinner:在初始化期间避免 onItemSelected 调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆