1

I want to access inner class Hashmap variable in outer class method means item selected listener of spinner. I have Hashmap declare inside inner class and I am assigning value in Hashmap inside inner class method. I am accessing the hashmap value using key in setOnItemSelectedListener from outer class but i got null value in hashmap.
I was made hashmap static in outer class and put the value in inner class and again access from the outer class listener but again got null value. if anybody having another soltion so please tell me. I am little bit confused here .I don't know where i am wrong. Please anybody have solution.

following is the outer and inner class

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;
    }
nilesh wani
  • 1,261
  • 5
  • 18
  • 30
  • http://stackoverflow.com/questions/12251922/i-thought-inner-classes-could-access-the-outer-class-variables-methods?rq=1 see the link – Sunil Kumar May 13 '13 at 09:02

2 Answers2

1

declare the variable you want to access in Outer scope

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

    });   

 ....

}
Blackbelt
  • 156,034
  • 29
  • 297
  • 305
  • I don't understand What did you say? – nilesh wani May 13 '13 at 09:11
  • you have the innerclass scoper variable and outerclass scope variable. Variable declared in the outerclass scope are visible to the inner class. Since you want to access p_id in the outer class, declare it in the outer class – Blackbelt May 13 '13 at 09:18
1

Problem is phaseNo[index] is not initialized. its null. your key is null

Also

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

A hashmap with key value pair as strings

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

To get

     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

If you want to access p_id everywhere in you class declare it as a outerclass variable.

Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • phaseNo is not null here because i did check this value using log like Log.v("PhaseNo[i]","-"+phase) and its print fine and also my hashmap also properly filled with data key-value pair. everything is put properly inner class but i am not able access in outer class. please see updated question – nilesh wani May 13 '13 at 09:37
  • @nileshwani before editing PhaseNo[i] was nit intialized. it was null. Hashmap in your case takes key value pair of strings. so whe you get the value use the string as a key. and keep the old post so it should not mislead people who visit the post.You did this phaseIdKv.put(phaseNo[i],phaseId1); and this phaseNo[i] was not initialized – Raghunandan May 13 '13 at 09:41
  • Thanks, it's working great when i write String.valueOf(id) in code. but i don't understand why this work now can you explain it. – nilesh wani May 13 '13 at 09:46
  • your key is a string as i mentioned in above comment. And you used id which was int. So you need to convert int to string and then get the value using the key. HashMap phaseIdKv = new HashMap(); hash map key value pair of string meaning key is string value is string. if it helps pls accept and vote the answer – Raghunandan May 13 '13 at 09:48