如何在java中访问外部类方法中的内部类变量

时间:2013-05-13 08:59:22

标签: java android

我想在外部类方法中访问内部类Hashmap变量意味着项目选择了微调器的侦听器。我在内部类中声明了Hashmap,我在内部类方法中的Hashmap中赋值。我使用外部类中的setOnItemSelectedListener中的键访问hashmap值,但我在hashmap中得到了null值。
我在外部类中创建了hashmap静态,并将值放在内部类中,并再次从外部类侦听器访问,但又获得了null值。如果有人有其他解决方案,请告诉我。我在这里有点困惑。我不知道我哪里错了。请有人解决。

以下是外层和内层

public class ProjectDetailActivity extends SherlockActivity {

// declare hashmap.
HashMap<String, String> phaseIdKv = new HashMap<String, String>();
@Override
protected void onCreate(Bundle savedInstanceState) {

    new LoadPhaseData().execute(projId);
    //create variable of inner class
    final ProjectDetailActivity.LoadPhaseData inner = new ProjectDetailActivity().new LoadPhaseData(); 

   //Listener for Phase spinner

    projSpinnerPhase.setOnItemSelectedListener((OnItemSelectedListener) new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {

            int id = projSpinnerPhase.getSelectedItemPosition();
            ++id;
            String p_id= inner.phaseIdKv.get(id); //Here I want to access inner class hashmap value
            Log.v("hashmap value","-"+inner.phaseIdKv.get(id));//getting null

            Toast.makeText(getApplicationContext(),"item-"+id+" item2"+item+"p_id"+p_id, Toast.LENGTH_LONG).show();
        }

    });     
}
private class LoadPhaseData extends AsyncTask<String, Void, Void> {


    @Override
    protected Void doInBackground(String... params) {
        -----
        JSONArray phaseData = new JSONArray(jsonpPhaseList);
            String [] phaseNo = new String[jsonpPhaseList.length()];
            String phase;
            String phaseId1;
            if (phaseData.length() >0) {
                for(int i =0;i<phaseData.length();i++){
                    JSONObject joPhasefromJa = phaseData.getJSONObject(i);

                    phase = joPhasefromJa.getString("phase_no")
                    phaseId1 = joPhasefromJa.getString("phase_id");

                    phaseIdKv.put(phase,phaseId1);                  
                }
            }
        return null;
    }

2 个答案:

答案 0 :(得分:1)

声明要在外部作用域中访问的变量

public class ProjectDetailActivity extends SherlockActivity {
 String p_id;

 ...


    projSpinnerPhase.setOnItemSelectedListener((OnItemSelectedListener) new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {

            int id = projSpinnerPhase.getSelectedItemPosition();
            ++id;
            p_id= inner.phaseIdKv.get(id); //Here I want to access inner class hashmap value
            Log.v("hashmap value","-"+inner.phaseIdKv.get(id));//getting null

            Toast.makeText(getApplicationContext(),"item-"+id+" item2"+item+"p_id"+p_id, Toast.LENGTH_LONG).show();
        }

    });   

 ....

}

答案 1 :(得分:1)

问题是phaseNo [index]未初始化。它的空。你的密钥是空的

另外

     HashMap<String, String> phaseIdKv = new HashMap<String, String>();

将键值对作为字符串

的哈希映射
      //phaseNo is a string array.
      for(int i =0;i<phaseData.length();i++){ 
       phase = joPhasefromJa.getString("phase_no")
       phaseId1 = joPhasefromJa.getString("phase_id");
       phaseIdKv.put(phase ,phaseId1);  
      }

获得

     int id = projSpinnerPhase.getSelectedItemPosition();
     ++id;
     String p_id= phaseIdKv.get(String.valueOf(id));// convert int to string
     id should match phase that is the key put in hash map

如果要在类中的任何位置访问p_id,请将其声明为外部类变量。