0

(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?!

azmath
  • 863
  • 1
  • 11
  • 30
  • 1
    please learn java's basics ... fx: how to get static fields ... also java's basics: if you assign new value to the field/variable it will not change data everywhere where you pass it ... – Selvin Oct 13 '15 at 12:27
  • I don't get it. I'm a noob to java. I thought that everything in Java is pass-by-reference except for primitive types. If that is not so, then what other options do I have to access the most updated version of the object? – azmath Oct 13 '15 at 12:38
  • 1
    FYI there's a good answer on Java being "pass by reference" or "pass by value" http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value – TayTay Oct 13 '15 at 12:46

2 Answers2

1

Have you also called notifyDatasetChanged() on your adapter when you load the data from the database? That will trigger an update to the view.

public static HashMap<String, Long[]> progressTracker = new HashMap<>();

I'd recommend you make the progressTracker not static as that isn't what you want. See Understanding Class Members.

Also, make sure you've added values to the HashMap using put() first.

E.g.

progressTracker.put("progress1", 1).
Tim Kist
  • 1,164
  • 1
  • 14
  • 38
  • I have already done these. The notifyDatasetChanged() is called and progressTracker.put is also used to update. The problem is with accessing the updated tracker. The android studio debugger tells me that when I am putting values in the hashmap it updates. But when I try to read them the hashmap has old values! I have removed the static declaration now and trying to access it via instance. i.e. I am using getActivity() to pass HomeActivity as Context to the recyclerview from the fragment. Then I cast the Context to my HomeActivity class and access inner methods of getprogressTracker. – azmath Oct 13 '15 at 14:03
1

Forget it, my Bad. The problem was something else. I was saving and loading the hashmap from storage. But I was reading it from the storage in the OnStart() method. This executed after OnActivityResult(). Thus, even though my hashmap was updated by the activity, it reloaded default values from storage! This was my problem. I moved the loader from OnStart() to OnCreate() and now it works!

Thank you all for trying to help!!

azmath
  • 863
  • 1
  • 11
  • 30