Spinner没有使用AsyncTask在Android中显示项目列表

时间:2012-05-17 06:26:44

标签: android android-asynctask spinner android-arrayadapter

我有一个活动,在AsyncTask的帮助下,我使用Spinner从WebService调用中填充它。我的问题是,在logcat中,我可以看到所有ArrayAdapter<String>项,但是当我将此适配器提供给我的Spinner时,它不显示下拉列表项。 这是我的代码片段:

    public static ArrayAdapter<String> adapter;
    Spinner spnrCourseType;
    spnrCourseType.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                new DownloadCourseTypesList().execute(training_id);
                return true;
            }
        });

        spnrCourseType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {
                ting("You Selected : " + adapter.getItem(arg2));
            }

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

            }
        });
    /** Thread AsyncTask Class */
    private class DownloadCourseTypesList extends AsyncTask<String,Void,ArrayList<String>> {

        ProgressDialog progressDialog = new ProgressDialog(CorporateTrainingInquiryActivity.this);
        protected void onPreExecute(){
            actionBar.setProgressBarVisibility(View.VISIBLE);
            progressDialog.setTitle("Course Types Loading..");
            progressDialog.setMessage("Please Wait..");
            progressDialog.show();  
        }

        protected ArrayList<String> doInBackground(String... params) {          

            trainingList = new TrainingList(params[0]);
            ArrayList<String> values = trainingList.getTrainingList();    
//          adapter = new ArrayAdapter<String>(CorporateTrainingInquiryActivity.this,android.R.layout.simple_spinner_item,values);

            return values;
        }

        protected void onPostExecute(ArrayList<String> values) {
            actionBar.setProgressBarVisibility(View.GONE);
            adapter = new ArrayAdapter<String>(CorporateTrainingInquiryActivity.this,android.R.layout.simple_spinner_item,values);
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            adapter.insert("Select Course Type",0);
            spnrCourseType.setAdapter(adapter);

            for(int i=0;i<adapter.getCount();i++){
                System.out.println("Item in adapter at position : " + i + adapter.getItem(i));
            }

            adapter.notifyDataSetChanged();
            if(progressDialog.isShowing()){
                progressDialog.dismiss();
            }
        }      
    }

当我执行上面的代码时,ProgressDialog变得可见,只有第一项,即“选择课程类型”在Spinner本身可见,但不在下拉列表项中。关于它的任何线索。我从昨天本身陷入困境:( enter image description here

1 个答案:

答案 0 :(得分:0)

此代码: -

    spnrCourseType.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            new DownloadCourseTypesList().execute(training_id);
            return true;
        }
    });

可能会导致某种竞争条件,当您按下微调器时,您的任务会被执行,但它可能无法足够快地填充微调器以便您查看结果。

我建议将此代码移动到其他位置,例如onCreate方法或等效方法。

public void onCreate(Bundle icicle) {
    super.onCreate(icicle);

    new DownloadCourseTypesList().execute(training_id);
}

希望有所帮助。