How to reference another java file from within Eclipse - java

Inside my GameActivity.java I have a link to a website, which is under a
public void gotoWebsite() {
//link to site here
}
however, I would like to refer to this from a TitleLayer.java class file.
For example, on that TitleLayer.java, there is a button, which is also a public void, how can I place a link to refer to the 'gotoWebsite' link that already exists in GameActivity.java?

There is a few ways:
1 - Add a Constructor to GameActivity and create a new instance of GameActivity inside TittleLayer or pass the GameActivity object to your TitleLayer class.
2 - Make the goToWebsite static and reference it using GameActivity.goToWebsite() from any other file.

Related

How to correctly implement stfalcon's chatkit

I'm trying to implement a chat app using stfalcon's ChatKit library. I've followed the docs in their repo, but there are things I'm not sure I'm getting right.
First, I created a new activity called DialogsListActivity, and copied the xml in the activity's xml file.
From here I first copied the xml part to the activity's xml file.
Next comes the adapter setup. I copied the given code after the OnCreate method, including the last line (dialogsListView.setAdapter(dialogsListAdapter);) as the last line in OnCreate. The whole activity now looks like this:
ListView dialogsListView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dialogs_list);
dialogsListView.setAdapter(dialogsListAdapter);
}
DialogsListAdapter dialogsListAdapter = new DialogsListAdapter<>(dialogs, new ImageLoader() {
#Override
public void loadImage(ImageView imageView, String url) {
//If you using another library - write here your way to load image
Picasso.with(DialogsListActivity.this).load(url).into(imageView);
}
});
}
Questions:
is this the right place to put the adapter in?
is it ok to put set the dialogsListView as an attribute and defining it inside OnCreate()?
the dialogs from new DialogsListAdapter gets a Cannot resolve symbol 'dialogs' message.
the new ImageLoader() from same place gets a Class 'Anonymous class derived from ImageLoader' must either be declared abstract or implement abstract method 'loadImage(ImageView, String, Object)' in 'ImageLoader'
What am I missing there?
For the IDialog and IUser implementation I created the classes DefaultDialog and Author, and copied the given code. As I expected, the 'symbols' returned by the methods 'cannot be defined'. Where should they be defined and how?
Next in the tutorial is the Data management section which I think would set those values.
I already downloaded the sample project and tried to look inside, but I cannot find the public class DefaultDialog that implements IDialog or anything similar. Plus, I got pretty lost trying to understand the library from that sample project.
Any help would be much appreciated.
Thanks in advance.

Android Interface Implementation Error for Network Fragment

I wanted to make a network fragment on my Android app so I could upload and download information from my database server. Following the guide on Android networking on the Developer page and the corresponding example project on Github, I created a demo to test a network connection.
I copied the files DowloadCallback.java (contains the implemented network interface) and NetworkFragment.java (the network fragment thread) word for word from the example project and added the necessary permissions in AndroidManifest.xml.
When I tried to implement the fragment into my activity, I got errors in several rather odd and counterintuitive places:
Code
public class MainActivity extends FragmentActivity implements DownloadCallback {
...
#Override
public void updateFromDownload(String result) {
...
}
}
Errors
Class 'MainActivity' must either be declared in abstract or implement method 'updateFromDownload(T)' in 'DownloadCallback'
Method does not override method from its superclass
The public class says it needs a particular method for the class to implement DownloadCallback, but when I add such method it says that it does not exist in its superclass. How can these errors coexist? How can I fix this?
By the way, this is the exact same way the main activity class is defined in the sample project. Also I have posted this as an issue on Github but I am hoping to get a quicker response and attention here.
base on this You have to define <T> for DownloadCallback
In your case T is String
So change your code like below
public class MainActivity extends FragmentActivity implements DownloadCallback<String>

How to create a .txt file with the name of the class that called the method?

I am new to programming, so let's see if I can explain this well enough.
I am making a Java package. In one of the classes there is a method that creates a file. The name of that file I have it set up as "file.txt", but I want to change it.
Let's say there is a user working on a new project and he imports the library package (the one I am working on). I want for the file that is created to take the name of the class in which the user is working on. For example if the user calls it in a class named Main, I want the file to be called main.txt or Main.txt.
If this is not clear enough please let me know, I'll try to explain it better.
Thanks
Edit: I've tried the getClass().getSimpleName() but it's not working exactly like I want it to. The method is located inside a library called library and the class is called Main.class but is being used by a use that imported the library library and is working on a class called SuperMario.class I want the text file to be called SuperMario.txt instead with getClass().getSimpleName() applied to my method the file will be called Main.txt, because that is the name of the class the method is in. Unfortunately I can't have the method pass the name as a parameter. Can anyone think of a way around this?
In your library, use stacktrace :
public class Called {
public static void calledMethod() {
System.out.println(Thread.currentThread().getStackTrace()[2].getClassName());
// stackTrace 0 is get stack trace.
// stack trace 1 is calledMethod
// stack trace 2 is the calling method aka main in
}
}
The result will be
eu.plop.test.TestClass
You had to search after stacktrace to see if you want the filename, class name, method's name... and then some string works to remove the package if unused.

GWT: How to Use Place Tokens

As a little example: Take a website where you land on a login page and after a successful login getting directed to the main page where on the left side you'd have a menu and on the right side a content are where everything gets loaded when clicking a link in the menu. In the menu I'd have multiple stores where each store would have its own settings. Those settings would be initialized with an id to determine which content the view should display.
For the history mapper I guess one would have something like this:
/#LoginPlace:login
/#MainPlace:home
/#MainPlace:storeSettings?id=<id>
/#MainPlace:userSettings
etc..
In [2] it says
"In order to be accessible via a URL, an Activity needs a corresponding Place."
Which sounds to me like I should have a LoginActivity and a MainActiviy since after all LoginActivity is the first Place I arrive when I come to my website and MainActivity is the place I go when I am successfully logged in.
I'd have someting like this:
public class LoginActivity extends AbstractActivity implements LoginView.Presenter {
// ..
private void onLoginSuccess() {
this.clientFactory.getPlaceController().goTo(new MainPlace());
}
}
public class MainActivity extends AbstractActivity implements MainView.Presenter {
// ..
}
and of course have respective view LoginView and MainView.
This is how AppActivityMapper.getActivity() would look like:
#Override
public Activity getActivity(Place place) {
if (place instanceof LoginPlace) {
return new LoginActivity((LoginPlace) place, clientFactory);
} else if (place instanceof MainPlace) {
return new MainActivity((MainPlace) place, clientFactory);
}
return null;
}
So far so good but how would I implement the MenuView and the MainContentView?
I want to be able to click on menu items in the MenuView and update MainContentView and generate place tokens accordingly like:
/#MainPlace:home
/#MainPlace:storeSettings?id=<id>
/#MainPlace:userSettings
But I have no idea how to do that and how I would for example initialize my activity StoreSettingsActivity with the given id. I believe that I would need another MainActivityMapper extends ActivityMapper that now should control the place changes initiated by clicking on menu links but I just can't figure out how that could work.
I hope my question is clear, please let me know if you need some more information.
I think I once did something similar, it was not perfect because I had to deal with an existing architecture but it worked.
Here is what I did:
When a link from the menu is click execute placeController.goTo(new MainPlace(id));
In your ActivityMapper, return your MainActivity in which you set the id of the MainPlace.
In your MainActivity, in the start method call a method that will initiate your MainContentView depending on the id of MainPlace you set earlier in the MainActivity.
I am pretty sure there is a better way of doing this, but it works and it can be a start that might help you to find a better solution.

Generic classes in my application

I've created a few minor apps for Android while learning. Being a PHP developer, it's a challenge to get used to it.
I'm especially wondering how I could define a couple of "general" functions in a separate class. Eg I have a function that checks if network connection is available, and if not, shows a dialog saying that the user should enable it. Currently, that function exists in several of my activities. Of course that seems strange - I suppose it would be more logical to define it once and include it in the activites where needed.
I tried putting it in a new class, and included that class in the original activity. But that failed since eg getBaseContext() is not accepted anymore.
I'm wondering how to go ahead. What should I be Google-ing for ? What is this mechanism called?
You need to create class with static methods. Like this
public class HelperUtils {
public static void checkNetworkConnection(Context ctx) {...}
}
Then you can call it from any place like this:
HelperUtils.checkNetworkConnection(this.getContext());
Assuming current class has Context.
You should read books on general OOP concepts where different type of methods are explained.
You can for example create a class - let's call it NetworkUtils. In this class you can create static method boolean isNetworkConnectionAvailable() and return true if is available and false otherwise. In this class you can create another static method void showNoConnectionDialog(Activity activity) - and in this method you create dialog starting with
public static void showNoConnectionDialog(Activity activity) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
//setting message, listener etc. and finally
builder.create().show();
}
In your activity, where you want to check and handle network connection you should call:
if (!NetworkUtils.isConnectionAvailable(getApplicationContext())) {
NetworkUtils.showNoConnectionDialog(YourActivityClassName.this)
}
I guess this should work.

Categories

Resources