声明变量 isSpinnerInitial 然后将 Make a Selection 作为默认选择
spinnertaggeview.setSelection(-1); 不会像我们在 .Net 或其他语言中那样选择 -1 或未选择的所有内容。所以你可以忽略那条线。
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);}
}