how does implement multiple types asynctaskloader - android

I want to use two type asynctaskloader in one FragmentActivity.
class MyLoader1 extends AsyncTaskLoader<String>{}
class MyLoader2 extends AsyncTaskLoader<Integer>{}
I write as follows. but it compile error.
public class MyActivity extends FragmentActivity
implements LoaderCallbacks<String>, LoaderCallbacks<Integer>{}
Please show me answer with easy sample code.
Thanks so much.

As hjpotter92 mentions, this is how Java handles generics. In this case, I would just suggest using anonymous classes as indicated in hjpotter92's link.
public class MyActivity extends FragmentActivity {
private LoaderCallbacks<String> mLoaderCallbackString = new LoaderCallbacks<String>() {
...
};
private LoaderCallbacks<Integer> mLoaderCallbackInteger = new LoaderCallbacks<Integer>() {
...
};
}
Then for each loader, you just pass the correct LoaderCallbacks object

Related

Use different (AsyncTask) Loaders in one Activity

I want to use different AsyncTaskLoaders (different in their return type) in my Activity, what's the best way to implement the callback methods?
This won't work:
public class MyActivity extends Activity implements
LoaderManager.LoaderCallbacks<MyPojo>,
LoaderManager.LoaderCallbacks<MyOtherPojo>
Eclipse says
The interface LoaderCallbacks cannot be implemented more than once with different arguments
So what do I do? My idea is to make the Activity
implements LoaderManager.LoaderCallbacks<Object>
then check in the callback methods what type of object it is but that doesn't seem too elegant. Is there a better way?
What about creating an inner class for each callback?
public class MyClass extends Activity {
private class Callback1 implements LoaderManager.LoaderCallbacks<MyPojo> {
...
}
private class Callback2 implements LoaderManager.LoaderCallbacks<MyOtherPojo> {
...
}
}

Android generic ArrayAdapter

I have multiple classes, which all extend some BasicDataModel:
public class NewsItem extends BasicDataModel
public class PhotosItem extends BasicDataModel
etc. So I need to make ArrayAdapters for all of them, and I wonder if it is possible to make some generic ArrayAdapter to fit all my objects. So far I tried to make
public class MyAdapter extends ArrayAdapter<BasicDataModel>
but as long as I pass ArrayList, for example, this doesn't work.
Thanks in advance
Try
public class MyAdapter<T extends BasicDataModel> extends ArrayAdapter<T>
and use
MyAdapter<NewsItem> news;
I tried Nikita Beloglazov suggestion:
public class MyAdapter<T> extends ArrayAdapter<T extends BasicDataModel>
but found that it wouldn't compile.
Then I tried:
public class MyAdapter<T extends BasicDataModel> extends ArrayAdapter<T>
and that worked just fine :)
Try the PIArrayAdapter.
reduce your code size and headache of maintaining it again.
just plug and play with simple annotations
pull it #
https://github.com/peeyush18/CustomAdapter.git
or view it at
https://github.com/peeyush18/CustomAdapter

Common Code for Activity and PreferenceActivity

I'm using common code in my Activity like this:
abstract class CommonCode extends Activity {
//Common Code here...
}
then in my "Activity" I extend CommonCode instead of Activity and it all works fine.
My problem arise when I try to use commoncode in a PreferenceActivity, I tried:
abstract class CommonCode extends Activity {
class CommonCodePreferences extends PreferenceActivity {
}
//Common Code here...
}
but it isn't right.
How can I do it?
May I suggest that you prefer composition over inheritance and do something like this:
abstract class CommonCode {
Activity parent;
public CommonCode(Activity activity) {
parent = activity;
}
}
class MyActivity extends Activity {
CommonCode commonCode;
public MyActivity() {
commonCode = new CommonCode(this);
}
}
This is a little more code to write in each activity, but it has a lot of advantages:
It can also easily handle PreferenceActivity and other classes
It is easier to test and mock
I usually have one each since you can't mess with the existing hierarchy of the base classes.
For example, I have an ActivityBase, ServiceBase, ListActivityBase, etc. If you want to have common code that they all use, I would suggest using composition - each of your base classes has a single instance of your CommonCode class or something to that effect. Another possibility is to use static methods and/or use a custom Application class (requires declaring the custom Application class in the manifest in the name attribute of the application element)

how to call method in service in android

I have created a service for TweetCollectorService. I want to call a method of another class in my service.can I do this Plz Help me.
Thankyou
Yes you can.. Only difference here is the method execution also occurs in background process.. No other difference..
Its not best practice to have other utility functions/methods in Activity, which mainly is to handle user interaction. so Strictly follow java convention and create different class which has all these methods, so it achieves cohesion.
You can dosomething like below..
Class YourActivity extends Activity{
public void do(){
// do your task
}
and in service just say new YourActivity().do()
}
Yes you can able to call the other class method. If you using your custom class then create the method as static so no need to create object of that class
suppose your custom class which extend Activity or not then also you can do like this way.
class CustomClass extends Activity{
public static void mymethod(){
// call me
}
}
now you can call into the service like this way without creating any object as explicitly
class MyService extends Service{
onCreate(){
CustomClass.mymethod();
}
}

Android performance testing using "android.test.PerformanceTestCase" interface

I am trying to test performance of an android application using "android.test.PerformanceTestCase" interface , can anyone tell me how to make use of it...
I know i can use Traceview tool to test performance but i want learn to use "android.test.PerformanceTestCase" interface provided by android.
If i get any example code it will be very much helpful
Download the Android source code and look in frameworks/base/tests/AndroidTests/src/com/android/unit_tests. You'll find a lot of PerformanceTestCase examples.
I cloned the android source looking for the files suggested by Mirko's answer. The repository is very large and took me a while to clone. I searched it for all instances of PerformanceTestCase and found the following files which implement it:
/base/graphics/tests/graphicstests/src/android/graphics
/GraphicsPerformanceTests.java: implements PerformanceTestCase {
/base/test-runner/src/android/test
/TestRunner.java:public class TestRunner implements PerformanceTestCase.Intermediates
/base/core/tests/coretests/src/android/app/activity
/ActivityTestsBase.java: implements PerformanceTestCase, LaunchpadActivity.CallingTest {
/base/core/tests/coretests/src/android/util
/LogTest.java: public static class PerformanceTest extends TestCase implements PerformanceTestCase {
/base/core/tests/coretests/src/android/database
/CursorWindowTest.java:public class CursorWindowTest extends TestCase implements PerformanceTestCase {
/DatabasePerformanceTests.java: public static class ContactReadingTest1 implements TestCase, PerformanceTestCase {
/DatabaseStatementTest.java:public class DatabaseStatementTest extends AndroidTestCase implements PerformanceTestCase {
/DatabaseGeneralTest.java:public class DatabaseGeneralTest extends AndroidTestCase implements PerformanceTestCase {
/NewDatabasePerformanceTests.java: implements PerformanceTestCase {
/DatabaseCursorTest.java:public class DatabaseCursorTest extends AndroidTestCase implements PerformanceTestCase {
/base/core/tests/coretests/src/android/view
/CreateViewTest.java:public class CreateViewTest extends AndroidTestCase implements PerformanceTestCase {
/InflateTest.java:public class InflateTest extends AndroidTestCase implements PerformanceTestCase {
Note that incase some of these change or are moved in the future, the commit hash I searched today was ce7dba6bdf2345f39ad8f39b8a4c1bac9bcd35ea

Categories

Resources