Spinner OnItemSelectedListener在1个活动中与2个微调器无法正常工作

时间:2017-03-17 17:53:56

标签: android sqlite spinner

我一直在android studio中进行编码,在1个活动中有2个微调器。当我选择一个Item时,即使微调器有不同的变量,它也不会调用Listener。

以下是从数据库填充微调器以及选择值的相关代码(在loadSYData函数中)

public void loadQtrData() {
    Cursor cursor = myDb.getQtr();
    ArrayList<String> qtrLabel = new ArrayList<>();
    ArrayAdapter<String> qtrAdapter = new ArrayAdapter<String>(this, R.layout.spinner_item, qtrLabel);
    qtrAdapter.setDropDownViewResource(R.layout.spinner_item);
    spinnerQtrParent.setAdapter(qtrAdapter);
    while(cursor.moveToNext()){
        String label = cursor.getString(cursor.getColumnIndex("SC_Quarter"));
        qtrLabel.add(label);
    }
}

public void loadSYData(){
    Cursor cursor = myDb.getSY();
    ArrayList<String> syLabel = new ArrayList<>();
    ArrayAdapter<String> syAdapter = new ArrayAdapter<String>(this, R.layout.spinner_item, syLabel);
    syAdapter.setDropDownViewResource(R.layout.spinner_item);
    spinnerSYParent.setAdapter(syAdapter);
    while(cursor.moveToNext()){
        String label = cursor.getString(cursor.getColumnIndex("SY"));
        syLabel.add(label);
    }
    spinnerSYParent.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            sy = "Success";
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });
}

我还举行了一场祝酒会,只是为了检查听众是否被调用。但是吐司程序没有执行。

1 个答案:

答案 0 :(得分:0)

可能是您的光标没有数据或者它没有移动到第一个位置 检查此代码并查看您的微调器事件设置与否的天气。

    public void loadSYData(){
        Cursor cursor = myDb.getSY();
        ArrayList<String> syLabel = new ArrayList<>();
        ArrayAdapter<String> syAdapter = new ArrayAdapter<String>(this, R.layout.spinner_item, syLabel);
        syAdapter.setDropDownViewResource(R.layout.spinner_item);
        spinnerSYParent.setAdapter(syAdapter);
        // add this line to move cursor to 1st position.
        cursor.moveToFirst();
        while(cursor.moveToNext()){
            String label = cursor.getString(cursor.getColumnIndex("SY"));
            syLabel.add(label);
        }
        // Put debug point or print some thing here to confirm
        spinnerSYParent.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                sy = "Success";
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }