Android:spinner&x27;行不通
我无法使用微调器setSelection()方法解决此问题。我正在写一个程序来保存数据库中的数据,这些数据来自某种表。当用户打开一个程序时,它会从数据库加载数据,并添加3个空的“表”行,这样用户就可以填充它们并保存。加载数据库中的数据时,我需要正确设置微调器的选择,但当我尝试编写代码时,它就是不起作用,屏幕上的所有微调器都显示为选中的0项。 这是我的密码Android:spinner&x27;行不通,android,spinner,android-spinner,Android,Spinner,Android Spinner,我无法使用微调器setSelection()方法解决此问题。我正在写一个程序来保存数据库中的数据,这些数据来自某种表。当用户打开一个程序时,它会从数据库加载数据,并添加3个空的“表”行,这样用户就可以填充它们并保存。加载数据库中的数据时,我需要正确设置微调器的选择,但当我尝试编写代码时,它就是不起作用,屏幕上的所有微调器都显示为选中的0项。 这是我的密码 OnCreate()方法: @Override public void onCreate(Bundle savedInstanceSt
OnCreate()方法:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.data);
dbHelper = new DBHelper(this);
content = (LinearLayout) findViewById(R.id.content);
inflater = getLayoutInflater();
Calendar c = Calendar.getInstance();
cur_year = c.get(Calendar.YEAR);
cur_month = c.get(Calendar.MONTH);
cur_day = c.get(Calendar.DAY_OF_MONTH);
load_array();
load_database();
for (int i = 0; i < EMPTY_LINES_TO_DRAW; i++) {
draw();
}
}
draw()函数:
private void load_database() {
SQLiteDatabase db = dbHelper.getWritableDatabase();
Cursor c = db.query("mytable", null, null, null, null, null, null);
if (c.moveToFirst()) {
int date_1_Index = c.getColumnIndex("date_1");
int key_Index = c.getColumnIndex("key_field");
int supp_Index = c.getColumnIndex("supplier");
int sum_Index = c.getColumnIndex("summ_field");
int check_Index = c.getColumnIndex("check_field");
int date_2_Index = c.getColumnIndex("date_2");
do {
draw(c.getString(date_1_Index), c.getString(key_Index), c.getString(supp_Index), c.getInt(sum_Index), c.getInt(check_Index), c.getString(date_2_Index));
} while (c.moveToNext());
}
c.close();
}
public void draw(String date_1_string, String key_string, String supp_string, Integer sum_int, Integer check_int, String date_2_string) {
View item = inflater.inflate(R.layout.ll_item, content, false);
for (int i = 0; i < 6; i++) {
delta_id[i] = generateViewId();
}
id.add(delta_id.clone());
Button date_1 = (Button) item.findViewById(R.id.date_1);
date_1.setText(date_1_string);
date_1.setOnClickListener(this);
date_1.setId(delta_id[0]);
EditText key = (EditText) item.findViewById(R.id.key);
key.setText(key_string);
key.setId(delta_id[1]);
Spinner spin = (Spinner) item.findViewById(R.id.spin);
adapter_supp = new ArrayAdapter<String>(this, R.layout.spinner, suppliers);
spin.setAdapter(adapter_supp);
spin.setPrompt("Поставщик:");
Log.d("my_log", "supp_string = " + supp_string);
spin.setSelection(Arrays.asList(suppliers).indexOf(supp_string));
Log.d("my_log", "Index of the supp_string = " + Arrays.asList(suppliers).indexOf(supp_string));
adapter_supp.notifyDataSetChanged();
spin.setId(delta_id[2]);
EditText sum = (EditText) item.findViewById(R.id.sum);
sum.setText(sum_int.toString());
sum.setId(delta_id[3]);
CheckBox check = (CheckBox) item.findViewById(R.id.check);
if (check_int == 1) {
check.setChecked(true);
} else if (check_int == 0){
check.setChecked(false);
}
check.setId(delta_id[4]);
Button date_2 = (Button) item.findViewById(R.id.date_2);
date_2.setText(date_2_string);
date_2.setOnClickListener(this);
date_2.setId(delta_id[5]);
content.addView(item);
}
public void draw(字符串日期\u 1\u字符串、字符串键\u字符串、字符串支持字符串、整数和\u int、整数检查\u int、字符串日期\u 2\u字符串){
查看项目=充气机。充气(R.layout.ll_项目,内容,false);
对于(int i=0;i<6;i++){
delta_id[i]=generateView();
}
id.add(delta_id.clone());
按钮日期_1=(按钮)项.findViewById(R.id.date_1);
date_1.setText(date_1_字符串);
date_1.setOnClickListener(此);
日期1.设置id(增量id[0]);
EditText键=(EditText)item.findViewById(R.id.key);
key.setText(key_字符串);
key.setId(delta_id[1]);
微调器旋转=(微调器)item.findViewById(R.id.spin);
adapter_supp=新阵列适配器(此,R.layout.spinner,供应商);
spin.setAdapter(适配器\u supp);
spin.setPrompt(“ПСаааааааааааааааааа;
Log.d(“我的日志”、“支持字符串=“+支持字符串”);
spin.setSelection(Arrays.asList(suppliers.indexOf(supp_string));
Log.d(“我的日志”,“支持字符串的索引=“+Arrays.asList(suppliers.indexOf(支持字符串));
适配器_supp.notifyDataSetChanged();
spin.setId(delta_id[2]);
EditText总和=(EditText)item.findViewById(R.id.sum);
sum.setText(sum_int.toString());
sum.setId(delta_id[3]);
复选框check=(复选框)item.findViewById(R.id.check);
如果(检查_int==1){
check.setChecked(true);
}else if(检查_int==0){
check.setChecked(false);
}
检查setId(delta_id[4]);
按钮日期_2=(按钮)项.findViewById(R.id.date_2);
date_2.setText(date_2_字符串);
日期2.setOnClickListener(本);
日期2.设置id(增量id[5]);
content.addView(项目);
}
奇怪的是,当我转到第二个活动,然后返回到第一个活动时,我的选择设置正确,就像以前一样。但当我关闭程序,然后再次打开它时,所有微调器都设置为索引为0的默认项。以下是我的onResume()代码:
@覆盖
恢复时公开作废(){
super.onResume();
加载数组();
// Верхний, главный, фильтр.
adapter_supp=新阵列适配器(此,R.layout.spinner,供应商);
适配器\u支持setDropDownViewResource(android.R.layout.simple\u微调器\u下拉项);
微调器过滤器\u supp=(微调器)findViewById(R.id.filter\u supp);
过滤器支持设置适配器(适配器支持);
过滤器辅助设置提示(“Фаааааааааааа”:);
//更新纺纱机。
对于(int i=0;i
是的,两个位置的Arrays.asList(suppliers).indexOf()都返回正确的索引(在onResume()和draw()函数中),我已经检查了几次。调用
适配器\u supp.notifyDataSetChanged()
在您尝试使用supp.setSelection()
设置所选项目之前(而不是之后),我已尝试将位置值存储在int(spinnerSelection)中,当它点击onItemSelected时(调用.setSelection()),我将再次进行设置。这是一个黑客,但它现在对我有效
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
if(spinnerFixer >=2){ //it hits this twice before any user input for some reason (init + setSelection both trigger).
//stuff
} else { spinnerFixer++;
if(spinnerSelection != 0){
spinner.setSelection(spinnerSelection,true); spinnerSelection=0; }
}
public void未选中(AdapterView父视图、视图、,
内部位置,长id){
如果(spinnerFixer>=2){//出于某种原因,在任何用户输入之前(init+setSelection)都会触发两次。
//东西
}else{spinnerFixer++;
if(喷丝头选择!=0){
spinner.setSelection(spinnerSelection,true);spinnerSelection=0;}
}
我也有同样的问题。解决方案是发布setSelection()
方法:
mSpinner.post(new Runnable() {
public void run() {
mSpinner.setSelection(1);
}
});
在某些情况下,调用setSelection(position,animation)
而不是setSelection(position)
可以解决以下问题:
setSelection(1, false);
但我推荐第一个代码
希望对您有所帮助。为适配器设置值后,请调用
setSelection()
ArrayAdapter适配器=新的ArrayAdapter
(这是android.R.layout.simple\u微调器\u项,
纺纱线);
setDropDownViewResource(android.R.layout.simple\u微调器\u下拉菜单\u项);
旋转器。设置适配器(适配器);
//选举
旋转器。选择(1);
没有帮助。在第一种情况下,当我转到第二个活动,然后返回时,它仍然有效。但它对关闭和打开程序没有影响。我仍然认为这与您在循环onResume()中调用notifyDataSetChanged()有关。您应该只需要在数据(供应商)之后调用notifyDataSetChanged()更改。设置适配器后,我看不到数据发生任何更改,因此无需在onResume()中调用notifyDataSetChanged()。此外,适配器不需要在draw()中反复安装。在onCreate()中创建一次,只要在需要调用setAdapter()时重用该实例即可对于您的微调器。在我的第二个活动中,我可以更改供应商列表,因此我需要使用更改的数组“供应商”创建适配器。忘记在问题中写入它,抱歉。感谢您提供有关在循环中调用notifyDataSetChanged()的建议,但它仍然无法解决我的问题。请查看我的答案是否为h
setSelection(1, false);