1

我想获取微调器选择的值并将其作为字符串返回以存储在 sqlite 数据库中,并在需要时读取数据。

我尝试如下方法,

sp = (Spinner) findViewById(R.id.spnCategory);      
ArrayAdapter<CharSequence> ar = ArrayAdapter.createFromResource(this,
                R.array.category, android.R.layout.simple_list_item_1);
ar.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
sp.setAdapter(ar);
String selection = sp.getSelectedItem().toString();

并获取字符串附加信息,如下所示:

selection = extras.getString("category");

附加如下:

v.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                Intent intent=new Intent(context,ViewItem.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);               

                intent.putExtra("category", category);

            }
        });

但是我无法获得选择的微调器值,我只得到微调器中的第一个值,我可以知道我的编码有什么问题吗?

4

1 回答 1

2

采用onItemSelected

 public void onItemSelected(AdapterView<?> parent, View view, 
        int pos, long id) {
    // An item was selected. You can retrieve the selected item using
    // parent.getItemAtPosition(pos)

    TextView tv = (TextView)view;
    String selection = tv.getText().toString();   // or you can use the position but I do this to do other things with the TextView 
}

public void onNothingSelected(AdapterView<?> parent) {
    // Another interface callback
}
}

检索微调器值

并确保你implements OnItemSelectedListener在你的Activity并将监听器设置在你的Spinner

sp.setOnItemSelectedSpinner(this);

笔记

您可能希望声明selection为成员变量,以便您可以在其他地方使用它并在此处分配它

于 2013-06-01T16:17:32.140 回答