1

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

以下是外部和内部类

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

2 回答 2

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 ,请将其声明为外部类变量。

于 2013-05-13T09:25:27.227 回答
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();
        }

    });   

 ....

}
于 2013-05-13T09:00:13.273 回答