0

我有这样的代码

@Override
public void onCreate(Bundle savedInstanceState) {
     addItemsOnSpinnerOrgaLevel();

      btn_getReport.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {

            //How can i access map and list defined in orgaLevelTask(AsyncTask)???
             Like
             String option = parent.getItemAtPosition(pos).toString();              
            int orgaCode = orgaLevelMap.get(option);

            // Both are defined in AsyncTask ??

    }); //end of anonymous class

} //end of onCreate()

public void  addItemsOnSpinnerOrgaLevel() {

    orgaLevelTask  = new OrgaLevelTask(AccountReportActivity.this, spinner_orgaLevel, spinner_branch, txt_extra, txt_extra1);       
    orgaLevelTask.execute();

} //end of addItemsOnSpinnerOrgaLevel()

在 AsyncTask onPostExecute() 方法中我有

@Override
protected void onPostExecute(ArrayList<OrgaLevel> result) {     
    super.onPostExecute(result);                        
    if (result != null) {               
        addItemsOnSpinnerOrgaLevel(result);                                     
    } 
    dialog.dismiss();   
} //end of onPostExecute()

 public void  addItemsOnSpinnerOrgaLevel(ArrayList<OrgaLevel> result) {

    orgaLevelElementslist = new ArrayList<String>();
    orgaLevelElementslist.add("All");
    orgaLevelMap = new HashMap<String, Integer>();
    orgaLevelMap.put("All", 0);

    for (int i=0; i<result.size(); i++) {

        OrgaLevel orgaLevelRecord = (OrgaLevel) result.get(i);
        String key = orgaLevelRecord.getOrgaName();
        String value = orgaLevelRecord.getOrgaCode();

        orgaLevelMap.put(key, Integer.parseInt(value));             
        orgaLevelElementslist.add(key);

    } //end of for()

    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(accountReportActivity, android.R.layout.simple_spinner_item, orgaLevelElementslist);
    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner_orgaLevel.setAdapter(dataAdapter);

    setSpinnerOrgaLevelListener();

} //end of addItemsOnSpinnerOrgaLevel()

private void setSpinnerOrgaLevelListener() {

    spinner_orgaLevel.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) {             
            String option = parent.getItemAtPosition(pos).toString();               
            int orgaCode = orgaLevelMap.get(option);            
            subOrgaLevelTask  = new SubOrgaLevelTask(accountReportActivity, spinner_branch, orgaCode);      
            subOrgaLevelTask.execute();             

        } //end of onItemSelected()

    }); //end of anonymous class

} //end of setSpinnerOrgaLevelListener()

在 subOrgaLevelTask​​ 中,我也有与此类相同的哈希映射。你可以看到我想要做的是,在微调器中放置一个键值。因此,当我的btn_getReport按钮被点击时,我得到所选项目的值。就像如果All被选中,那么我得到 0 等等。这个关键价值的东西正在起作用。问题是当btn_getReport获得点击时,我怎样才能获得所选项目的值。因为我在后台线程中填充项目(在 OrgaLevelTask​​ 和 SubOrgaLevelTask​​ 中)并且我的按钮在 Activity 中。那么当按钮被点击时我该怎么做,然后我从 OrgaLevelTask​​ 和 SubOrgaLevelTask​​ 中定义的地图中获取值?

谢谢

4

1 回答 1

0

好吧,让它们在 orgaLevelTask​​ 中公开,然后简单地访问它们。orgaLevelTask​​ 应该是您活动类中的变量。你试过了吗?有什么错误吗?

您必须确保以线程安全的方式访问 orgaLevelTask​​ 成员

于 2012-07-16T13:52:50.087 回答