I've created a class called website, and want to access it like a variable so I can update values to it, probably better explained below:
Website w = new Website();
w.URL="stackoverflow.com";
Here's the code for the class:
class Website {
public String URL;
public Website(){
URL = "";
}
}
I would also like to add a method such as this:
public long save() {
return db.save(URL);
}
This (the method) isn't working for me at the moment
I would do it more OO way, hiding this URL variable from outside and letting change it's value from getter and setter methods. You can try this, maybe this will help.
In Website class
public class Website {
private String URL;
public Website(){
this.URL = "";
}
public void setUrl(String url) {
this.URL = url;
}
public String getUrl() {
return this.URL;
}
public long save() {
return db.save(this.URL);
}
}
And then call it
Website w = new Website();
w.setUrl("http://www.stackoverflow.com");
long someLongValue = w.save();
Related
I create a model called Review. There are two activities (QualityReivewActivity.java & FairnessReivewActivity.java) will call the model whenever user leaves a comment.
I want to
public class Review {
float fairness_rating; //change the name to quality_rating
String post_id;
String review_time;
String reviewer_id;
String text_review;
public Review(){
//
}
Review(float fairness_rating, String post_id, String review_time, String reviewer_id, String text_review){
this.fairness_rating=fairness_rating;
this.post_id=post_id;
this.review_time=review_time;
this.reviewer_id=reviewer_id;
this.text_review=text_review;
}
public float getFairness_rating() {
return fairness_rating;
} //change to getQuality_rating if actitivty is QualityReivewActivity.java
public String getPost_id() {
return post_id;
}
public String getReview_time() {
return review_time;
}
public String getReviewer_id() {
return reviewer_id;
}
public String getText_review() {
return text_review;
}
}
and this is the segment of code of QualityReivewActivity
Review c = new Review(mRatingBar.getRating(), post_id, timedComment.toString(), reviewer_uid, my_comment.getText().toString()); //
However, the QualityReviewActivity always shows "fairness_rating". How can I make a dynamic model name to change to "quality_rating" if I am calling from QualityReviewActivity?
You can solve it by using enum.
public enum ratingType {
Quality,
Fairness
}
Then use that enum in your function.
public float get_rating(ratingType type) {
switch (type) {
case Quality:
return quality_rating;
case Fairness:
return fairness_rating;
}
}
After that, depending on the class, you can pass the necessary arguments and it will work.
I'm not good at Java, so it may not work if I write it as it is, but the idea is ok.
I'm trying to use these enums with driver.get but it output as invalid argument. How to resolve this issue?
Here is my code:
public enum Url_1{
URL_LOGIN("http://localhost/wordpress/wp-login.php"),
URL_DASHBOARD("http://localhost/wordpress/wp-admin/"),
URL_NEWPOST("http://localhost/wordpress/wp-admin/post-new.php"),
URL_EDIT("http://localhost/wordpress/wp-admin/edit.php");
public String url;
private Url_1(String env_url){
this.url = env_url;
}
public String getUrl(){
return url;
}
}
public void setUp() {
WebDriver driver = getDriver(Browser.CHROME);
driver.get(String.valueOf(Url_1.URL_LOGIN));
}
driver.get(Url_1.URL_LOGIN.getUrl())
You need to call the getter (public String getUrl()), because otherwise your output will be equal to the name of the enum constant (E.g.: URL_LOGIN, URL_DASHBOARD, etc.). getUrl will retrieve the contents held by those constants.
I have a class that holds contact data; wrapped in a respective class. I recently changed my Photo setup from being a simple byte[] to being a wrapped class as well, but the instantitaion is a little different and now won't serialize/wrap properly.
My other classes wrap properly such as "number":{"log.PhoneNumber":{"number":"123-456-7890"}} but if I feed in a new photo (ie: new Photo("DEADBEEF")) I just get "photo":"DEADBEEF". This is causing problems with the deserializer too.
public class ContactInfo {
#JsonProperty("name") private Name m_name = null;
#JsonProperty("number") private PhoneNumber m_number = null;
#JsonProperty("email") private Email m_email = null;
#JsonProperty("photo") private Photo m_photo = null;
#JsonCreator
public ContactInfo(#JsonProperty("name") Name name,
#JsonProperty("number") PhoneNumber number,
#JsonProperty("email") Email email,
#JsonProperty("photo") Photo photo) {
/** Set vars **/
}
#JsonTypeInfo(use=Id.CLASS, include=As.WRAPPER_OBJECT)
static public class Photo {
private byte[] m_decodedBase64 = null;
public Photo(byte[] encodedBase64) {
m_decodedBase64 = Base64.decodeBase64(encodedBase64);
}
#JsonCreator
public Photo(#JsonProperty("photoData")String encodedBase64) {
m_decodedBase64 = Base64.decodeBase64(encodedBase64);
}
#JsonProperty("photoData")
public String getEncodedPhoto() {
return Base64.encodeBase64String(m_decodedBase64);
}
public byte[] getDecodedData() {
return m_decodedBase64;
}
}
}
What am I doing wrong?
Just figured out what it was. In the ContactInfo class there was a simple accessor function to get the encodedData.
public String getPhoto() {
return m_photo.getEncodedPhoto();
}
By simple putting it on ignore (or simply change it to return the object itself, which I might do),
#JsonIgnore
public String getPhoto() {
return m_photo.getEncodedPhoto();
}
The serializer stopped trying to read from it. I wish there was a way to set the serializer engine to be more "explicit declaration" for properties instead of "serialize everything that seems to match the member variables."
import android.content.Context;
import android.content.SharedPreferences;
import android.net.Uri;
public class LoadSettings
{
public static void LoadMySettings (Context ctx)
{
SharedPreferences sharedPreferences = ctx.getSharedPreferences("MY_SHARED_PREF", 0);
String strSavedMem1 = sharedPreferences.getString("gSendTo", "");
String strSavedMem2 = sharedPreferences.getString("gInsertInto", "");
String cCalId = sharedPreferences.getString("gCalID", "");
setInsertIntoStr(strSavedMem2);
setSendToStr(strSavedMem1);
}
private static String cSendToStr;
private static String cInsertIntoStr;
private int cCalId;
private Uri cCalendars;
public String getSendToStr()
{
return this.cSendToStr;
}
public static void setSendToStr(String pSendToStr)
{
cSendToStr = pSendToStr;
}
public String getInsertIntoStr()
{
return this.cInsertIntoStr;
}
public static void setInsertIntoStr(String pInsertIntoStr)
{
cInsertIntoStr = pInsertIntoStr;
}
}
from the calling class i have tryed lots the current is.
LoadSettings.LoadMySettings(this);
but when i try to get some data for example.
textSavedMem1.setText(LoadSettings.getSendToStr());
i get a Null error.
LoadMySettings is not a class but a method (so it should start with a lower case, if you follow Oracle/Sun's naming conventions for the Java language).
You access it indeed by calling LoadSettings.loadMySettings(someContext), where someContext is the context to pass around. In your example, we don't know what this refers to, so maybe your error lies there.
Then when you do this: textSavedMem1.setText(LoadSettings.getSendToStr());
You call a non-static method, so that should be either using an instance of LoadSettings or, more likely considering your code, you could change getSendToStr to be:
public static String getSendToStr()
{
return cSendToStr;
}
Though that seems to be rather bad design.
Maybe if you tell us more about what you try to do, we can help more, as as such our answers will just take you one step further.
EDIT: Ok, I just figured out what you are trying to do...
You need to go back and learn basic Java concepts and read on access modifiers, and constructors first, and OO semantics in Java in general.
Change your class to this:
public class LoadSettings
{
public LoadSettings(Context ctx)
{
SharedPreferences sharedPreferences =
ctx.getSharedPreferences("MY_SHARED_PREF", 0);
String strSavedMem1 = sharedPreferences.getString("gSendTo", "");
String strSavedMem2 = sharedPreferences.getString("gInsertInto", "");
String cCalId = sharedPreferences.getString("gCalID", "");
setInsertIntoStr(strSavedMem2);
setSendToStr(strSavedMem1);
}
private String cSendToStr;
private String cInsertIntoStr;
private int cCalId;
private Uri cCalendars;
public String getSendToStr()
{
return cSendToStr;
}
public void setSendToStr(String pSendToStr)
{
cSendToStr = pSendToStr;
}
public String getInsertIntoStr()
{
return cInsertIntoStr;
}
public void setInsertIntoStr(String pInsertIntoStr)
{
cInsertIntoStr = pInsertIntoStr;
}
}
And create a new instance of LoadSettings with:
LoadSettings mySettings = new LoadSettings(someContext);
You can then correctly invoke:
textSavedMem1.setText(mySettings.getSendToStr());
Haylem has the right of it, but I just wanted to add a comment:
There are basically two design patterns in Java for what you're trying to do. One is the static class where all the methods and variables are static and you access them as e.g.
LoadSettings.loadMySettings(this);
string = LoadSettings.getSendToStr()
// etc.
The other pattern is the "singleton" class where you create exactly one instance of the class and you access the instance:
LoadSettings ls = new LoadSettings(this);
ls.loadMySettings();
string = ls.getSendToStr();
Either way is good, but what you're doing is a mish-mash of both methods and it won't work.
So i'm a little bit confused as i've never used an enum before. I want to use this enum in my main method. For some reason, i can't (i keep getting errors anytime i even try to do Status s; in main). I can however call my TestingEnum method from main and of course this works... but i am 100% sure that using the enum this way is just plain wrong. Could someone tell me how i'd go about using this in main properly?
If i try to do: Status s; in my main method, i get this error - "connot find symbol Status s;"
BACKGROUND: new to java and enums...
class MyClass {
public Status s;
public enum Status {
STATUS_OPEN(1),
STATUS_STARTED(2),
STATUS_INPROGRESS(3),
STATUS_ONHOLD(4),
STATUS_COMPLETED(5),
STATUS_CLOSED(6);
private final int status;
Status(int stat) {
this.status = stat;
}
public int getStatus() {
return this.status;
}
}
private void setStatus(Status stat) {
s = stat;
}
public void TestingEnum() {
Status myStat = Status.STATUS_ONHOLD;
setStatus(myStat);
}
#Override
public String toString() {
StringBuilder result = new StringBuilder();
String NEW_LINE = System.getProperty("line.separator");
result.append(NEW_LINE + " Status: " + s + NEW_LINE);
return result.toString();
}
}
public class Main {
public static void main(String[] args) {
MyClass obj = new MyClass();
// PROBLEM SETTING STATUS HERE
// I can't do this:
Status s;
}
}
Move the enum to its own class file, or access it with a reference to the enclosing class.
It looks like you defined the Enum as an inner class of another class. If you're doing this, you need to access it with the syntax OuterClass.Status to access it. You made it public, so that will work. You can access it from within the class with no problem because it's contained in the scope of the parent class.
So you can either add the OuterClass. before Status, or you can move the Enum into its own file like any other class.
From the limited code I think the problem is that you try to access
public Status s;
which is not static from the static method main
public static void main(String[] args) {
...
}
Create a instance of your class from main and have a method on that instance use s or declare s as static.
You cannot just instantiate an Enum just before a public class like that. One way to resolve the issues is to have an outer class which will have your Enum class as inner class like this:
public class MyStatus {
static enum Status {
STATUS_OPEN(1),
STATUS_STARTED(2),
STATUS_INPROGRESS(3),
STATUS_ONHOLD(4),
STATUS_COMPLETED(5),
STATUS_CLOSED(6),
ABANDONED(7);
private final int status;
Status(int stat) {
this.status = stat;
}
public int getStatus() {
return this.status;
}
}
private Status s;
public void setStatus(Status stat) {
s = stat;
}
public void TestingEnum() {
Status myStat = Status.ABANDONED;
setStatus(myStat);
}
#Override
public String toString() {
StringBuilder result = new StringBuilder();
String NEW_LINE = System.getProperty("line.separator");
result.append(NEW_LINE + " Status: " + s + NEW_LINE);
return result.toString();
}
}
Also ABANDONED wasn't defined so I just added it in the end.
Based on your edit. The problem you have is the name of the class is nested and called MyClass.Status
Try
public static void main(String[] args) {
MyClass obj = new MyClass();
//PROBLEM SETTING STATUS HERE
//I can't do this:
MyClass.Status s = MyClass.Status.STATUS_OPEN;
}
BTW: Your IDE should be able to auto fix this mistake.
The only compilation error I see is that you have used
Status myStat = Status.ABANDONED;
without defining it. I suggest you add this enum or use one you have defined.
I resolved this error by creating a nonstatic enum as outside the class.
public enum Status {
STATUS_OPEN(1),
STATUS_STARTED(2),
STATUS_INPROGRESS(3),
STATUS_ONHOLD(4),
STATUS_COMPLETED(5),
STATUS_CLOSED(6),
ABANDONED(7);
private final int status;
Status(int stat) {
this.status = stat;
}
public int getStatus() {
return this.status;
}
}
Created MyStatus class without inner enum block:
public class MyStatus {
private Status s;
public void setStatus(Status stat) {
s = stat;
}
public void TestingEnum() {
Status myStat = Status.ABANDONED;
setStatus(myStat);
}
#Override
public String toString() {
StringBuilder result = new StringBuilder();
String NEW_LINE = System.getProperty("line.separator");
result.append(NEW_LINE + " Status: " + s + NEW_LINE);
return result.toString();
}
}
Resolved for me.
You would not be able to refer to Status in main() because, while Status is public, it is not static. You would need to create an instance of your enclosing class and then use it to create an instance of the enum:
MyEnclosingClass clz = new MyEnclosingClass();
Status status = clz.new Status();
This should resolve the error I believe you are probably getting.