Android的微调:避免onItemSelected初始化过程中调用 [英] Android Spinner : Avoid onItemSelected calls during initialization

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

问题描述

我创建Android应用程序与微调的TextView 。我的任务是显示所选项目从Spinner的下拉列表中的TextView中。我在实施微调的的onCreate 方法,所以当我运行程序时,它显示了值的TextView (从下拉列表中选择一个项目之前)。

我只想从下拉列表中选择一个项目后,显示在TextView中的价值。如何做到这一点?

下面是我的code:

 进口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扩展活动实现OnItemSelectedListener {
    / **第一次创建活动时调用。 * /
    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);

        微调微调=(微调)findViewById(R.id.noOfSubjects);

        //创建一个使用字符串数组和默认微调布局ArrayAdapter
        ArrayAdapter< CharSequence的>适配器= ArrayAdapter.createFromResource(这一点,R.array.noofsubjects_array,android.R.layout.simple_spinner_item);
        //指定被使用的布局出现选项列表时,
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        //应用适配器微调
        spinner.setAdapter(适配器);
        spinner.setOnItemSelectedListener(本);
    }

    公共无效onItemSelected(适配器视图<>母公司,查看ARG1,诠释POS,长I​​D){
        TextView中的TextView =(TextView中)findViewById(R.id.textView1);
        字符串str =(字符串)parent.getItemAtPosition(POS);
        textView.setText(STR);
    }

    公共无效onNothingSelected(适配器视图<>为arg0){
        // TODO自动生成方法存根

    }
}
 

解决方案

  spinner.setOnItemSelectedListener(本);会叫'onItemSelected`监听器。
 

所以,第一次处理这种任意的整数值

例:  最初以 INT检查= 0;

 公共无效onItemSelected(适配器视图<>母公司,查看ARG1,诠释POS,长I​​D){
    检查=检查+ 1
   如果(检查→1)
   {
      TextView中的TextView =(TextView中)findViewById(R.id.textView1);
      字符串str =(字符串)parent.getItemAtPosition(POS);
      textView.setText(STR);
   }
}
 

您可以用布尔值,并通过检查当前和previous位置做到这一点。 <一href="http://stackoverflow.com/questions/2297601/how-to-disable-onitemselectedlistener-to-be-invoked-when-setting-selected-item-b">See这里

I created Android application with a Spinner and a TextView. My task is to display selected item from the Spinner's drop down list in the TextView. I implemented 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) {
    check=check+1
   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的微调:避免onItemSelected初始化过程中调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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