下面是我的代码,我从sqlite中在spinner中填充状态,它工作正常。现在我想要的是我想在第一个位置添加硬编码的“选择状态”。我怎样才能做到这一点?
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();
}
答案 0 :(得分:1)
试试这段代码。
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());
}
答案 1 :(得分:0)
state_array.add(0,“选择州”); ArrayAdapter my_Adapter = new ArrayAdapter(getActivity(),android.R.layout.simple_list_item_1,state_array); spnState.setAdapter(my_Adapter); cursor.close();