(Forget this, just Read Edit 2 & 3) I have declared a hashmap in my main activity class as follows:
public static HashMap<String, Long[]> progressTracker = new HashMap<>();
Then, inside my fragment, I instantiate the recycler view using getActivity()
as follows:
genList = new azRVAdapter(myStream,!masterIndex,masterIndex?0:(position-1),getActivity());
recList.setAdapter(genList);//azRVAdapter is custom adapter class
And I have declared my custom adapter class variables as follows:
private azStream myStream;
private HashMap<String,Long[]> progressTracker;
private boolean getTopicsList;
private int subjectIndex;
private Context masterContext;
My Custom adapter class constructor is as follows:
public azRVAdapter(azStream myStream, boolean getTopicsList, int subjectIndex, Context masterContext){
this.myStream = myStream;
this.getTopicsList = getTopicsList;
this.subjectIndex = subjectIndex;
this.masterContext = masterContext;
this.progressTracker = ((HomeActivity) masterContext).progressTracker;
}
Now the problem is in the last line of the constructor. I am trying to pass the hashmap in the homeactivity to the adapter but instead I am getting a new instance of progressTracker with empty values!
What am I getting wrong... It actually worked but I did some code cleanup and changed many variables declared public
to private
. I don't remember what all variables I changed so can't revert back. If you could, pls point out the problem.
Edit
I've tried this as well
this.progressTracker = HomeActivity.progressTracker;
Edit 2 (More info on the problem)
I have declared a simple integer in my main activity like this:
public int currPageNo = 0;
I then access it from the recyclerView within the fragment within the Activity as follows:
((HomeActivity) masterContext).currPageNo = myPageNo;
((HomeActivity) masterContext).navigateToPage(myPageNo);
And it works! When I use the same thing to access my hashmap it doesn't work!
((HomeActivity) masterContext).getProgressTracker(myPath);
where,
public Long[] getProgressTracker(String key){
return progressTracker.get(key);
}
Is there something special about HashMaps?! (BTW, My hashmap is not static anymore, its public and i am still using a getter method)
Edit 3 (In case it helps, this is how I update my HashMap
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if ((resultCode == RESULT_OK) && (requestCode == 1)) {
if (data.hasExtra("id")) {
String id = data.getStringExtra("id");
Long stat[] = {(long)0,(long)0,(long) 0};
stat[0] = (long)data.getIntExtra("remCards",0);
stat[1] = (long)data.getIntExtra("allCards",0);
stat[2] = System.currentTimeMillis()/1000;
progressTracker.put(id,stat);
((indexFragment) getSupportFragmentManager().findFragmentByTag("android:switcher:"+R.id.home_pager+":"+String.valueOf(currPageNo))).updateProgress();
}
}
When I debug the line progressTracker.put, Android Studio shows me an updated version of the progressTracker (it seems when I access progressTracker from here it is a different instance from when I access from the getter!) What's wrong?!