Reputation: 1291
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;
}
Upvotes: 1
Views: 1540
Reputation: 133560
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.
Upvotes: 1
Reputation: 157437
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();
}
});
....
}
Upvotes: 1