简体   繁体   English

setOnitemSelectedListener 不会通过以编程方式创建的微调器 Android 触发

[英]setOnitemSelectedListener is not triggering with the programmatically created spinner Android

My need is to我的需要是

  1. create a dynamic spinner on a button click单击按钮创建动态微调器
  2. select the spinner values and set the values into an edit text field.选择微调器值并将这些值设置到编辑文本字段中。

For this I have created a dynamic spinner programatically.为此,我以编程方式创建了一个动态微调器。 Put that code inside button click listener.将该代码放在按钮单击侦听器中。 Its working fine upto here.它在这里工作正常。

but the setOnitemSelectedListener of the dynamic spinner is not at all working.. there are no errors in the Logcat... please help me..但是动态微调器的setOnitemSelectedListener中没有错误......请帮助我..

------------ These are the methods inside onCreate ------------ ------------ 这些是 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());
        }

    }

Try this尝试这个

Write setOnItemSelectedListener inside createDynamicSpinnersetOnItemSelectedListenercreateDynamicSpinner

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

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

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