我创建了是当一个人使用数组适配器增加的设备与装置的名称自动更新的好手。 我创建了,所以当在所说的纺丝器的名称中选择一个旋转器的OnItemSelected方法,会出现一个新的窗口。 然而,当活动开始,这样用户就没有机会真正做出选择,直到出现新的窗口OnItemSelected自动选择列表中的第一项。
下面是代码:
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
startActivity(new Intent("com.lukeorpin.theappliancekeeper.APPLIANCESELECTED"));
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
有谁知道,其中,列表上的第一个项目不会被自动选择的方法吗?
下面是微调的其余部分的代码:
ArrayAdapter<String> appliancenameadapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, ApplianceNames); //Sets up an array adapter containing the values of the ApplianceNames string array
applianceName = (Spinner) findViewById(R.id.spinner_name); //Gives the spinner in the xml layout a variable name
applianceName.setAdapter(appliancenameadapter); //Adds the contents of the array adapter into the spinner
applianceName.setOnItemSelectedListener(this);
有谁知道,其中,列表上的第一个项目不会被自动选择的方法吗?
总有一个选择Spinner
,你不能改变的。
恕我直言,你不应该使用Spinner
触发启动的活动。
话虽这么说,你可以用一个boolean
来跟踪这是否是第一次评选活动,而忽略它,如果它是。
如果你正试图避免你的听众的初始呼叫onItemSelected()
方法,另一种选择是使用post()
把视图的消息队列的优势。 在第一时间为你的听众微调检查就还没有加入。
// Set initial selection
spinner.setSelection(position);
// Post to avoid initial invocation
spinner.post(new Runnable() {
@Override public void run() {
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// Only called when the user changes the selection
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
});
它的工作对我来说,
private boolean isSpinnerInitial = true;
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
if(isSpinnerInitial)
{
isSpinnerInitial = false;
}
else {
// do your work...
}
}
声明变量isSpinnerInitial然后做出做出选择作为默认选择
spinnertaggeview.setSelection(-1); 不作选择为-1或未被选择的一切正如我们在.NET或其他语言做。 所以,你可以忽略thatline。
testStringArrayListinside.add("Make a Selection");
ADD this line so that this is selected by default and user never selects it
testStringArrayList = (ArrayList<String>) ClinqLinX.get("Tag");
boolean added = false;
testStringArrayListinside.add("Make a Selection");
for (String s : testStringArrayList) {
if (s != null || s != "") {
String[] results = s.split(","); // split on commas
for (String string : results) {
testStringArrayListinside.add(string);
Toast.makeText(getContext(), string, Toast.LENGTH_SHORT).show();
added = true;
}
}
}
if (added == false) {
testStringArrayListinside.add("Not tagged details found");
}
spinnertaggeview.refreshDrawableState();
}
// Adapter: You need three parameters 'the context, id of the layout (it will be where the data is shown),
// and the array that contains the data
if (testStringArrayListinside.size() > 0) {
adapter = new ArrayAdapter<String>(this.getContext(),android.R.layout.select_dialog_singlechoice, testStringArrayListinside);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Here, you set the data in your ListView
spinnertaggeview.setAdapter(adapter);
isSpinnerInitial = true;
spinnertaggeview.setSelection(-1);
spinnertaggeview.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
if (isSpinnerInitial){
isSpinnerInitial = false;
return;}
else{
TextView tv = (TextView) arg1;
String spinner_value = tv.getText().toString();
if (spinner_value.length() == 0) {
spinner_value = "Nothing";
}
String strusername = spinner_value;//As you are using Default String Adapter
Toast.makeText(getContext(), strusername, Toast.LENGTH_SHORT).show();
Intent intent = new Intent(spinnertaggeview.getContext(), Userdetails.class);
intent.putExtra("Username", strusername);
spinnertaggeview.getContext().startActivity(intent);}
}