gpt4 book ai didi

android - 如何获取微调器选择的值并在 sqlite 中存储为字符串?

转载 作者:行者123 更新时间:2023-11-29 21:41:47 26 4
gpt4 key购买 nike

我想获取微调器选择的值并将其作为字符串返回以存储在 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

并确保在 Activity实现 OnItemSelectedListener 并在 Spinner 上设置监听器

sp.setOnItemSelectedSpinner(this);

注意

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

关于android - 如何获取微调器选择的值并在 sqlite 中存储为字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16874653/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com