如何获取微调控件的选定值并以字符串的形式存储在SQLite中? [英] How to get spinner selected value and store as string in sqlite?

查看:23
本文介绍了如何获取微调控件的选定值并以字符串的形式存储在SQLite中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望获取微调控制项选定的值,并将其作为字符串返回以存储在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);

            }
        });

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

推荐答案

使用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
}
}

Retrieving Spinner values

并确保在Activityimplements OnItemSelectedListener并在Spinner

上设置侦听器
sp.setOnItemSelectedSpinner(this);

注意:

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

这篇关于如何获取微调控件的选定值并以字符串的形式存储在SQLite中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆