机器人如何让选择的项目从数据驱动微调 [英] Android how to get selected item from data driven spinner
问题描述
新手问题。我使用的是SimleCursorAdapter来填充,从一个SQLite表中的微调,如在Android开发文档:
Newbie question. I'm using a SimleCursorAdapter to populate a spinner from an SQLite table, as shown in the Android dev docs:
Spinner list=(Spinner)findViewById(R.id.cboModel);
SimpleCursorAdapter ModelAdapter = new SimpleCursorAdapter(this,
android.R.layout.simple_spinner_item, model,
new String[] {"Drug"},
new int[] {android.R.id.text1});
ModelAdapter.setDropDownViewResource(
android.R.layout.simple_spinner_dropdown_item);
list.setAdapter(ModelAdapter);
list.setOnItemSelectedListener(onModelSelect);
我已经建立了一个侦听器,但我无法弄清楚如何获得所选项目的文本,它拉起来SQLiteCursor,而不是在微调的实际文本。
I've set up a listener, but I can't figure out how to get the selected item text, it pulls up the SQLiteCursor, not the actual text in the spinner.
private AdapterView.OnItemSelectedListener
onModelSelect= new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?>
parent, View view, int position, long id) {
ModelName = parent.getSelectedItem().toString();
android.util.Log.w("OnItemSelect.cboModel", ModelName);
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
};
谷歌轮番上涨的问题在几个留言板,但没有答案,所以这似乎是一个常见的新手问题。这可能是非常明显一些,但如果你可以点我在正确的方向我想AP preciate它。谢谢你。
Google turns up the question on several message boards, but no answers, so it appears to be a common newbie question. It may be painfully obvious to some, but if you could point me in the right direction I would appreciate it. Thank you.
推荐答案
由于所选择的项目是一个光标,你可以很容易地通过调用的getString与列在你用来填充原始数据库的查询索引获得的价值的微调。
Since the selected item is a Cursor, you can easily get the value by calling getString with the index of the column in the original database query that you used to populate the Spinner.
String spinnerString = null;
Cursor cc = (Cursor)(yourSpinner.getSelectedItem());
if (cc != null) {
spinnerString = cc.getString(
cc.getColumnIndex("Drug"));
}
在微调从数据库填充这个技术肯定能行。我还没有尝试过与资源阵列。
This technique definitely works when the Spinner is populated from the database. I have not tried it with a resource array.
这篇关于机器人如何让选择的项目从数据驱动微调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!