简体   繁体   English

Android:微调器 setSelection 不起作用

[英]Android: spinner setSelection doesn't work

I can't solve the problem with my spinner setSelection() method.我无法用我的微调器 setSelection() 方法解决问题。 I'm writing a program to save data, which I have from some kind of a table, in the DB.我正在编写一个程序来保存数据库中的某种表中的数据。 When the user opens a program it loads data from the DB and adds 3 empty "table" rows so the user can fill them and save them.当用户打开程序时,它会从数据库加载数据并添加 3 个空的“表”行,以便用户可以填充并保存它们。 When data from the DB is loaded, I need to set the selections of spinners correctly, but when I'm trying my code it just doesn't work and all the spinners on the screen appear with the 0 items selected.加载数据库中的数据时,我需要正确设置微调器的选择,但是当我尝试我的代码时,它不起作用,屏幕上的所有微调器都显示为选择了 0 个项目。 Here is my code.这是我的代码。
OnCreate() method: 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(); } }

load_array() is a function that load array "suppliers" (from file) which i then put in my spinner adapter. load_array() 是一个 function 加载数组“供应商”(来自文件),然后我将其放入我的微调器适配器中。
load_database() function: load_database() function:

 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(); }

draw() function:绘制()function:

 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); }

The weird thing is that when I go to the second activity, then return to the first, I got my selections set correctly as it was.奇怪的是,当我 go 到第二个活动,然后返回第一个活动时,我的选择设置正确。 But when I close the program, then open it again, all the spinners are set to default item with index 0. Here is my onResume() code:但是当我关闭程序,然后再次打开它时,所有微调器都设置为索引为 0 的默认项。这是我的 onResume() 代码:

 @Override public void onResume() { super.onResume(); load_array(); // Верхний, главный, фильтр. adapter_supp = new ArrayAdapter<String>(this, R.layout.spinner, suppliers); adapter_supp.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); Spinner filter_supp = (Spinner) findViewById(R.id.filter_supp); filter_supp.setAdapter(adapter_supp); filter_supp.setPrompt("Фильтр по поставщику:"); // Обновить спиннеры поставщиков. Update spinners. for (int i = 0; i < id.size(); i++) { Integer[] item = id.get(i); Spinner supp = (Spinner) findViewById(item[2]); supp.setAdapter(adapter_supp); if (pause_cash_string.= null) { supp.setSelection(Arrays.asList(suppliers);indexOf(pause_cash_string[i])). adapter_supp;notifyDataSetChanged() } } }

Yes, Arrays.asList(suppliers).indexOf() in both places (in onResume() and draw() functions) returns correct index, I have checked it few times.是的,Arrays.asList(suppliers).indexOf() 在这两个地方(在 onResume() 和 draw() 函数中)返回正确的索引,我检查了几次。

I have same problem.我有同样的问题。 the solution is posting setSelection() method :解决方案是发布setSelection()方法

 mSpinner.post(new Runnable() { public void run() { mSpinner.setSelection(1); } });

also in some cases calling setSelection(position, animation) instead of setSelection(position) solves the problem:在某些情况下调用setSelection(position, animation)而不是setSelection(position)可以解决问题:

 setSelection(1, false);

but I recommend the first code.但我推荐第一个代码。

hope helpful.希望有帮助。

Call adapter_supp.notifyDataSetChanged();调用adapter_supp.notifyDataSetChanged(); before (not after) you try to set the selected item with supp.setSelection()在您尝试使用supp.setSelection()

I've resorted to storing the value of the position in an int (spinnerSelection) and when it hits onItemSelected (which it will when you call.setSelection() ) I set it again.我已经将 position 的值存储在一个 int (spinnerSelection) 中,当它点击 onItemSelected 时(当你调用.setSelection() 时它会)我再次设置它。 It's a hack but it works for me now.这是一个黑客,但它现在对我有用。

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

After setting values to adapter, call setSelection()为适配器设置值后,调用setSelection()

 ArrayAdapter<String> adapter = new ArrayAdapter<String> (this, android.R.layout.simple_spinner_item, spinnerArray); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner.setAdapter(adapter); //setSelection spinner.setSelection(1);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM