Like lots of askers on SO, I'm relatively new to java and have attempted to teach myself android programming with some decent success. I'm sure this is trivial to someone with actual knowledge on the subject. I'm working on a app that attempts to fetch data from the net and 'returns true' if you get the data and 'returns false' if it doesn't. I want to do something when it returns false but can't figure out how to properly handle the response. Right now, I just ignore the response an do nothing. Any help?
public void onBackPressed() {
Someclass.getinfo().maybeShowInfo(this);
finish();
}
What I would like to do is something like (in pseudo code)
public void onBackPressed() {
Someclass.getinfo().maybeShowInfo(this);
// if false is returned
// do something
// else
// finish();
}
public void onBackPressed() {
boolean result = Someclass.getinfo().maybeShowInfo(this);
if (result) {
finish();
} else {
// do something else
}
}
It looks to me like you've combined two things that must be separate. Make fetching the data and displaying two methods, by two classes.
private InfoDao infoDao; // This is a class that gets the data; it's a member of the class with the onBackPressed() method
public void onBackPressed() {
Info info = this.infoDao.find();
if (info != null) {
displayInfo();
}
}
public void onBackPressed()
{
boolean result = Someclass.getinfo().maybeShowInfo(this);
if (result = false)
{
//do work for false response;
}
else
{
finish();
}
}
don't forget that you have to make your Someclass.getinfo() return true if it succeded and false if it didn't.
Related
I have been trying to figure out a way to create a boolean to determine which biome the user chose based on their input using a scanner. But I have not been able to find a way to implement it. I have tried to use boolean return methods but I kept on getting null or the exact opposite biome I was trying to go for.
File: game.java
public boolean isPlains() {
if (biome == "Plains") {
return true;
} else {
return false;
}
}
public boolean isTundra() {
if (biome == "Tundra") {
return true;
} else {
return false;
}
}
File: phase.java
public class phase extends game{
public void battle() {
game gm = new game();
if (isPlains() == true) {
System.out.println("Hello");
} else {
System.out.println("Bye");
}
}
}
Above is the test run I've tried to run, but whenever I try and run the code it seems to output the opposite I ask it to. I tried flipping it around but it seems to do the same thing. I am trying to use the isPlains/isTundra methods from game.java to determine whether to print hello or bye from phase.java.
I am working on CS50 Android track Fiftygram app. My code is working but I don't like to see copy pasted codes.
Currently checking dropdown selected item name and using if/else if to call with representing instance of a Transformation. How can I directly call apply function with the string without using all these if and else ifs.
If I can find a way, I can fix some of the wording. For example, I can get rid of Transformation end of the strings and add it myself before calling the function.
public void applyFilter(View view) {
if (filterList.getSelectedItem().toString() != null) {
if (filterList.getSelectedItem().toString().equals("ToonFilterTransformation")) {
apply(new ToonFilterTransformation());
}
else if (filterList.getSelectedItem().toString().equals("SepiaFilterTransformation")) {
apply(new SepiaFilterTransformation());
}
else if (filterList.getSelectedItem().toString().equals("ContrastFilterTransformation")) {
apply(new ContrastFilterTransformation());
}
else if (filterList.getSelectedItem().toString().equals("InvertFilterTransformation")) {
apply(new InvertFilterTransformation());
}
else if (filterList.getSelectedItem().toString().equals("PixelationFilterTransformation")) {
apply(new PixelationFilterTransformation());
}
else if (filterList.getSelectedItem().toString().equals("SketchFilterTransformation")) {
apply(new SketchFilterTransformation());
}
else if (filterList.getSelectedItem().toString().equals("SwirlFilterTransformation")) {
apply(new SwirlFilterTransformation());
}
else if (filterList.getSelectedItem().toString().equals("KuwaharaFilterTransformation")) {
apply(new KuwaharaFilterTransformation());
}
else if (filterList.getSelectedItem().toString().equals("VignetteFilterTransformation")) {
apply(new VignetteFilterTransformation());
}
}
}
public void apply(Transformation<Bitmap> filter) {
if (original != null) {
Glide
.with(this)
.load(original)
.apply(RequestOptions.bitmapTransform(filter))
.into(imageView);
}
}
I suggest you to create a method and make filterList.getSelectedItem().toString() into a variable. Then make use of reflection in creating a class
something like
String item = filterList.getSelectedItem().toString();
apply(createInstance(item))
public Object createInstance(String item){
Class classDefinition = Class.forName(item);
return classDefinition.newInstance();
}
But you have to add further validations there if the item exists or not.
But the idea is like that. Take advantage of reflection instead of using new keyword
I tried disable button when some integer value != 1
For example (my idOrder is IntegerProperty)
refreshButton.disableProperty().bind(new BooleanBinding() {
#Override
protected boolean computeValue() {
return currentOrder.getIdOrder() != 1;
}
});
And it's works. But when I changed value on 1 (currentOrder.setIdOrder(1)) button is still disabled.
What I doing wrong?
You've created a BooleanBinding but haven't configured it to observe anything, thus it will never be notified of your property changing. You need to invoke BooleanBinding#bind(Observable...) during instantiation. For example:
refreshButton.disableProperty().bind(new BooleanBinding() {
{
bind(currentOrder.idOrderProperty());
}
#Override protected boolean computeValue() {
return currentOrder.getIdOrder() != 1;
}
#Override public void dispose() {
// for a proper implementation, we need this as well
unbind(currentOrder.idOrderProperty());
}
});
That said, the above can be simplified with Bindings#createBooleanBinding(Callable,Observable...):
refreshButton.disableProperty()
.bind(Bindings.createBooleanBinding(
() -> currentOrder.getIdOrder() != 1, currentOrder.idOrderProperty()));
But even that can be simplified further with one of the following:
Bindings#notEqual(int,ObservableNumberValue):
refreshButton.disableProperty().bind(Bindings.notEqual(1, currentOrder.idOrderProperty());
NumberExpresion#isNotEqualTo(int):
refreshButton.disableProperty().bind(currentOrder.idOrderProperty().isNotEqualTo(1));
I've a simple inheritance problem.. but I can't solve it.
This is the basic class:
public abstract class RpcOkCallback extends RpcTupleCallback
{
// [...] constructor [...]
public boolean callback(int responseCode, final String module, boolean flag){
if (flag){
return onResponse(responseCode, module);
} else {
return onError(responseCode, module);
}
return false;
}
protected abstract boolean onResponse(int responseCode, String module);
protected boolean onError(int responseCode, String module){
return true;
}
}
And this is an anonymous class that redefine the base class:
new RpcOkCallback("color_seek_ir", "set_flash_ir"){
#Override
protected boolean onResponse(int responseCode, String module) {
if (seekBar != null) seekBar.setEnabled(true);
return true;
}
#Override
protected boolean onError(int responseCode, String module) {
if (seekBar != null) seekBar.setEnabled(true);
return true;
}
}
The question is.. why when the onResponse method is called it calls correctly the overridden method while when it calls the onError method, it calls the base case (the "return true" method)? I've tried to declare abstract the "return error" method too and it works... but I don't want to declare in every anonym class a basic method like that.
Any idea? Thanks :)
I found the problem. It actually works correctly (the overriding part), but the callback was already handled by another "onError" method (not overridden) and because of the "return true" the event was canceled before reaching the final destination :(
Adding the abstract and defining all the methods did the job, so thanks corsair (if you want to add your answer or what I will mark as solved the question by you :) ).
I currently have code to share a variable between two entry points in my application. The variable is the iconCount variable used to indicate how many notices the user has which is displayed on the home screen beside the icon. The way I've managed to do this is with a singleton and it (seems) to work fine at the moment. The issue is now that I do not want those notices to reset to zero when I completely turn off and turn on the phone. Should there be 7 notifications, I want there to be 7 notifications even after a device restart. For this I apparently need a persistent store integration which I've researched for a while.
So far my code for the bare singleton is:
public class MyAppIndicator{
public ApplicationIndicator _indicator;
public static MyAppIndicator _instance;
MyAppIndicator () {
setupIndicator();
}
public static MyAppIndicator getInstance() {
if (_instance == null) {
_instance = new MyAppIndicator ();
}
return(_instance);
}
public void setupIndicator() {
//Setup notification
if (_indicator == null) {
ApplicationIndicatorRegistry reg = ApplicationIndicatorRegistry.getInstance();
_indicator = reg.getApplicationIndicator();
if(_indicator == null) {
ApplicationIcon icon = new ApplicationIcon(EncodedImage.getEncodedImageResource ("notificationsdemo_jde.png"));
_indicator = reg.register(icon, false, true);
_indicator.setValue(0);
_indicator.setVisible(false);
}
}
}
public void setVisible1(boolean visible, int count) {
if (_indicator != null) {
if (visible) {
_indicator.setVisible(true);
_indicator.setValue(count); //UserInterface.incrementCount()
} else {
_indicator.setVisible(false);
}
}
}
}
I have been using the blackberry tutorial to figure out how to implement the persistable storage: http://supportforums.blackberry.com/t5/Java-Development/Storing-persistent-data/ta-p/442747
Now before I go any further I must stress I'm very new to java development so my coding might be completely wrong, but here is what I've tried to do:
public void setVisible1(boolean visible, int count) {
if (_indicator != null) {
if (visible) {
_indicator.setVisible(true);
_indicator.setValue(count); //UserInterface.incrementCount()
StoreInfo info = new StoreInfo();
info.incElement();
synchronized (persistentCount) {
//persistentCount.setContents(_data);
persistentCount.commit();
}
} else {
_indicator.setVisible(false);
}
}
}
static {
persistentCount = PersistentStore.getPersistentObject(0xdec6a67096f833cL);
synchronized (persistentCount) {
if (persistentCount.getContents() == null) {
persistentCount.setContents(new Vector()); //don't know what to do with this?
persistentCount.commit();
}
}
}
private static final class StoreInfo implements Persistable{
private int iconCount;
public StoreInfo(){}
public int getElement(){
return (int)iconCount;
}
public void incElement(){
iconCount++; //persistently increment icon variable
}
public void resetElement(){
iconCount=0; //when user checks application
}
}
The code above doesn't work which I'd expect somehow because I'm having trouble implementing the persistent portion. If anyone has any idea or input on how to accomplish this any assistance would be helpful. And of course thanks in advance.
In the example they have a variable called _data that holds the StoreInfo class, so first of all you should be keeping the StoreInfo in some variable. To do this have something like the following in your static initializer:
persistentCount = PersistentStore.getPersistentObject(0xdec6a67096f833cL);
synchronized (persistentCount) {
if (persistentCount.getContents() == null) {
persistentCount.setContents(new StoreInfo());
persistentCount.commit();
}
}
_data = (StoreInfo)persistentCount.getContents();
Now when you want to update it and save to the PersistentStore you can have something like:
_data.incElement();
synchronized(persistentCount) {
persistentCount.setContents(_data);
persistentCount.commit();
}
Assuming you're going to only ever have one instance of StoreInfo it could be better to put the commit code into the modifier methods so you don't forget to save the new values to the PersistentStore.