- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个微调器,它使用以下代码填充,我想从微调器中获取用户选择的项目。最好的方法是什么?
List<Map<String, String>> tablelist = new ArrayList<>();
SQLiteDatabase db = openOrCreateDatabase("database", MODE_PRIVATE, null);
String query = "select * from table";
Cursor ps = db.rawQuery(query, null);
while (ps.moveToNext()){
Map<String, String> datanum = new HashMap<>();
datanum.put("Id", ps.getString(ps.getColumnIndex("Id")));
datanum.put("Some", ps.getString(ps.getColumnIndex("Some")));
tablelist.add(datanum);
}
db.close();
ps.close();
SimpleAdapter spinnerAdapter = new SimpleAdapter(this, tablelist, R.layout.row_spinner, new String[] {"Id", "Some"}, new int[] {android.R.id.text1, android.R.id.text1});
spinnerAdapter.notifyDataSetChanged();
spinnerAdapter.setDropDownViewResource(R.layout.row_spinner_list);
spinner.setAdapter(spinnerAdapter);
最佳答案
我将回答以下问题:“获取 setSelection() 方法所需项目位置的最佳方式”我希望这是您所要求的。
我将 SimpleAdapter 更改为 ArrayAdapter。
我更喜欢为您的 DTB 请求为每个结果创建一个对象,并将该对象映射到微调器中。因此您可以轻松访问每个结果。
像这样:
public class test {
private String TAG = "test";
private Activity curAct;
private Spinner spinner;
private SpnObj[] spinnerOptions;
public test(Activity curAct) {
this.curAct = curAct;
}
public void test() {
this.spinner = new Spinner(this.curAct);
this.spinnerOptions = this.getSpnContent();
final ArrayAdapter spinnerAdapter = new ArrayAdapter<>(this.curAct, android.R.layout.simple_spinner_item, spinnerOptions);
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
this.spinner.setAdapter(spinnerAdapter);
// setSelection with item id = 1
this.preSelectItem(1);
// setSelection with item some = 'some'
this.preSelectItem("some");
// get selectedItem
SpnObj selectedItem = this.getSelectedItem();
Log.d(TAG, "the current selected item id is : " + selectedItem.id);
Log.d(TAG, "the current selected item some is : " + selectedItem.some);
}
// this method should be used when you need to access on you're selectedItem
public SpnObj getSelectedItem() {
return (SpnObj) this.spinner.getSelectedItem();
}
// this method should be used for set the selection on a item with the id => maybe that was what you're asking for
public void preSelectItem(int id) {
boolean doWeStopTheLoop = false;
for (int i = 0; i < this.spinnerOptions.length && !doWeStopTheLoop; i++) {
if (((SpnObj) this.spinner.getItemAtPosition(i)).id == id) {
this.spinner.setSelection(i);
doWeStopTheLoop = true; //you can use break; too
}
}
}
// this method should be used for set the selection on a item with the id => maybe that was what you're asking for
// surcharge for 'some' column
public void preSelectItem(String some) {
boolean doWeStopTheLoop = false;
for (int i = 0; i < this.spinnerOptions.length && !doWeStopTheLoop; i++) {
if (((SpnObj) this.spinner.getItemAtPosition(i)).some.equals(some)) {
this.spinner.setSelection(i);
doWeStopTheLoop = true; //you can use break; too
}
}
}
private SpnObj[] getSpnContent() { // this method must be call in a thread
SQLiteDatabase db = this.curAct.openOrCreateDatabase("database", 0, null);
String query = "select * from table";
Cursor ps = db.rawQuery(query, null);
SpnObj[] spnContent = new SpnObj[ps.getColumnCount()];
if (ps.getColumnCount() > 0) {
int iterator = 0;
while (ps.moveToNext()) {
spnContent[iterator] = new SpnObj(
ps.getInt(ps.getColumnIndex("Id")),
ps.getString(ps.getColumnIndex("Some"))
);
iterator++;
}
} else {
// no rows : insert code here if you want manage this
}
db.close();
ps.close();
return spnContent;
}
}
// this object is only encapsulate the DTB result in a java class, so we can work easily with
class SpnObj {
public int id;
public String some;
public SpnObj(int id, String some) {
this.id = id;
this.some = some;
}
@Override
public String toString() { // important ! this method will be used for display the value in the dropdown list from the spinner
return this.some;
}
}
关于android - 如何使用 SimpleAdapter 从 Spinner 中获取选定的项目位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38111694/
所以我有一个微调器,我成功地更改了所选项目的颜色,但我无法更改下拉菜单中项目的颜色 这是我的 spinner_layout.xml 这是我的 样式.xml @style/Spin
我需要做的是,如果选择了微调器 1 中的某个项目,它需要在微调器 01 中显示某个数组例如如果微调器一个选定的项目是红色微调器 01 需要显示 level_array 作为微调器 01 的下拉选项,否
我想在点击微调器外部后关闭 Android 微调器。这可能吗? 最佳答案 我在这方面运气不错,即使它不能完全奏效。 Spinner 适配器的获取 View : public View getView(
我正在使用 eneter 框架来处理我的 android 应用程序中的通信;问题是当我试图填充微调器时,将适配器设置为微调器会导致未定义的异常 这里是代码 public void populateSp
我在 xml 文件中有一个微调器,宽度和高度设置为 wrap_content,我将背景设置为微调器, 文本居中显示,如果我在 dp 或 fill_parent 中设置宽度,则文本显示为左对齐,但当微调
我有一个微调器 onItemSelect 我需要根据第一个选择打开另一个微调器。这是代码......我能够给第一个微调器充气但是在选择一个条目时没有任何反应 Spinner filterSpinner
当我打开 TalkBack 时,在微调器中做出选择后,焦点会立即返回到屏幕顶部(工具栏)。我希望在用户选择后立即将焦点放在微调器上。 我已经尝试将微调器设置为可聚焦: spinner.setFocus
我知道已经有大约一百万个主题,但请听我说完。 标题说明了一切,当我在微调器 1 中选择一个项目时,微调器 2 会获得一个特定的选项列表供您选择(然后将用于显示信息)。它本质上是一本小型通讯录。 *更新
我正在为 Android 构建一个有两个微调器的应用程序。第一个有一系列选择。我有一些代码可以更改第二个微调器的值,但数组在第二个微调器中永远不会改变。我检查过,selectedValue 确实等于所
我有一个 xml 布局文件,其中包含一些小部件,包括一个 Spinner 我想在微调器中显示一个字符串列表,该列表是在运行时作为函数的结果生成的,因此它不能在 arrays.xml 中。 我试过: S
spinner 没有根据另一个 spinner selection 填充,我已经研究了好几个小时了,仍然无法解决问题,没有错误洛格卡特。提前致谢。 这是我的代码 ArrayAdapter adapt
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 6 年前。 Improve this qu
我介绍一个Spinner在我的小部件中,每次我从中选择不同的值时,我都想执行一些操作。 是否可以? 我好像只收到事件 on_press和 on_release ,但在做出不同值(value)的选择时,
这是一个应用程序运行中的简单 gif,以显示我的意思:Video Gif here 我有一个 Spinner,这是我的 XML 代码: 我遵循了一些教程并浏览了此处,以使用 parse.com 动态
我有一个包含 1 个微调器的“发票”登记 Activity 。在这个 Spinner 中包含带有 IDCard 的卡片注册、标题和描述(保存在数据库的“Card”表中)。当我将此“发票”记录保存在“发
我在我的元素中实现了 mat-spinner,但它只有很少的可配置更改,例如颜色、旋转模式。我想在微调器中包含品牌/公司 Logo 的图像图标,我该如何实现? 以下是 Stackblitz 链接: h
在处理 Android Honeycomb 项目时,我偶然发现了一个有趣的问题。如下图所示,在对话框中展开 Spinner 时,底部的导航栏与它重叠。因此,无法选择底部的元素。 为了解决这个问题,我尝
当我在第一个微调器中选择中国作为国家/地区时,看到我想要的,所以我希望第二个微调器必须显示中国的所有州,这是通过我的代码完成的……但是……!我的查询是,当我从第二个微调器中选择状态时,它会自动将其设置
我正在尝试为应用创建一个简单的设置页面。 我想创建一个对象列表,我在 XML 文件中定义了其布局。 在这些对象的布局中有一个标题 TextView、一个帮助 TextView 和一个 Spinner。
我正面临这个问题,已经阅读了很多问题/答案但无法解决。 我有一个带有微调器的 ListView(用户必须为该行中的产品选择一个数量),但是当项目多于 View 并且在微调器中选择一个数字后滚动 Lis
我是一名优秀的程序员,十分优秀!