2

我的需要是

  1. 在按钮单击时创建动态微调器
  2. 选择微调器值并将值设置到编辑文本字段中。

为此,我以编程方式创建了一个动态微调器。将该代码放在按钮单击侦听器中。它的工作正常到这里。

但是setOnitemSelectedListener动态微调器根本不起作用.. Logcat中没有错误...请帮助我..

------------这些是onCreate里面的方法------------

Spinner spnOutHospitalList = new Spinner(Referance.this);

// list button on click event
btnList = (Button) findViewById(R.id.btn_list);
btnList.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
    // TODO Auto-generated method stub
    // function to create spinner dynamically
    createDynamicSpinner();
    }
});

// Out Hospital List Spinner on item click listener
spnOutHospitalList.setOnItemSelectedListener(new OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> arg0, View arg1,int position, long arg3) {
    // TODO Auto-generated method stub
    outHospitalName = hospitalNameListArray.get(position);
    outHospital.setText(outHospitalName);
    }
    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
    // TODO Auto-generated method stub
    }
});



------------ These are the functions outside onCreate but inside the Activity------------


// to create spinner dynamically
private void createDynamicSpinner() {
    // TODO Auto-generated method stub
    spnOutHospitalList.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    loadOutHospitalListSpinner();
    spnOutHospitalList.performClick();
}

// to load out hospital/ clinic data into spinner
private void loadOutHospitalListSpinner() {
    // TODO Auto-generated method stub
    try {
        if (getFirstRun()) {
        sampleDB = dbAdapter.getDatabase();
        setRunned();
        } 
        else {
        sampleDB = dbAdapter.getWritableDatabase();
        }
        Cursor c1 = sampleDB.rawQuery("select DISTINCT EPR_OUT_HOSPITAL from EMR_PT_REFERNCE",null);
        System.out.println("count is " + c1.getCount());

        if (c1 != null && c1.getCount() != 0) {
            hospitalNameListArray.clear();
                if (c1.moveToFirst()) {
                    do {
                        hospitalNameListArray.add(c1.getString(c1.getColumnIndex("EPR_OUT_HOSPITAL")));
                    } while (c1.moveToNext());  
                }
            }
            c1.close();

            ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, hospitalNameListArray);

            // dropdownlist
            dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

            spnOutHospitalList.setAdapter(dataAdapter);

        } 
        catch (Exception e) {
            // TODO: handle exception
            System.out.println("CAT LIST ERROR IS: " + e.getMessage());
        }

    }
4

1 回答 1

2

尝试这个

setOnItemSelectedListener在里面createDynamicSpinner

private void createDynamicSpinner() {

//Remove this line from top in your code and add here
Spinner spnOutHospitalList = new Spinner(Referance.this);

    spnOutHospitalList.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

    //Pass Spinner Object to this function to load data in it!
    loadOutHospitalListSpinner(spnOutHospitalList);
    spnOutHospitalList.performClick();

spnOutHospitalList.setOnItemSelectedListener(new OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> arg0, View arg1,int position, long arg3) {
    // TODO Auto-generated method stub
    outHospitalName = hospitalNameListArray.get(position);
    outHospital.setText(outHospitalName);
    }
    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
    // TODO Auto-generated method stub
    }
});
}
于 2014-03-19T10:36:31.820 回答