如何在微调器的第一个位置设置默认值?
How to Set Default value in spinner at first position?
下面是我的代码,我从中通过 sqlite 在微调器中填充状态,它运行完美。现在我想要在第一个位置添加硬编码的 "Select State" 。我怎样才能做到这一点?
try {
ArrayList<String> state_array = new ArrayList<String>();
Cursor cursor = db.rawQuery("SELECT nSerialNo as _id,cCodeName FROM CodeMaster where nCtgId = 6", null);
if (cursor.moveToFirst()) {
do {
//assing values
String stateID = cursor.getString(0);
String stateName = cursor.getString(1);
stateData = stateName;
state_array.add(stateData);
} while (cursor.moveToNext());
}
ArrayAdapter my_Adapter = new ArrayAdapter(getActivity(), android.R.layout.simple_list_item_1, state_array);
spnState.setAdapter(my_Adapter);
cursor.close();
spnState.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
state = spnState.getSelectedItem().toString();
Cursor cursor = db.rawQuery("SELECT nSerialNo FROM CodeMaster where cCodeName = '" + state + "'", null);
if (cursor.moveToFirst()) {
do {
//assing values
stateCodeId = cursor.getString(0);
} while (cursor.moveToNext());
}
cursor.close();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
} catch (Exception e) {
e.printStackTrace();
}
试试这个代码。
try {
ArrayList<String> state_array = new ArrayList<String>();
state_array.add("Select State");
Cursor cursor = db.rawQuery("SELECT nSerialNo as _id,cCodeName FROM CodeMaster where nCtgId = 6", null);
if (cursor.moveToFirst()) {
do {
//assing values
String stateID = cursor.getString(0);
String stateName = cursor.getString(1);
stateData = stateName;
state_array.add(stateData);
} while (cursor.moveToNext());
}
state_array.add(0, "Select State");
ArrayAdapter my_Adapter = new ArrayAdapter(getActivity(), android.R.layout.simple_list_item_1, state_array);
spnState.setAdapter(my_Adapter);
cursor.close();
下面是我的代码,我从中通过 sqlite 在微调器中填充状态,它运行完美。现在我想要在第一个位置添加硬编码的 "Select State" 。我怎样才能做到这一点?
try {
ArrayList<String> state_array = new ArrayList<String>();
Cursor cursor = db.rawQuery("SELECT nSerialNo as _id,cCodeName FROM CodeMaster where nCtgId = 6", null);
if (cursor.moveToFirst()) {
do {
//assing values
String stateID = cursor.getString(0);
String stateName = cursor.getString(1);
stateData = stateName;
state_array.add(stateData);
} while (cursor.moveToNext());
}
ArrayAdapter my_Adapter = new ArrayAdapter(getActivity(), android.R.layout.simple_list_item_1, state_array);
spnState.setAdapter(my_Adapter);
cursor.close();
spnState.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
state = spnState.getSelectedItem().toString();
Cursor cursor = db.rawQuery("SELECT nSerialNo FROM CodeMaster where cCodeName = '" + state + "'", null);
if (cursor.moveToFirst()) {
do {
//assing values
stateCodeId = cursor.getString(0);
} while (cursor.moveToNext());
}
cursor.close();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
} catch (Exception e) {
e.printStackTrace();
}
试试这个代码。
try {
ArrayList<String> state_array = new ArrayList<String>();
state_array.add("Select State");
Cursor cursor = db.rawQuery("SELECT nSerialNo as _id,cCodeName FROM CodeMaster where nCtgId = 6", null);
if (cursor.moveToFirst()) {
do {
//assing values
String stateID = cursor.getString(0);
String stateName = cursor.getString(1);
stateData = stateName;
state_array.add(stateData);
} while (cursor.moveToNext());
}
state_array.add(0, "Select State"); ArrayAdapter my_Adapter = new ArrayAdapter(getActivity(), android.R.layout.simple_list_item_1, state_array); spnState.setAdapter(my_Adapter); cursor.close();