-1

i am trying to get session stored variable in to a class. please see my actual code for class

 public class GetDataAdapter {

        public String ImageServerUrl;
        public String ImageTitleName;
        public String ImageUrlName;



        public String getImageServerUrl() {
            return ImageServerUrl;
        }

        public void setImageServerUrl(String imageServerUrl) {
            this.ImageServerUrl = imageServerUrl;
        }

        public String getImageTitleName() {
            return ImageTitleName;
        }

        public void setImageTitleNamee(String Imagetitlename) {
            this.ImageTitleName = Imagetitlename;
        }

        public String getImageUrlName() {
            return ImageUrlName;
        }

        public void setImageUrlNamee(String Imageurlname) {
            this.ImageUrlName = Imageurlname;
        }

   } 

now i stored a value in session and i want to use in above code. Imageurlname is a url fetching from database. i want to add extra to the url. for example

this is my URl Getting form database http://example.com?id= i stored user id in session so combining both url should be http://example.com?id=5

please see my modified code

public class GetDataAdapter extends AppCompatActivity {

    public String ImageServerUrl;
    public String ImageTitleName;
    public String ImageUrlName;

    private Session session;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        session = new Session(GetDataAdapter.this);

        HashMap<String, String> user = session.getUserDetails();
        final String Uid = user.get(session.KEY_UID);

    }


    public String getImageServerUrl() {
        return ImageServerUrl;
    }

    public void setImageServerUrl(String imageServerUrl) {
        this.ImageServerUrl = imageServerUrl;
    }

    public String getImageTitleName() {
        return ImageTitleName;
    }

    public void setImageTitleNamee(String Imagetitlename) {
        this.ImageTitleName = Imagetitlename;
    }

    public String getImageUrlName() {
        return ImageUrlName;
    }

    public void setImageUrlNamee(String Imageurlname) {
        this.ImageUrlName = Imageurlname + Uid;
    }


}

Uid is getting error. i hope you understand.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Asesha George
  • 2,232
  • 2
  • 32
  • 68
  • i asked a question for solution. not for negative voting. – Asesha George Oct 30 '16 at 04:36
  • Does the user id stays same everytime?, I mean user is logged in case etc? – Manikanta Oct 30 '16 at 04:59
  • yes its same as per the user. please see my both codes. is it my way of modification is correct? – Asesha George Oct 30 '16 at 05:05
  • okay one last question. Are you aware of shared preferences? and also from where are you getting uid? I can see this line mUid = session.getUserDetails().get(session.KEY_UID); before execution of this code you should be setting the obtained UID somewhere right? – Manikanta Oct 30 '16 at 05:21
  • ya i know. this session id i am using in MainActivity also its working fine every where. – Asesha George Oct 30 '16 at 05:27
  • Check my solution and let me know if it has not solved your problem. – Manikanta Oct 30 '16 at 06:01
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/126988/discussion-between-manikanta-and-asesha-george). – Manikanta Oct 30 '16 at 06:03

2 Answers2

1

Looks like the problem is with persisting the userid in your case it is because of this.

  1. Using instance variable to store user id which you can get only if you are getting the same object

Here are the solution(s):

Solution 1: Using Static Variables

public class Example {

    //this is the default value which will there stored before we are setting our actual userId

    public static String USER_ID="DefaultId";
}

You can set and access the values this way.

Log.d("Default Value",Example.USER_ID);

        //setting user id here
        Example.USER_ID = "Manikanta Garikipati";

        Log.d("Updated value",Example.USER_ID);

Solution 2: Using Shared preferences.

As you already know about this i would explain anyway.

Comment below if your problem is still not solved.

Here is the brief summary of the problem

The problem is not in shared preferences neither any storage.

Instead of creating a bean alone and setting the values to it , bean is extended with Activity etc.. which made things haywire..

Those who want the complete solution can go through the conversation in question.

Manikanta
  • 3,421
  • 4
  • 30
  • 51
0

Application class is there for you. use it and save your application level data, like this:

public class WhatEverApp extends Application
{
    String mApplicationLevelVar = "Hello";
}

WhatEverApp will be the name of your app used in manifest.xml

Look here for detailed discussion on Application class.

Community
  • 1
  • 1
Jamil Hasnine Tamim
  • 4,389
  • 27
  • 43