这给了我一个我可以使用的字符串值:
Cursor vTypeChose = (Cursor)(vTypeSpinner.getSelectedItem());
if (vTypeChose != null) {
typePicked = vTypeChose.getString(
vTypeChose.getColumnIndex(DataBaseHelper.POWERSPORTS_TYPE));
}
这给了我null:
vTypeSpinner.setOnItemSelectedListener(new OnItemSelectedListener(){
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int
pos, long id) {
if(pos!=0){
Cursor vTypeChose = (Cursor)(vTypeSpinner.getSelectedItem());
if (vTypeChose != null) {
typePicked = vTypeChose.getString(
vTypeChose.getColumnIndex(DataBaseHelper.POWERSPORTS_TYPE));
}
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
在OnItemSelectedListener中使用Log.e和Toast以及typePicked变量向我显示它正在触发,只是没有传递给我正确的值。
如果没有OnItemSelectedListener,它会使用相关数据填充第二个微调器。添加OnItemSelectedListener后,第二个微调器将填充任何内容,因为字符串值为null。
(编辑)添加了代码:
public class PowersportsEquivalent extends Activity {
DataBaseHelper myDbHelper;
String typePicked = null;
String makePicked = null;
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_powersports_equivalent);
myDbHelper = new DataBaseHelper(this);
try {
myDbHelper.createDataBase();
} catch (IOException ioe) {
// TODO Auto-generated catch block
throw new Error("Unable to create database");
}
try {
myDbHelper.openDataBase();
}catch(SQLException sqle){
throw sqle;
}
final Cursor vType;
final Cursor vMake;
final Spinner vTypeSpinner;
final Spinner vMakeSpinner;
//POWERSPORTS TYPE Cursor
vType = (Cursor) DataBaseHelper.getPowersportsType();
startManagingCursor(vType);
SimpleCursorAdapter scaType = new SimpleCursorAdapter(this,
android.R.layout.simple_spinner_item,
vType,
new String [] {DataBaseHelper.POWERSPORTS_TYPE},
new int[] {android.R.id.text1});
scaType.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
vTypeSpinner = (Spinner) findViewById(R.id.typeSpinner);
vTypeSpinner.setAdapter(scaType);
//POWERSPORTS MAKE Cursor
vTypeSpinner.setOnItemSelectedListener(new OnItemSelectedListener(){
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int
pos, long id) {
if(pos!=0){
Cursor vTypeChose = (Cursor)(vTypeSpinner.getSelectedItem());
if (vTypeChose != null) {
typePicked = vTypeChose.getString(
vTypeChose.getColumnIndex(DataBaseHelper.POWERSPORTS_TYPE));
}
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
vMake = (Cursor) DataBaseHelper.getPowersportsMake(typePicked);
startManagingCursor(vMake);
SimpleCursorAdapter scaMake = new SimpleCursorAdapter(this,
android.R.layout.simple_spinner_item,
vMake,
new String [] {DataBaseHelper.POWERSPORTS_MAKE},
new int[]{android.R.id.text1});
scaMake.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
vMakeSpinner = (Spinner) findViewById(R.id.makeSpinner);
vMakeSpinner.setAdapter(scaMake);
答案 0 :(得分:0)
也许尝试在pos
参数中使用onItemSelected
,然后通过适配器获取项目,如下所示:
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int
pos, long id) {
if(pos!=0){
Cursor vTypeChose = (Cursor) scaType.getItem(pos);
if (vTypeChose != null) {
typePicked = vTypeChose.getString(
vTypeChose.getColumnIndex(DataBaseHelper.POWERSPORTS_TYPE));
}
}
}
答案 1 :(得分:0)
为了使用spin1选择相应地更新spin2,你必须在onItemSelected重新加载spin2数据。试试这个:
vTypeSpinner.setOnItemSelectedListener(new OnItemSelectedListener(){
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int
pos, long id) {
TextView picked = (TextView)selectedItemView;
if(picked != null){
typePicked = picked.getText().toString();
vMake = (Cursor) DataBaseHelper.getPowersportsMake(typePicked);
scaMake.changeCursor(vMake);
}
}
}
也改变。 (此代码在onItemSelected
之前运行,这就是为什么typePicked
为空)
vMake = (Cursor) DataBaseHelper.getPowersportsMake(typePicked);
startManagingCursor(vMake);
SimpleCursorAdapter scaMake = new SimpleCursorAdapter(this,
android.R.layout.simple_spinner_item,
vMake,
new String [] {DataBaseHelper.POWERSPORTS_MAKE},
new int[]{android.R.id.text1});
到
//vMake = (Cursor) DataBaseHelper.getPowersportsMake(typePicked);
//startManagingCursor(vMake);
SimpleCursorAdapter scaMake = new SimpleCursorAdapter(this,
android.R.layout.simple_spinner_item,
null,
new String [] {DataBaseHelper.POWERSPORTS_MAKE},
new int[]{android.R.id.text1});
我认为在完成这些更改后,您可以使用原始代码。
另一种可能的方法是使用传递给此方法的id并直接从数据库
获取值答案 2 :(得分:0)
您的代码已编辑:
public class PowersportsEquivalent extends Activity {
DataBaseHelper myDbHelper;
String typePicked = null;
String makePicked = null;
final SimpleCursorAdapter scaMake;
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_powersports_equivalent);
myDbHelper = new DataBaseHelper(this);
try {
myDbHelper.createDataBase();
} catch (IOException ioe) {
// TODO Auto-generated catch block
throw new Error("Unable to create database");
}
try {
myDbHelper.openDataBase();
}catch(SQLException sqle){
throw sqle;
}
final Cursor vType;
final Cursor vMake;
final Spinner vTypeSpinner;
final Spinner vMakeSpinner;
//POWERSPORTS TYPE Cursor
vType = (Cursor) DataBaseHelper.getPowersportsType();
startManagingCursor(vType);
SimpleCursorAdapter scaType = new SimpleCursorAdapter(this,
android.R.layout.simple_spinner_item,
vType,
new String [] {DataBaseHelper.POWERSPORTS_TYPE},
new int[] {android.R.id.text1});
scaType.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
vTypeSpinner = (Spinner) findViewById(R.id.typeSpinner);
vTypeSpinner.setAdapter(scaType);
//POWERSPORTS MAKE Cursor
vTypeSpinner.setOnItemSelectedListener(new OnItemSelectedListener(){
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int pos, long id) {
if(pos!=0){
TextView picked = (TextView)selectedItemView;
if(picked != null){
typePicked = picked.getText().toString();
vMake = (Cursor) DataBaseHelper.getPowersportsMake(typePicked);
scaMake.changeCursor(vMake);
}
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
//vMake = (Cursor) DataBaseHelper.getPowersportsMake(typePicked);
//startManagingCursor(vMake);
scaMake = new SimpleCursorAdapter(this,
android.R.layout.simple_spinner_item,
null,
new String [] {DataBaseHelper.POWERSPORTS_MAKE},
new int[]{android.R.id.text1});
scaMake.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
vMakeSpinner = (Spinner) findViewById(R.id.makeSpinner);
vMakeSpinner.setAdapter(scaMake);