我创建了一个Android应用程序Spinner
和TextView
。 我的任务是显示来自Spinner的下拉在TextView的列表中选择项目。 我在实施微调onCreate
方法,所以当我运行程序时,它显示了一个值TextView
(从下拉列表中选择一个项目之前)。
我只想从下拉列表中选择一个项目后,以显示TextView的价值。 我该怎么做呢?
这里是我的代码:
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
}
}
Answer 1:
spinner.setOnItemSelectedListener(this); // Will call onItemSelected() Listener.
所以,第一次处理这种与任何整数值
例如:最初取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);
}
}
你可以用布尔值,并通过检查当前和以前的位置去做。 看这里
Answer 2:
只要把该行集之前OnItemSelectedListener
spinner.setSelection(0,false)
Answer 3:
与API级别3开始您可以在一个活动使用onUserInteraction()用布尔,以确定是否所述用户与所述设备进行交互。
http://developer.android.com/reference/android/app/Activity.html#onUserInteraction()
@Override
public void onUserInteraction() {
super.onUserInteraction();
userIsInteracting = true;
}
由于在活动现场,我有:
private boolean userIsInteracting;
最后,我的微调:
mSpinnerView.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View view, int position, long arg3) {
spinnerAdapter.setmPreviousSelectedIndex(position);
if (userIsInteracting) {
updateGUI();
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
当你来来去去,通过活动的布尔重置为false。 奇迹般有效。
Answer 4:
这为我工作
飞旋在Android的初始化是有问题的,有时上述问题通过这种模式来解决。
Spinner.setAdapter();
Spinner.setSelected(false); // must
Spinner.setSelection(0,true); //must
Spinner.setonItemSelectedListener(this);
设置适配器应该是第一部分和onItemSelectedListener(本)初始化微调时,将要在后。 通过我的OnItemSelected()上面的图案微调的初始化过程中不叫
Answer 5:
哈哈...我有同样的问题。 当initViews()只是做这样的一只顺序是关键,听众是最后一个。 祝好运 !
spinner.setAdapter(adapter);
spinner.setSelection(position);
spinner.setOnItemSelectedListener(listener);
Answer 6:
我的解决方案:
protected boolean inhibit_spinner = true;
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int pos, long arg3) {
if (inhibit_spinner) {
inhibit_spinner = false;
}else {
if (getDataTask != null) getDataTask.cancel(true);
updateData();
}
}
Answer 7:
您可以通过这种方式做到这一点:
AdapterView.OnItemSelectedListener listener = new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
//set the text of TextView
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
yourSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
yourSpinner.setOnItemSelectedListener(listener);
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
起初,我创建了一个监听器,并归结为一个变量回调; 然后我创建第二听者匿名,并且当这被称为在第一时间,这改变听众=]
Answer 8:
类似的简单解决方案,使多个纺纱是把适配器视图集合中 - 在活动超 - 对onItemSelected(...)的第一次执行再检查,看是否是适配器视图集合中执行它。 这使得一组在超类中的方法和支持多个AdapterViews和为此的多个纺纱。
超...
private Collection<AdapterView> AdapterViewCollection = new ArrayList<AdapterView>();
protected boolean firstTimeThrough(AdapterView parent) {
boolean firstTimeThrough = ! AdapterViewCollection.contains(parent);
if (firstTimeThrough) {
AdapterViewCollection.add(parent);
}
return firstTimeThrough;
}
子类...
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
if (! firstTimeThrough(parent)) {
String value = safeString(parent.getItemAtPosition(pos).toString());
String extraMessage = EXTRA_MESSAGE;
Intent sharedPreferencesDisplayIntent = new Intent(SharedPreferencesSelectionActivity.this,SharedPreferencesDisplayActivity.class);
sharedPreferencesDisplayIntent.putExtra(extraMessage,value);
startActivity(sharedPreferencesDisplayIntent);
}
// don't execute the above code if its the first time through
// do to onItemSelected being called during view initialization.
}
Answer 9:
有同样的问题,这对我的作品:
我有2个纺纱和我在init和期间与其他控件相互作用或从服务器获取数据后更新它们。
这里是我的模板:
public class MyClass extends <Activity/Fragment/Whatever> implements Spinner.OnItemSelectedListener {
private void removeSpinnersListeners() {
spn1.setOnItemSelectedListener(null);
spn2.setOnItemSelectedListener(null);
}
private void setSpinnersListeners() {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
spn1.setOnItemSelectedListener(MyClass.this);
spn2.setOnItemSelectedListener(MyClass.this);
}
}, 1);
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// Your code here
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
当类正在发起使用setSpinnersListeners(),而不是直接设置侦听器。
可运行将防止击发onItemSelected右后设置它们的值微调。
如果您需要更新的微调(服务器调用等)之后使用removeSpinnersListeners()您的更新之前行权,setSpinnersListeners()更新后的行权。 这将防止onItemSelected从更新后射击。
Answer 10:
您可以通过setOnTouchListener第一则setOnItemSelectedListener在onTouch实现它
@Override
public boolean onTouch(final View view, final MotionEvent event) {
view.setOnItemSelectedListener(this)
return false;
}
Answer 11:
用户交互标记随后可以在onTouch方法被设定为真,并在复位onItemSelected()
一旦选择变化已被处理。 我喜欢这个解决方案,因为用户交互标志的微调进行专门处理,而不是在活动的其他意见,可能会影响所需的行为。
在代码:
创建你的听众微调:
public class SpinnerInteractionListener implements AdapterView.OnItemSelectedListener, View.OnTouchListener {
boolean userSelect = false;
@Override
public boolean onTouch(View v, MotionEvent event) {
userSelect = true;
return false;
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
if (userSelect) {
userSelect = false;
// Your selection handling code here
}
}
}
添加监听到微调既是OnItemSelectedListener
和OnTouchListener
:
SpinnerInteractionListener listener = new SpinnerInteractionListener();
mSpinnerView.setOnTouchListener(listener);
mSpinnerView.setOnItemSelectedListener(listener);
Answer 12:
为了避免初始化过程中调用spinner.setOnItemSelectedListener()
spinner.setSelection(Adapter.NO_SELECTION, true); //Add this line before setting listener
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
Answer 13:
创建一个布尔字段
private boolean inispinner;
里面的OnCreate活动的
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (!inispinner) {
inispinner = true;
return;
}
//do your work here
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
文章来源: Android Spinner : Avoid onItemSelected calls during initialization