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

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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
@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
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.
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<String, String> phaseIdKv = new HashMap<String, String>(); hash map key value pair of string meaning key is string value is string. if it helps pls accept and vote the answer
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();
        }

    });   

 ....

}

2 Comments

I don't understand What did you say?
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.