Android:微调器 setSelection 不起作用

标签 android spinner android-spinner

我无法使用微调器 setSelection() 方法解决问题。我正在编写一个程序来保存数据库中的某种表中的数据。当用户打开一个程序时,它从数据库加载数据并添加 3 个空的“表”行,以便用户可以填充并保存它们。加载数据库中的数据时,我需要正确设置微调器的选择,但是当我尝试我的代码时它不起作用,屏幕上的所有微调器都选择了 0 个项目。 这是我的代码。
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() 是一个加载数组“供应商”(从文件)的函数,然后我将其放入微调器适配器中。
load_database() 函数:

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() 函数:

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

奇怪的是,当我进入第二个 Activity ,然后返回到第一个 Activity 时,我的选择设置正确。但是当我关闭程序,然后再次打开它时,所有微调器都设置为索引为 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();
        }
    }
}

是的,Arrays.asList(suppliers).indexOf() 在两个地方(在 onResume() 和 draw() 函数中)返回正确的索引,我已经检查了几次。

最佳答案

我也有同样的问题。 解决方案是发布 setSelection() 方法:

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

在某些情况下,调用 setSelection(position, animation) 而不是 setSelection(position) 也可以解决问题:

setSelection(1, false);

但我推荐第一个代码。

希望对您有所帮助。

关于Android:微调器 setSelection 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17573018/

相关文章:

android - 屏幕截图 :get Bitmap from Textureview and display in imageview android

android - 突出显示预选的微调项

java - Android Edittext 的起始值不能是我数组中的第一个项目(Edittext Spinner)

android - 如何在 TextView 下方正确显示弹出菜单,类似于 Spinner?

java - 设置微调器选择不起作用

android - 未收到通知的 PendingIntent

android - 通过主题应用 buttonStyle 不起作用,直接应用完美

android - 单击 logcat 时 Eclipse 未打开源代码

android - 在下拉模式下单击展开时微调器崩溃但在对话框模式下工作(仅在 API < 26 中)

android - 如何更改微调器弹出窗口的背景颜色?