How to execute a non static method from a static method - java

I am new to android/java development so I'm having a few issues, this is one of them.
I want to use the result returned in recevieResults in getTickets. I tried to make getTickets static but it has
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0);
and so the method being Static doesn't allow for getApplicationContext.
public static void receiveResults(String result2) {
usersXML = result2;
}
public void getTickets() {
//this method users usersXML from above and needs to execute after receiving the results
}

If you have a class:
public class ViewTicket extends Activity {
public static <result> recieveResults(Context context, String result2) {
ViewTicket ticket = (ViewTicket) context;
return <result>
}
public void getTickets(<result> result) {
// user <result> as you wish.
SharedPreference pref = this.getSharedPreferences("MyPref", 0);
}
}
Call the static method and pass the object to it.
ViewTicket myTicket = new ViewTicket();
<result> = ViewTicket.recieveResults(myTicket, "<someString>");
myTicket.getTickets(<result>);

Related

Efficient Way to Pass an Object Parameter in Java Method

I'm just looking some another efficient way to pass an object parameter to method.
So I have some method like this:
private void dashboardMenu() {
Dashboard dashboard = new Dashboard();
body.removeAll();
body.add(dashboard);
dashboard.setSize(body.getWidth(), body.getHeight());
dashboard.setVisible(true);
}
private void dataMenu() {
Data data = new Data();
body.removeAll();
body.add(data);
data.setSize(body.getWidth(), body.getHeight());
data.setVisible(true);
}
And I want an efficient method to call between this two method with object parameter (dashboard = new Dashboard(), and data = new Data()).
What I think it should be like this for example:
private void dasboardMenu() {
navigateMenu(Type object);
}
private void dataMenu() {
navigateMenu(Type object);
}
private void navigateMenu(Type object) {
object menu = new object();
body.removeAll();
body.add(menu);
menu.setSize(body.getWidth(), body.getHeight());
menu.setVisible(true);
}
Is it possible to do that?
Please give me an example. I don't even know what keyword should I do.
How about this (assuming your Dashboard and Data are Swing Components)?
private void dashboardMenu() {
navigateMenu(new Dashboard());
}
private void dataMenu() {
navigateMenu(new Data());
}
private void navigateMenu(JComponent c) {
body.removeAll();
body.add(c);
c.setSize(body.getWidth(), body.getHeight());
c.setVisible(true);
}

Modify variables in an instance from another class Android

I simplified this for brevity; hopefully this example isn't actually functional. I'm creating and doing things with a variable, then I'm having another class do some stuff, then that class refers back to the original and tells it to do more stuff with that variable.
I've done exactly this with views. I simply pass the activity and then when I need to use it I use activity.findViewById(id) to do stuff. With variables, you can't just do activity.variable. I tried using a getter (as shown in this example), but maybe I'm still just doing it wrong or it can't be done how I'd like:
public class MyActivity {
private int test;
public void onCreate(Bundle savedInstanceState) {
test = 5;
int data = 100;
//Pass something to it
new NotAnActivity().func(MyActivity.this,data);
}
public int gettest() {
return test;
}
public void func(Activity instance, int response) {
int test = new MyActivity().gettest();
//Do stuff with test
}
}
public class NotAnActivity {
public void func(Activity instance, int data) {
//Do stuff with data
int response = 20;
//Try to pass information back
new MyActivity().func(instance,response);
}
}
You can't use a activity.gettest() because you're passing the superclass Activity between classes. To have access to the gettest() method you need to pass the specific child activity (MyActivity extends Activity, pass MyActivity instead of Activity) or you can cast to your specific activity.
((MyActivity)activity).getter();
So here, instead of:
public void func(Activity instance, int data) {
//Do stuff with data
int test = ((MyActivity)instance).gettest();
}
or
public void func(MyActivity instance, int data) {
//Do stuff with data
int test = instance.gettest();
}
It's not a good idea to instantiate your activities yourself new A()
public class MainActivity extends AppCompatActivity {
private int test;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
test = 5;
int data = 100;
new NotAnActivity().func(this,data);
}
public int gettest() {
return test;
}
public void func(MainActivity instance, int response) {
//int test = new MainActivity().gettest();
int test = instance.gettest();
Log.v("variable", "test = " + test);
}
}
class NotAnActivity {
public void func(MainActivity instance, int data) {
//Do stuff with data
int response = 20;
//Try to pass information back
instance.func(instance,response);
}
}
You can try it. Your mistake is [new MyActivity()]

How to image button to SharedPreferences and save it

Hello guys i have a question.
I have a button with 2 options like checked and unchecked for girl and i have same for boys. In my fragment i need to select one and save it in SharedPreferences.
i have this for configure buttons:
private void setSexButtons() {
sexButtonBoy.setOnClickListener(view -> {
sexButtonBoy.setSelected(true);
sexButtonBoy.setScaleX(1.4f);
sexButtonBoy.setScaleX(1.4f);
sexButtonGirl.setSelected(false);
sexButtonGirl.setScaleY(1.0f);
sexButtonGirl.setScaleX(1.0f);
Settings.setSelectedIem(true);
});
sexButtonGirl.setOnClickListener(view -> {
sexButtonBoy.setSelected(false);
sexButtonGirl.setSelected(true);
sexButtonGirl.setScaleX(1.4f);
sexButtonGirl.setScaleX(1.4f);
sexButtonBoy.setScaleY(1.0f);
sexButtonBoy.setScaleX(1.0f);
Settings.setSelectedIem(true);
});
}
and i also have a method to save - but i think i do something bad becouse this not work
public static void setSelectedIem(boolean selectedIem) {
getPreferences().edit()
.putBoolean(SELECTED_SEX, selectedIem)
.apply();
}
private static final String SELECTED_SEX = "selectedSex";
Please give me any advice how to do this good.
Sorry i miss it here is it:
privated SharedPreferences sharedPreferences;
and in onCreate
sharedPreferences = getSharedPreferences("me.fast.app", MODE_PRIVATE);
and here is main method:
private static SharedPreferences getPreferences() {
return ApplicationFast.sharedPreferences;
}
Updated with new method
public static boolean isSelectedItem(){
return getPreferences().getBoolean(SELECTED_SEX, false);
}
I think your problem is saving a boolean, because for both sexes you are saving "true".
You should do this:
private void setSexButtons() {
//If you want to recover the settings do this:
sexButtonBoy.setSelected(Settings.isSelected());
sexButtonGirl.setSelected(!Settings.isSelected());
sexButtonBoy.setOnClickListener(view -> {
sexButtonBoy.setSelected(true);
sexButtonBoy.setScaleX(1.4f);
sexButtonBoy.setScaleX(1.4f);
sexButtonGirl.setSelected(false);
sexButtonGirl.setScaleY(1.0f);
sexButtonGirl.setScaleX(1.0f);
Settings.setSelectedIem(true);
});
sexButtonGirl.setOnClickListener(view -> {
sexButtonBoy.setSelected(false);
sexButtonGirl.setSelected(true);
sexButtonGirl.setScaleX(1.4f);
sexButtonGirl.setScaleX(1.4f);
sexButtonBoy.setScaleY(1.0f);
sexButtonBoy.setScaleX(1.0f);
Settings.setSelectedIem(false);
});
}
And then:
public static void setSelectedIem(boolean selectedIem) {
getPreferences().edit()
.putBoolean(IS_BOY, selectedIem)
.apply();
}
private static final String IS_BOY = "isboy";

Java: saved settings with Preferences not restored when a GUI application starts

In my Java app, I have a number of saved values and settings that I would like to get as soon as the application starts. All these values are supposed to be stored in a singleton.
For example, I have a JDialog, where I save a path to my file using this:
AppSingleton appSingleton = AppSingleton.getInstance( );
private Preferences prefs;
private void btnSetPanelSetNavdataActionPerformed(java.awt.event.ActionEvent evt) {
appSingleton.setNavdataPath(txtNavdataPath.getText());
prefs = Preferences.userRoot().node(this.getClass().getName());
prefs.put("NAVDATA_PATH", txtNavdataPath.getText());
dispose();
}
I am checking and the value is saved.
This is my singleton:
public class AppSingleton
{
private static AppSingleton instance = null;
private String navdataPath = "";
private AppSingleton()
{
}
public static AppSingleton getInstance()
{
if(instance == null)
{
instance = new AppSingleton();
}
return instance;
}
public String getNavdataPath() {
return navdataPath;
}
public void setNavdataPath(String navdataPath) {
this.navdataPath = navdataPath;
}
public void initDefaultValues()
{
Preferences prefs = Preferences.userRoot().node(this.getClass().getName());
this.navdataPath = prefs.get("NAVDATA_PATH","");
System.out.printf("CHECK DEFAULT: %s",this.navdataPath);
}
}
I am trying to get the saved values from the main() method in the class that initializes the JFrame:
public static void main(String args[]) {
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable()
{
public void run() {
new IGAMainFrame().setVisible(true);
/* HERE!!! */
AppSingleton appSingleton = AppSingleton.getInstance();
appSingleton.initDefaultValues();
}
});
}
But the values are not initialized from the Preference class. "CHECK DEFAULT" shows empty string. I do not understand why and whether there is a better practice of restoring all saved values when a GUI application starts.
Thanks!
Change the line prefs = Preferences.userRoot().node(this.getClass().getName()); inside btnSetPanelSetNavdataActionPerformed to prefs = Preferences.userRoot().node(Appsingleton.class.getName()); Inside button click method, this.getClass() is not AppSingleton

Getting value from one activity to another class

can anyone guide me to the right direction, as the situation is
have two classes calssA with Activity while classB is simple java class
1.classA has a editbox where the user inputs some value.
2.classB has a method that computes the user input.
need to get the user input value from classA to classB.
a).cannot get it through passing intent as the other class does not have activity.
b).have tried getter setter method by creating a class thus,calling setter where the user inputs the detail.
and calling the getter in another class.
Still the value is null, so what apparently is missing here.Any guidance to a example or brief explanation would be great.Thanks
You can use your own customized class to store the data and use it any where you need.
DataClass
public class Data {
private String a,b;
private static Data data= new Data( );
/* A private Constructor prevents any other
* class from instantiating.
*/
private Data (){ }
/* Static 'instance' method */
public static Data getInstance( ) {
return data;
}
public String getA()
{
return this.a;
}
public String getB()
{
return this.b;
}
public void setA(String a)
{
this.a = a;
}
public String getB(String b)
{
this.b = b;
}
}
In Activity
Data data = Data.getInstance()
data.setA("Stack");
data.setA("Overflow");
you can use this values in Java file like this
Data data = Data.getInstance()
System.out.println(data.getA());
System.out.println(data.getB());
according to my knowledge here you have to use singleton class.
public class DataHolderClass {
private static DataHolderClass dataObject = null;
private DataHolderClass() {
// left blank intentionally
}
public static DataHolderClass getInstance() {
if (dataObject == null)
dataObject = new DataHolderClass();
return dataObject;
}
private String _ProductNames;
public String get_ProductNames() {
return _ProductNames;
}
public void set_ProductNames(String _ProductNames) {
this._ProductNames = _ProductNames;
}
}
to set data
DataHolderClass.DataHolderClass.set_ProductNames(your data variable);
to get data
DataHolderClass.DataHolderClass.get_ProductNames(your data variable);
You can save the edittext value in SharedPreferences, thus making it available in all activity.
By doing this you can fetch the value in other activity without any hassle.
eg:
SharedPreferences.Editor editor = preferences.edit();
editor.putString("edtTextValue", valueOfEditText);
editor.commit();
Further fetching the value in other activity like this,
preferences.getString("edtTextValue", "");
In your activity class create
public static String valueEntered;
valueEntered=mEdtTxt.getText().toString();
and in your java class where you want
String enteredValue=ClassA.valueEntered;

Categories