Asesha George Asesha George - 1 month ago 10
Android Question

how to get session variable in to class android

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.

Answer

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.

Comments