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.
Related
I created a simple little directory listing method in the default MainActivity.class
I was able to get it to function the way I wanted it to, however, when I moved the method to a different class and called it in MainActivity, I wound up getting a lot of Context Errors all over the place. After searching the web I am stumped and require assistance.
The code where it errors out is for the Context for FileArray:
(public class Utilities extends Activity)
arrayAdapter = new FileArrayAdapter(getApplicationContext(),R.layout.custom_explorer,dir);
Method Name:
public void listDirectories(ListView listView, File directory)
It errors out on the getApplicationContext, this method worked just fine in MainActivity.
I have not implemented Fragments to MainActivity yet, wanted to get the working functionality first then spread out for OOP; then call by Fragment.
Hope someone can help, any further info I am happy to share.
Thank you for the timely response: ρяσѕρєя K & Nilabja.
Ya I guess it just needed another person to suggest feeding Context as a param. I did that once but it did not work so I passed it off, attempted once again from Nilabja suggestion. Yip that was the solution.
utilities.ListDirectory(mainList, root, getApplicationContext());
public void listDIR(ListView listView, File directory, Context setContext)
I have 15 or so activities. Each one of them has a method, and I want to play audio in that method. Now, I have the obvious option to copy and paste the following line of code into each and every one of my activities Now, if I wanted to change something, I would have to go back into each and every one of my activities again. Here is the code:
MediaPlayer pop = MediaPlayer.create(CurrentActivity.this, R.raw.pop);
pop.start();
So, after searching the web for a few hours, I found that most people would just copy and paste it into each activity. So, I put the line of code (above) into a separate java class (which was a service by the way) tried to call that method in the service every time I needed to play the audio. So, I did the following:
public class TwentySeconds extends Service{
public void myPop(View view){
MediaPlayer pop = MediaPlayer.create(TwentySeconds.this, R.raw.pop);
pop.start();
}
}
Now, I got the error non static method cannot be referenced from static context. So, naturally, I tried to make method myPop static. Then, I got the error on TwentySeconds.this about being referenced from static context. So, it seems I am stuck. Changing the methods to static can't work, as I am trying to use an instance of the class as well using this. So, how should I go about calling method myPop where the MediaPlayer can successfully play?
Thanks for the advice,
Rich
Typically, if a utility method needs a Context, it is passed in.
public class Utilities {
public static void myPop(Context context){
MediaPlayer pop = MediaPlayer.create(context, R.raw.pop);
pop.start();
}
}
Utilities.myPop(CurrentActivity.this);
yea, i know, another thread about changing icons in osmdroid.
But there isn't any good explanation available !
I made lot of research and it is a frequently asked topic. Why not make a clear answer at this point for all user who need to specify their own resources ?
All stuff available is to "Have a look at ResourceProxy.java" or "Pass the bitmap to constructor". No way, it doesn't work for me, I don't even know how a simple interface could find my drawable in my res folder ! I tried to pass some .PNG files with the same names as osmdroid (like "person.png") but when I run the app I have still the default resource.
Can someone explain clearly how to write "our own ResourceProxy" step by step ? Not brievely like that because I followed the steps and didn't manage to success.
I know that
osmdroid was fixed recently in github but the .aar is older than the fix. Here you can find the
Resource Proxy class.
When you create in an instance of the MapView, you need to pass in a reference to a implementation of "ResourceProxy". By default, it loads the "DefaultResourceProxyImpl". To override, create a new class that extends DefaultResourceProxyImpl and then you can override getBitmap and getDrawable. There is actually is an example that overrides the DefaultResourceProxyImpl here:
https://github.com/osmdroid/osmdroid/blob/master/OpenStreetMapViewer/src/main/java/org/osmdroid/ResourceProxyImpl.java
Just for you, I'm working on including such an example with the osmdroid example app. It's going to look something like this
public class CustomResourceProxy extends DefaultResourceProxyImpl {
private final Context mContext;
public CustomResourceProxy(Context pContext) {
super(pContext);
mContext = pContext;
}
#Override
public Bitmap getBitmap(final bitmap pResId) {
switch (pResId){
case person:
//your image goes here!!!
return BitmapFactory.decodeResource(mContext.getResources(),org.osmdroid.example.R.drawable.sfgpuci);
}
return super.getBitmap(pResId);
}
#Override
public Drawable getDrawable(final bitmap pResId) {
switch (pResId){
case person:
//your image goes here!!!
return mContext.getResources().getDrawable(org.osmdroid.example.R.drawable.sfgpuci);
}
return super.getDrawable(pResId);
}
}
Edit: done
See https://github.com/osmdroid/osmdroid/blob/master/OpenStreetMapViewer/src/main/java/org/osmdroid/CustomResourceProxy.java
Edit: Edit:
Wiki updated, see
https://github.com/osmdroid/osmdroid/wiki/How-to-use-the-osmdroid-library
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.
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.